In our last post, we set up Python on your system and introduced the interactive interpreter (REPL). Now it’s time to start diving into the building blocks of Python programming. In this installment, we’ll cover how to:
- Print messages to the screen using print()
- Work with variables and basic data types
- Understand Python’s syntax and indentation rules
These fundamental concepts will form the core of everything you’ll do in Python moving forward.
Printing Text and Using the REPL Effectively
Let’s start with something simple. You’ve already seen how to display text using the print() function:
print("Hello, world!")
This outputs:
Hello, world!
The print() function is incredibly versatile, and you’ll use it often to see the results of your code. For a quick primer on print(), check out this short video tutorial.
Remember, you can experiment with print() right in the REPL. Just type python (or python3) in your terminal and start typing print statements. You might try:
print("Learning Python is fun!")
print("Python version:", 3.10)
This instant feedback loop makes it easy to learn by doing. Need a refresher on using the REPL? Revisit the REPL demo video.
Variables: Storing Information
Variables are like “labeled boxes” in your computer’s memory. You assign a name to a piece of data, and you can reuse that data easily by referring to its name later. Variables are created simply by assigning a value to a name:
message = "Hello, Python!"
number = 42
In this example, message is now storing the string "Hello, Python!" and number is storing the integer 42. You can print them out:
print(message)
print(number)
You can also change what’s stored in a variable at any time by reassigning it:
number = 100
print(number) # Now prints 100
For a deeper look at variables, watch this introductory variables video.
Basic Data Types
Variables can store different types of data. Some common ones include:
- Integers (int): Whole numbers, such as 42 or -7.
- Floating-Point Numbers (float): Numbers with a decimal point, such as 3.14 or 0.001.
- Strings (str): Text surrounded by quotes, like "Hello" or "My name is Python!".
- Booleans (bool): Values that are either True or False.
Try this in your REPL:
age = 30 # int
pi = 3.14159 # float
name = "Alice" # str
is_student = True # bool
print(age, pi, name, is_student)
To learn more about data types, consider checking out this Python data types overview.
Performing Simple Operations
You can use Python like a calculator. For instance:
x = 10
y = 3
print(x + y) # 13
print(x - y) # 7
print(x * y) # 30
print(x / y) # 3.3333...
Arithmetic with integers and floats is straightforward. Strings can also be manipulated. For example, you can concatenate (join) them:
first_name = "Alice"
last_name = "Anderson"
full_name = first_name + " " + last_name
print(full_name) # Alice Anderson
Watch this arithmetic and string operations tutorial for more examples.
Understanding Python’s Syntax and Indentation
One of the most distinctive features of Python is that it uses indentation (spaces or tabs) to define code blocks rather than braces ({}) like many other languages. For example, consider an if statement:
x = 10
if x > 5:
print("x is greater than 5")
Notice the four spaces before print(). This tells Python that print("x is greater than 5") belongs to the if block. If you don’t indent properly, you’ll get an error.
For a quick guide to Python’s syntax rules, check out this Python syntax basics video.
Tips for Beginners
- Start small: Play with print() and variables in the REPL to get comfortable.
- Experiment: Change variable values, print different data types, and try simple arithmetic.
- Practice consistency: Always use the same number of spaces for indentation. Most editors will handle this for you if you use their default settings.
What’s Next?
Now that you can print messages, use variables, and understand Python’s indentation, you’re ready to take on more complex topics. In the next post, we’ll learn about control flow—specifically, how to make decisions in code using if statements and how to repeat actions with for and while loops.
Coming Up: Getting Started with Python for Beginners #3: Control Flow – Conditions & Loops
- Using if, elif, and else statements
- Working with for and while loops
- Building a simple guessing game in the REPL
If you want a head start, check out this video on if statements and this tutorial on loops.
Wrapping Up
You’ve just taken another step forward in your Python journey. You now know how to print output, store and manipulate data in variables, and follow the rules of Python’s syntax and indentation. These skills will serve as the foundation for everything else you learn.
Keep experimenting, keep testing new ideas in the REPL, and don’t forget to have fun. Your understanding and confidence will grow as you practice.