Python Foundations Lesson 1 of 7

Your first Python program

20 min, 5 exercisesOCR 2.2.1AQA 3.2.7Edexcel 6.4.1

Learn

Printing with print()

A program is a list of instructions, and Python reads them from the top down, one line at a time. The very first instruction most people learn is print(), which shows something on the screen.

print("Hello, world!")
OUTPUT "Hello, world!"
print("Hello, world!")

Three things are going on in that one line:

  • print is the name of a function, a ready-made command that Python already knows.
  • The round brackets ( ) hold what you hand to the function.
  • "Hello, world!" is a string: a piece of text. The quotes tell Python "this is text, not code". Single quotes work too: 'Hello'.

Learn

Errors and multiple values

Forget the quotes and Python tries to treat Hello as the name of something, gets confused, and throws an error. That is not you breaking the computer. Errors are just Python saying "I don't understand", and you will read lots of them.

Each print() puts its text on a new line. You can also give print() several things separated by commas, and it puts a space between them:

print("I am" + " " + str(15) + " " + "years old")
OUTPUT "I am" + " " + INT_TO_STRING(15) + " " + "years old"
print("I am", 15, "years old")

Learn

Comments and the run loop

Lines starting with # are comments. Python ignores them completely; they are notes for humans (including future you, who will have forgotten everything).

One more thing: type the code yourself. Copy-paste teaches your clipboard, not your brain. Hit Run, read the output, change something, run it again. That loop is the whole job.

GCSE exam boards
  • OCR 2.2.1 Programming fundamentals
  • AQA 3.2.7 Input and output
  • Edexcel 6.4.1 User input

Try

Run it: a few printed lines

The example is already in the editor. Run it, change one thing, and run it again.

In exam pseudocode
print("Hello, world!")
print("Python is reading this line second.")
print("You can print numbers too:" + " " + str(42))
print("Single quotes work as well.")
OUTPUT "Hello, world!"
OUTPUT "Python is reading this line second."
OUTPUT "You can print numbers too:" + " " + INT_TO_STRING(42)
OUTPUT "Single quotes work as well."
# Python runs these lines from top to bottom
print("Hello, world!")
print("Python is reading this line second.")
print("You can print numbers too:", 42)
print('Single quotes work as well.')
example.py to run
# Python runs these lines from top to bottom
print("Hello, world!")
print("Python is reading this line second.")
print("You can print numbers too:", 42)
print('Single quotes work as well.')

Practice

Say hello

Make the program print exactly this one line:

Hello, Python!

Capital letters, comma and exclamation mark all count.

exercise_1.py to run
# Change the text inside the quotes
print("Hello")

Practice

Three lines

Print three lines, in this order, using three print() calls: Ready, then Set, then Go!

exercise_2.py to run
print("Ready")

PracticePredict the output

Commas and comments

Read the program and type exactly what it prints. Two things to watch: the line that starts with #, and what the commas do.

exercise_3.py
# print("Ready?")
print("Hi", "there")
print("Score:", 7, "points")

Break/fixFix the bug

The game will not start

This program should print two lines, but Python refuses to run any of it. Run it, read the error, then fix it.

exercise_4.py to run
print("Welcome to the game!)
print("Press start")

Your turn

A game's title screen

Write a program that prints this title screen, line for line. On the middle line, give print() the text and the numbers as separate values with commas, not as one string.

Output
SPACE DRIFT
Level 1 of 12
Press start

Constraint

  • Use commas inside print() on at least one line.

On your own. Hints open after your first check.

exercise_5.py to run
Python 3.13

Code editor. Press Control or Command plus Enter to run the code. Tab indents; to move focus out of the editor, press Escape and then Tab.