Lesson 1 of 35Python
Python · Lesson 1 · 15 minutes
Hello, Python!
What you'll learn
- Print text and numbers with
print() - Create and use variables
- Combine text and numbers with f-strings
Your first program
A program is a list of commands that the computer works through from top to bottom. The most important command at the start is print(). It writes something on the screen.
Press Run in the editor (or Ctrl + Enter, on a Mac ⌘ + Enter):
print("Hello, world!")
print("I'm learning Python.")
print(7 * 6)Text always goes in quotes. Numbers don’t – that’s why Python calculates 7 * 6 instead of copying it. Try it: write "7 * 6" with quotes and see what happens.
Variables: boxes with names
A variable is like a labelled box. You put a value in and can find it again later by its name. The equals sign = doesn’t mean “is equal to” here, it means “store in”.
points = 10
print(points)
points = points + 5
print(points)Now press Steps. You can see line by line what happens in memory. Watch how points changes from 10 to 15 – the old number is simply replaced.
Remember: Variable names are written in lower case, without spaces:
points,my_name,lives_left.
Combining text and numbers
Often you want to print text and variables in one sentence. The easiest way is an f-string: you write an f in front of the quotes and put variables in curly brackets { }.
game = "Minecraft"
hours = 3
print(f"I'm playing {game}.")
print(f"Already {hours} hours today, {hours * 2} tomorrow?")Python can even calculate inside the brackets, like {hours * 2}.
Typical mistake:
print("Age: " + 12)doesn’t work because you can’t join text and a number with+. With an f-string that won’t happen to you.
Quick check
Before you start the task: what does this program print? Type your guess – then Python works it out.
x = 5
x = x + 2
print("x is", x)Your task
Your turn
Create a variable name with your first name and a variable age with your age as a number. Then print exactly this sentence: My name is Lena and I am 12 years old. (with your own values, of course). In a second line print how old you'll be next year: Next year I will be 13.
# Create your variables here:
name = ""
age = 0
# Print the two sentences here:- Something to think about
You need two boxes with names: one for text, one for a number. What changes about the age in the second sentence – and who should work it out?
- Which command helps?
Put your name in quotes, e.g. name
="Lena".The age is a number without quotes, e.g. age
=12.Check every character, including the full stop at the end. Tip – use an f-string.
Let Python do the maths – inside an f-string {age
+1} works. - Gap code – fill in the ___
name = ___ age = ___ print(___) print(___)
Solve it without hints to earn a ⭐ on your progress page.
Show solution🔒 Unlocks after all hints or 2 checks
Try it yourself first – mistakes teach you the most. Here is one possible solution:
name = "Lena"
age = 12
print(f"My name is {name} and I am {age} years old.")
print(f"Next year I will be {age + 1}.")For fast learners
Pro task
The variable seconds holds a time in seconds. Print it like a digital clock: 01:02:05 for 3725 seconds (1 hour, 2 minutes, 5 seconds). Every number has exactly two digits. Calculate with // and % – and remember: the test tries other times too.
seconds = 3725
# Work out hours, minutes and seconds and print them as 01:02:05Show pro solution
seconds = 3725
hours = seconds // 3600
minutes = seconds % 3600 // 60
rest = seconds % 60
print(f"{hours:02d}:{minutes:02d}:{rest:02d}")