Warm-up · Activity 1 of 7
// B1.1 · ~25 min · Beginner
Install Python and run your first script
After this lesson Python 3.14 runs on your computer. You can check its version, try code at the >>> prompt, and write a .py file that prints text and numbers.
Lesson 1 of 5 in B1 First steps
You will be able to
- Install Python 3.14 and check which version runs with python --version
- Tell the interactive prompt (>>> and ...) from a script, and run a script with python hello.py
- Use print() to show text and numbers, with several items on one line
Predict · Activity 2 of 7
Predict before you read on: what does this line print?
print("I am", 30 + 5, "years old")Practice · Activity 3 of 7
Match each thing you type or see to what it means.
Practice · Activity 4 of 7
Fill in one character so that the line prints Hello Ada, with a space between the words.
print("Hello"____ "Ada")print("Hello" "Ada")Practice · Activity 5 of 7
This file is run as a script with python main.py. What does it print?
2 * 10 print(2 + 3)Brain teaser · Activity 6 of 7
Brain teaser. What does this print?
print("2 + 2", 2 + 2)Apply · Activity 7 of 7
Mini-task, on your own computer. Create a file hello.py in a folder of your choice, with three print() lines: your name, the Python version you installed, and I ran my first script. Open a terminal in that folder and run python hello.py (python3 hello.py on macOS and Linux).
Check your work against this list
Build it yourself
Read the worked example, then write the exercises. Your code runs in your browser or on your computer and is never uploaded.
Worked example
A first script
This is a whole Python program. Save it as main.py in a new folder, open a terminal there and run python main.py (python3 main.py on macOS and Linux). Python runs the lines from top to bottom and prints the output below. Then change a text or a number, save, and run it again.
main.py
# main.py: a first script. Run it with: python main.py
print("Hello, world!")
print("Python runs this file from top to bottom.")
print()
print("2 + 3 =", 2 + 3)
print("Minutes in a day:", 24 * 60)
Run it with
python main.pyOutput
Hello, world!
Python runs this file from top to bottom.
2 + 3 = 5
Minutes in a day: 1440- The first line starts with #: it is a comment, and Python ignores it.
- print() with nothing inside writes the empty line in the middle of the output.
- "2 + 3 =" is printed as written, while 2 + 3 without quotes becomes 5.
- Each print() call writes one line, and the items inside it are separated by a space.
Exercises
Exercise 1 of 2
Hello from a file
On your computer, save the starter as main.py and run python main.py (python3 main.py on macOS and Linux). It prints one line. Add a second print() so the script prints exactly these two lines: Hello, Python! This is my first script. You can also run the checks on this page.
This exercise needs Python on your computer (the browser version cannot run it). The files and commands are below.
Hints
Hint 1
Each print() call writes one line, so a second line needs a second print().
Hint 2
Put the text in quotes and copy it exactly, including the full stop.
Hint 3
print("This is my first script.")
Show a solution
One way to solve it. Yours can look different and still pass the checks.
print("Hello, Python!")
print("This is my first script.")
Run it on your computer
Install Python 3.14 or newer. Save these files in one folder, open a terminal in that folder, and run the commands below.
main.py
print("Hello, Python!")
# Add a second line: This is my first script.
test_main.py
from learnrun import run_main
def lines():
return run_main().splitlines()
def test_first_line():
"""The first line is Hello, Python!"""
got = lines()
assert got[:1] == ["Hello, Python!"], f"the first line is {got[:1]!r}, expected ['Hello, Python!']"
def test_second_line():
"""The second line is This is my first script."""
got = lines()
assert got[1:2] == ["This is my first script."], f"the second line is {got[1:2]!r}, expected ['This is my first script.']"
def test_two_lines():
"""Exactly two lines are printed"""
got = lines()
assert len(got) == 2, f"the script printed {len(got)} line(s), expected 2"
On macOS and Linux, type python3 wherever these commands say python, as in the first lesson.
Run the program:
python main.pyRun the checks (needs learnrun.py in the same folder):
python learnrun.py testDownload learnrun.pyExercise 2 of 2
A name card
Write a script that prints this card, one print() per line: Name: Ada Lovelace Born: 1815 (an empty line) Age in 1843: 28 Let Python work out the 28 with 1843 - 1815 instead of typing it. Run it on your computer with python main.py (python3 main.py on macOS and Linux).
This exercise needs Python on your computer (the browser version cannot run it). The files and commands are below.
Hints
Hint 1
print("Born:", 1815) prints Born: 1815, because print() puts a space between its items.
Hint 2
print() with nothing between the parentheses writes an empty line.
Hint 3
print("Age in 1843:", 1843 - 1815): the sum has no quotes, so Python works it out.
Show a solution
One way to solve it. Yours can look different and still pass the checks.
print("Name: Ada Lovelace")
print("Born:", 1815)
print()
print("Age in 1843:", 1843 - 1815)
Run it on your computer
Install Python 3.14 or newer. Save these files in one folder, open a terminal in that folder, and run the commands below.
main.py
print("Name: Ada Lovelace")
# Next: Born: 1815, an empty line,
# then Age in 1843: followed by 1843 - 1815.
test_main.py
from learnrun import run_main
def lines():
return run_main().splitlines()
def test_name_and_year():
"""The card starts with the name and the year of birth"""
got = lines()[:2]
assert got == ["Name: Ada Lovelace", "Born: 1815"], f"the first two lines are {got!r}"
def test_empty_line():
"""The third line is empty"""
got = lines()[2:3]
assert got == [""], f"the third line is {got!r}, expected an empty line"
def test_age():
"""The last line is Age in 1843: 28"""
got = lines()[3:]
assert got == ["Age in 1843: 28"], f"the lines after the empty one are {got!r}, expected ['Age in 1843: 28']"
def test_calculated():
"""The age is calculated, not typed"""
with open("main.py", encoding="utf-8") as f:
code = f.read()
assert "1843 - 1815" in code, "calculate the age with 1843 - 1815"
On macOS and Linux, type python3 wherever these commands say python, as in the first lesson.
Run the program:
python main.pyRun the checks (needs learnrun.py in the same folder):
python learnrun.py testDownload learnrun.pyCommon mistakes
Typing a terminal command at the >>> prompt
python hello.py
What Python prints
SyntaxError: invalid syntaxWhy, and the fix
python hello.py is a command for the terminal, not Python code. At the >>> prompt, Python tries to read it as code and fails. Leave the interpreter with exit(), then type the command at the normal terminal prompt, in the folder where hello.py is saved.
A capital P in print
Print("Hello")
What Python prints
NameError: name 'Print' is not defined. Did you mean: 'print'?Why, and the fix
Python tells upper and lower case apart, so Print and print are different names, and only print exists. Write print in lower case. The message even suggests the right name.
A missing closing parenthesis
print("Hello"
What Python prints
SyntaxError: '(' was never closedWhy, and the fix
Every ( needs a matching ). Python reads to the end of the file looking for it and then gives up before running anything. Add the ) at the end: print("Hello").
Python in the browser: Pyodide 314.0.7, MPL-2.0. Licence and source
Exit ticket
5 questions, no hints. Score 80% or more to complete the lesson.
Finish every activity above to unlock the exit ticket.