Lesson 1
Your first Python program
What Python is, print(), and text in quotes.
What is Python?
Python is a programming language. You write instructions, and the computer follows them one by one, from the top of your program to the bottom.
It’s one of the most popular languages in the world because it’s easy to read. People use it to build websites, study data, train AI, make games and do boring jobs automatically.
Everything in this course is real Python 3, running right here on your device.
Say hello with print()
print() shows whatever you put inside its brackets.
Text has to go inside quotes, either "double" or 'single'. A piece of text in quotes is called a string.
Each print() starts a new line. Press Run this code to see it.
print("Hello, world!")
print("I am learning Python.")
Lesson 2
Numbers and maths
Adding, multiplying, three kinds of dividing, and why 7 and "7" are different.
Python is a calculator
Numbers don’t need quotes. Python can do maths with them:
+adds and-takes away
*multiplies (a star, not an x)
**is “to the power of”:2 ** 3is 2 × 2 × 2
Multiplying happens before adding, like in maths class. Use brackets to change the order.
print(2 + 3)
print(10 - 4)
print(6 * 7)
print(2 ** 3)
print((2 + 3) * 4)
Three kinds of dividing
/divides and always gives a decimal number (a float):7 / 2is3.5, and even8 / 2is4.0
//divides and throws away the part after the point:7 // 2is3
%gives the remainder:7 % 2is1, because 7 is 2 × 3, plus 1 left over
% is handy for spotting even numbers: if n % 2 is 0, n is even.
print(7 / 2)
print(8 / 2)
print(7 // 2)
print(7 % 2)
Numbers and text are different
7 is a number. "7" (in quotes) is a string, a piece of text that happens to look like a number.
With numbers, + adds. With strings, + sticks them together. Mixing the two, like "7" + 7, stops with a TypeError.
print(7 + 7)
print("7" + "7")
Lesson 3
Variables and f-strings
Give values names, change them, and put them inside text.
Variables are names for values
A variable gives a value a name, so you can use it later. = means “from now on, this name means this value”:
name = "Ama"makes a variable calledname
age = 14makes one calledage
Names can have letters, digits and underscores, but no spaces, and they can’t start with a digit. Python programmers write long names like high_score, with underscores.
name = "Ama"
age = 14
print(name)
print(age + 1)
Changing a variable
You can give a variable a new value whenever you like. The old one is forgotten.
score = score + 10 works out the right side first (the old score plus 10), then stores the answer. The short way to write it is score += 10. There’s also -=, *= and /=.
score = 0
score = score + 10
score += 5
print(score)
f-strings: values inside text
Put an f just before the quotes, and anything in {curly brackets} is replaced by its value. This is an f-string.
Lines that start with # are comments: notes for people, which Python skips.
name = "Sly"
legs = 0
print(f"{name} has {legs} legs.")
# This line is a comment: Python skips it
Recap
What Level 1 covers, in a few lines.
- Python is a programming language: you write instructions, and the computer follows them in order, from the top down.
print()shows something on the screen. Text goes inside quotes, likeprint("Hello"). Text in quotes is called a string.- Python is a calculator too:
+,-,*and/, plus**for powers,//for whole-number dividing and%for the remainder./always gives a decimal, like4.0. - A variable is a name for a value:
age = 14. You can change it later:age = age + 1, orage += 1. - An f-string puts values inside text:
f"I am {age}". Lines starting with#are comments, which Python skips.
Now try it in Python School
Write real Python and run it right there, with a check after every step and a test at the end of each level. Free.