Module 1 · print() — making Python show something
1
print() — making Python show something
Unread
▶
A Python program is a list of instructions that run one after another. The first instruction almost everyone learns is print(): it shows whatever you put between its brackets.
Text goes inside quotes — 'single' or "double", both work. Numbers go in without quotes. Give print() several things separated by commas and it shows them on one line with a space between each.
Press Run on the examples below: the output panel shows exactly what print() produced.
Text needs quotes, numbers don't. Each print() starts a new line.
print('Hello, world!')
print("Double quotes work too")
print(42)
print(3.5)
Commas: several values on one line, separated by spaces.
print('I am', 30, 'years old')
print('2 + 3 =', 2 + 3)
2
Variables — giving a value a name
Unread
▶
A variable is a name that points at a value. You create one with a single equals sign: name = value. Read = as 'is set to', not 'equals'.
Once a name exists you can use it anywhere you would use the value. You can also point the same name at a new value later — the old one is simply forgotten. A very common pattern is updating a variable from its own current value: score = score + 10 means 'take the current score, add 10, and store the result back in score'.
Create two variables and use them.
city = 'Lisbon'
population = 545000
print(city, 'has', population, 'people')
Reassigning and updating a variable.
score = 0
print('start:', score)
score = score + 10
print('after one goal:', score)
score = 100
print('reset to:', score)
3
Naming rules and good names
Unread
▶
Variable names may use letters, digits and the underscore _, but must not start with a digit and cannot contain spaces or hyphens. Python is case-sensitive: age, Age and AGE are three different names.
Some words are reserved by Python itself (if, for, class, True, None…) and can't be used as names.
By convention Python names are written in snake_case: lower-case words joined by underscores, like total_price or user_name. Pick names that say what the value means — total_price beats tp or x.
Valid, readable snake_case names. Case matters: these are two different variables.
total_price = 19.99
items_in_cart = 3
first_name = 'Ada'
name = 'lower'
Name = 'Upper'
print(name, Name)
4
Python runs your code from top to bottom
Unread
▶
Python reads your program line by line, from the first line to the last, and does each instruction in turn. That has two consequences.
First, a name must be created before it is used: if line 1 prints total and total is only set on line 2, Python stops with an error at line 1.
Second, a variable holds whatever it was last set to at that moment. b = a copies a's CURRENT value into b; changing a afterwards does not change b. Use Step through on the example to watch each line run and the variables change.
Step through this: b keeps the value a had when b was set.
a = 5
b = a
a = 99
print('a is', a)
print('b is', b)
5
Reading an error message
Unread
▶
Errors are normal — every programmer sees them all day. When Python can't run a line it stops and prints a traceback. Read it from the bottom: the last line names the error type and says what went wrong, and the lines above point at where it happened.
Three you will meet first:
• NameError — you used a name that doesn't exist (often a typo or wrong capital letter).
• SyntaxError — the line isn't valid Python (a missing bracket or quote).
• TypeError — an operation on the wrong kind of value, like adding text to a number.
Run it: a NameError. The last line says which name Python couldn't find.
message = 'hi'
print(mesage)
Run it: a TypeError — text and a number can't be added with +.
age = 30
print('Age: ' + age)
1
Using a variable before you create it
Unread
▶
Python runs from top to bottom, so a name only exists after the line that assigns it. Using it earlier raises NameError.
❌ The tempting version
Run it — or step through it — and watch it go wrong.
print(total)
total = 10 + 5
Watch it break first — the fix sticks better that way.
✅ The fix
Changed lines are marked in green.
total = 10 + 5
print(total)
Why? Assign first, use second. When you see NameError, check the name is spelled the same way AND that its assignment runs before this line.
2
Quotes around a variable's name
Unread
▶
Quotes turn something into text. print('name') prints the four letters n-a-m-e, not the value stored in the variable name.
❌ The tempting version
Run it — or step through it — and watch it go wrong.
name = 'Grace'
print('Hello,', 'name')
Watch it break first — the fix sticks better that way.
✅ The fix
Changed lines are marked in green.
name = 'Grace'
print('Hello,', name)
Why? Quotes = this exact text. No quotes = look up this variable. Leave the quotes off when you mean the variable.
3
Adding text and a number with +
Unread
▶
+ joins two pieces of text, or adds two numbers — but it can't do one of each. 'Age: ' + 30 raises TypeError.
❌ The tempting version
Run it — or step through it — and watch it go wrong.
age = 30
print('Age: ' + age)
Watch it break first — the fix sticks better that way.
✅ The fix
Changed lines are marked in green.
age = 30
print('Age:', age) # a comma puts a space between them
print('Age: ' + str(age)) # or turn the number into text first
print(f'Age: {age}') # or use an f-string
Why? Either pass both to print() with a comma, convert the number with str(), or use an f-string (you'll meet those properly in a later module).
4
Capital letters change the name
Unread
▶
Python is case-sensitive. Score and score are different variables, so a wrong capital letter gives a NameError — or worse, silently uses a different variable.
❌ The tempting version
Run it — or step through it — and watch it go wrong.
Score = 10
print(score)
Watch it break first — the fix sticks better that way.
✅ The fix
Changed lines are marked in green.
score = 10
print(score)
Why? Stick to lower-case snake_case for variables and you will rarely trip over this.
5
Reusing a built-in name like print
Unread
▶
Names such as print, list or input already mean something. Python lets you reassign them — and then they stop working for the rest of the program.
❌ The tempting version
Run it — or step through it — and watch it go wrong.
print = 'oops'
print('Hello')
Watch it break first — the fix sticks better that way.
✅ The fix
Changed lines are marked in green.
label = 'oops'
print('Hello', label)
Why? After print = 'oops', the name print points at a piece of text, and text can't be called like a function (TypeError: 'str' object is not callable). Choose a different name.
Question 1
Easy
What does print() do?
Question 2
Easy
Which of these is a valid variable name?
Question 3
Easy
What does this print?
x = 5
x = x + 3
print(x)
Question 4
Medium
What does this print — or which error does it raise?
Score = 10
print(score)
Question 5
Easy
Where in an error message do you find the error type and what went wrong?
Challenge 1
What is printed?
print('Hello', 'world', 2026)
My prediction:
Challenge 2
What is printed?
a = 3
b = a
a = 10
print(a, b)
My prediction:
Challenge 3
What are the two printed lines?
print('3' + '4')
print(3 + 4)
My prediction:
Challenge 4
What are the two printed lines?
name = 'Ada'
print('name')
print(name)
My prediction:
Challenge 5
What is printed?
count = 0
count = count + 1
count = count + 1
print(count * 10)
My prediction: