code2

“Learn Python”, you have probably come across this advice regardless of your background. Then comes the question, “Where should I start?” My suggestion: don’t overwhelm yourself trying to learn everything about a programming language when you are just starting. You’ll most likely end up bored and demotivated very soon, and eventually quit learning in the middle. Have you already experienced something like that? Share your experience in the comments.

So, what’s the best way to learn Python or any other programming language as a beginner?

  1. Find an interesting goal that you wish to achieve through coding. It can be web development, game development, data analysis, or task automation.
  2. Then follow your preferred learning platform, focusing on that goal. Build interesting projects as you learn.

However, you still need to know the basics of coding. This 10-part series will teach you those basics with examples and a few challenges at the end of each part. The final part is a walk-through of a mini Python project. You will be ready to start building Python projects in 10 days. The series will progress as follows:

  • Day 1: Hello World (This article)
  • Day 2: Python Data Types and Operations
  • Day 3: Control Flow – Conditions and Loops
  • Day 4: Functions in Python
  • Day 5: Python Data Structures
  • Day 6: Files, Folders, and Paths
  • Day 7: Debugging Techniques
  • Day 8: Object Oriented Programming (OOP)
  • Day 9: Convert Your Code Into Modules and Packages
  • Day 10: Final Python Project – Bringing It All Together

After completing this series, you will be able to do the following:

  • Do basic computations and printing tasks using Python. Learn to use Jupyter Notebook and PyCharm.
  • Solve any problems that require conditions or repetition of a task.
  • Modularize your messy code, make it cleaner and more efficient.
  • Handle files lying around in various locations on your PC.
  • Learn to convert your code into modules and packages like a pro coder.
  • Complete challenges and a mini project to build your confidence in Python.

Enough with the introduction. Let’s learn Python with Tanim (yes, that’s me)!

Day 1 of 10: “Hello World!”

Why Python

Python is one of the most versatile and beginner-friendly programming languages, making it an ideal choice for both novices and experts alike. Its simple syntax, vast libraries, and strong community contribute to its widespread adoption in fields ranging from web development to artificial intelligence. Unlike other languages, Python allows you to focus on solving problems rather than worrying about intricate syntax, making it a top choice for rapid prototyping and development.

“Python is a High-Level Interpreted Object-Oriented language used for General Purpose programming.”

Let’s break down the above line to clear up some characteristics of Python:

  • Python is a High-Level Language
    • A high-level language hides most of the hardware details and provides a syntax closer to human language, making it easier to read and write code. So, Python spares developers from dealing with memory management and CPU instructions while coding and allows them to focus on problem-solving. You will see that commands in Python are almost like regular English.
    • In contrast, a low-level language (e.g., Assembly, C) is closer to machine code (i.e., 0s and 1s) and gives more control over hardware. For example, in Assembly, you have to specify which memory location will be utilized to store a variable. In C, memory must often be managed manually with functions like malloc() and free(). Although this gives more control over hardware; reading, writing, and debugging (i.e., finding faults and fixing codes) becomes more difficult.
  • Python is an Interpreted Language
    • Interpreted languages execute code line by line through an interpreter. They don’t need to be compiled into machine code before execution. This makes Python platform-independent and allows interactive and faster development.
    • On the other hand, compiled languages like C++ or Java are compiled into machine code first, and then executed by the computer. This results in faster execution but slower code development
  • Python is an Object-Oriented Language
    • Object Oriented Programming (OOP) is a paradigm based on the concept of ‘objects’ that contain both data and methods. We will go into details of OOP later.
    • Other paradigms are Procedural Programming (like in C) and Functional Programming (like in Haskell). These languages organize code into procedures or functions and directly operate on data. Python supports OOP principles like classes, inheritance, encapsulation, and polymorphism. However, it’s also a multi-paradigm, supporting procedural and functional programming. This makes Python versatile. In contrast, Java is fully object-oriented, requiring all code to be written inside classes.
    • Don’t worry if OOP doesn’t make much sense now. Even experienced coders struggle with OOPs. In this series, we’ll learn functional programming first and then cover the basics of OOP.
  • Python is a General Purpose Language
    • A general-purpose language can be used to build a wide variety of applications, from web development to machine learning. Python’s extensive libraries and frameworks make it versatile for tasks ranging from web development (Django, Flask), game development (Pygame), data analysis (pandas), and artificial intelligence (Pytorch, TensorFlow). In summary, you can pretty much do anything with Python.
    • On the other hand, Domain Specific Languages (DSL) are designed for specific tasks and are limited outside their scope of application. For example, SQL is designed for database queries, and HTML is for web page structuring.

Now, you know a little bit more about Python and various programming paradigms in general. You don’t need to memorize any of the above. Feel free to come back to this section when you feel the need. Now that these terms are out of the way, let’s start coding in Python!

Setting Up Your Python Environment (fix this section)

Before diving into coding, it’s essential to set up a proper environment to ensure a smooth learning experience. There are plenty of Integrated Development Environments (IDEs) and code editors to help you write and run Python efficiently. VS Code, PyCharm, Jupyter Notebook, and Google Colab are the most commonly used. We’ll primarily use Jupyter Notebook for this series due to its interactive nature, which is perfect for experimenting and learning. Later, we will move to PyCharm to utilize the full potential of an IDE.

We will use Anaconda to install Python and Jupyter Notebook, as it provides a convenient package manager. I show the step-by-step method to install Python and Jupyter Notebook using Anaconda in the following video. This walkthrough also explores the user interface of Jupyter Notebook.

Watch the video to follow along. If you still face issues, let me know in the comments.

The ‘print()’ Function: Your first code in Python

The print() function is one of the most commonly used functions in Python. It allows you to display output to the console/ output screen. Let’s follow the tradition of printing ‘Hello World!’ as your first code. To do this, just write “Hello World!” inside the parentheses as shown below. Use a code cell in your Jupyter notebook as shown in the video.

Python
print("Hello World!")

Output:
Hello World!

You can print anything you write inside the double quotation marks or single quotation marks. Anything inside the quotation marks is called a “string”. Note that the ‘p’ in print is not capitalized. Python is case sensitive, hence ‘Print(“Hello World!”)’ will throw a ‘Name Error’. Try to generate this error yourself. Don’t worry, it’s not going to break anything. It’s a good practice to familiarize yourself with reading error messages. Well, that’s where every coder spends most of their time! Once you fix the bugs and your code works smoothly, that’s the feeling we code for.

Let’s explore a few more use cases of the print() function.

Displaying Variables

Variables are used to store values in Python. For example, we store the string “Alice” in the variable “name” and the integer “25” in the variable “age”. Later we can use these values by calling the variables. Create a new code cell to do this:

Python
name = "Alice"
age = 25
print(name) #Notice that we do not use quotation marks when using variables inside print
print(age)

Output:
Alice
25

‘#’ is used to comment in Python. Anything you write after ‘#’ will be considered a comment. Comments are not executed like commands. We use comments to explain what’s happening in the code. Moving on…

String Formatting

There is a feature called ‘f-string'(formatted string literals) that helps us combine strings and variables inside the print statement. This is very handy. The variables must be inside the curly braces. Let’s print a sentence with the variables “name” and “age”.

Python
print(f"This is {name}. She is {age} years old.")

Output:
This is Alice. She is 25 years old.

Notice how the print function prints the values of the variables. Let’s change the values of the variables and print again. Use a new code cell:

Python
name = "Morgan"
age = 45
print(f"This is {name}. She is {age} years old.")

Output:
This is Morgan. She is 45 years old.

It is also possible to combine strings and variables without the f-string. Look at the following example where we use commas. However, this doesn’t give us the flexibility like the f-string.

Python
print("This is ",name,". She is ",age," years old.")

Output:
This is Morgan. She is 45 years old.

Multiline String

What if you want to write something big that spans more than one line inside the print statement? Then you can use the multiline string format by using 3 quotation marks as shown below.

Python
print("""This is
a multiline string.
Use it when you need
more space to write inside the print function""")

Output:
This is
a multiline string.
Use it when you need
more space to write inside the print function

But what if you want indentation or a new line in the output? You can use special characters like ‘\t’ to print a ‘Tab’ and ‘\n’ to print in a new line. Let’s look at an example.

Python
print("\tThis is an indentation. \nThis is a new line.")
#Notice that we wrote everythin on the same line, yet the output is different

Output:
This is an indentation.
This is a new line.

Suppressing Newlines

By default, the print function uses a newline for each print statement. For example,

Python
print("AI")
print("With")
print("Tanim")

Output:
AI
With
Tanim

We can suppress this behavior by using the ‘end=’ parameter. Let’s put spaces instead of newlines in the previous example.

Python
print("AI", end=" ")
print("With", end=" ")
print("Tanim")

Output:
AI With Tanim

Challenge Time!

  1. Challenge 1: Print the desired output.
    Write a Python program that prints your name, your current study, and your country. First, print each on a separate line using a single print. Then, print all 3 on the same line, separated by a tab.
  2. Challenge 2: Find the printing bug.
    Here’s a buggy line of code. Can you fix it?
    Print(“Hello, my name is ” + name + “and this is day ” + day + “of learning Python.”)

Post a screenshot of your code and the output in the comments.

Conclusion

This is all for day 1 of learning Python at AI With Tanim. Congratulations on printing your first hello in Python!

Make sure to practice the examples and play around with the print function, comments, and variables. Try to solve the challenges by yourself. Click ‘Next’ for part 2, where we’ll dive into Python’s basic syntax and data types.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top