Module 02 โ Print, Variables & Basic Output
Talking back to the screen, storing values with names, and Python's core data types.
Prerequisite: Module 01 โ Introduction & Setup
After this lesson, you will be able to
- Use print() with multiple values, separators, and escape sequences
- Create and update variables, and understand Python's naming rules
- Use Python as a calculator with +, -, *, /, //, %, **
- Identify a value's type using type()
Concept & Syntax
print() is how a Python program talks to the outside world. A variable is a
named box in your computer's memory that holds a value โ think of it as a sticky note with a name on it, stuck
to a piece of data. Once you understand these two things, you can already build something that computes and
reports results, which is the heart of programming.
Variables matter because programs need to remember things between steps: a user's age, a running total, a game score. Without a name for a value, you'd have no way to refer back to it later in your code.
variable_name = value
print(value1, value2, sep=", ", end="\n")Examples
print() with several values Very Easy
print("Score:", 95, "/", 100)print() accepts any number of items separated by commas and automatically puts a space between them.
Escape sequences Easy
print("She said, \"Python is fun!\"")
print("Line one\nLine two")
print("Tab\tSeparated")A backslash before a character gives it special meaning: \" is a literal quote, \n is a new line, and \t is a tab.
Raw strings and emoji Intermediate
path = r"C:\Users\name\Documents"
print(path)
print("Python is fun ๐")Prefixing a string with r makes it a raw string, so backslashes are treated literally โ essential for Windows file paths. Emoji are just Unicode characters and print like any other text.
Python as a calculator Easy
print(7 + 3)
print(7 - 3)
print(7 * 3)
print(7 / 3)
print(7 // 3)
print(7 % 3)
print(7 ** 2)/ always gives a decimal result, // gives the whole-number (floor) division, % gives the remainder, and ** raises to a power.
Variables and type() Real World
age = 25
price = 19.99
name = "Maria"
is_member = True
print(type(age), type(price), type(name), type(is_member))Python figures out a value's type automatically. The four basics here are integers, floating-point numbers, strings (text), and booleans (True/False).
Visual Explanation
๐ What happens internally? (advanced)
Under the hood, Python compiles your source into bytecode and runs it on a virtual machine (the CPython interpreter). Variables are names in a namespace dictionary pointing at objects in memory โ which is exactly what the memory panel in the tracer above is showing you.
See variables appear in memory as each line runs
Use the controls below to run this example one line at a time and watch the interpreter's memory update live.
Code
x = 10
y = 20
result = x + y
print(result)
Interpreter state
Console output
Common Mistakes
Variable names in Python must start with a letter or underscore, contain no spaces, and can't be a reserved word like <code>print</code> or <code>class</code>.
2nd_place = "Silver"
my variable = 5second_place = "Silver"
my_variable = 5Forgetting quotes around text makes Python think you're referring to a variable that doesn't exist yet.
print(Hello)print("Hello")- Use
snake_casefor variable names:total_price, notTotalPrice. - Pick descriptive names โ
agebeatsaonce code gets longer than a few lines. - Use raw strings (
r"...") for Windows file paths and regular expressions. - Check a value's type with
type()whenever a bug looks type-related.
Exercise & Challenge
Exercise 1 โ Fill in the blank
count = 42
____________________Complete the line so that it prints exactly: Total: 42 apples (with the word "Total:", a space, the number, a space, and "apples"). Type only the missing print() call.
๐ Challenge
Write a program that stores the price of an item and a quantity in two variables, then prints the total cost formatted to two decimal places.
- Use variables named price and quantity
- Compute total = price * quantity
- Print something like 'Total: $59.97'
๐ก Need a hint?
You can round with round(total, 2), or format with an f-string like f"{total:.2f}".
๐ Show a sample solution
price = 19.99
quantity = 3
total = price * quantity
print(f"Total: ${total:.2f}")Quiz
Answer every question, then submit to see your score and explanations.
1. What does 7 // 3 evaluate to in Python?
2. Which escape sequence inserts a new line inside a string?
3. Which of these is a valid Python variable name?
4. What type does type(19.99) report?
5. What does a raw string like r"C:\Users" do?
Summary
- print() displays one or more values, automatically spacing and newlining them.
- Variables are names pointing at values; naming rules matter (no spaces, no reserved words).
- Python's core math operators include //, %, and ** alongside the familiar +, -, *, /.
- type() tells you exactly what kind of value you're working with.