Lesson 1
True or false
Booleans, comparing values, and the difference between = and ==.
True and False
Python has two special values, True and False, with capital letters. They’re called booleans.
Comparing two values gives you a boolean:
==equal to, and!=not equal to
<less than,>greater than,<=and>=for “or equal to”
Careful: one = stores a value in a variable. Two, ==, ask a question.
print(5 > 3)
print(2 == 3)
print("cat" == "cat")
print(10 != 7)
and, or, not
a and bisTrueonly when both are true
a or bisTruewhen at least one is true
not aflips it:not TrueisFalse
sunny = True
warm = False
print(sunny and warm)
print(sunny or warm)
print(not warm)
Lesson 2
if, elif and else
Run code only when something is true, with Python’s famous indentation.
if
if runs some lines only when a condition is true. The line ends with a colon :, and the lines that belong to it are indented by 4 spaces.
Indentation isn’t just neat in Python: it’s how Python knows which lines are inside the if. The last line below isn’t indented, so it always runs.
temperature = 30
if temperature > 25:
print("It is hot!")
print("Drink some water.")
print("Have a nice day.")
else and elif
else: runs when the if condition is false.
elif (short for “else if”) adds more conditions. Python checks them from the top down and runs only the first one that’s true.
score = 72
if score >= 90:
print("Gold")
elif score >= 70:
print("Silver")
else:
print("Keep going")
Lesson 3
Asking with input()
Ask a question, and turn the answer into a number.
input() asks a question
input("question") shows the question, waits for someone to type an answer, and gives the answer back.
Here in the lessons, the answers are typed in for you, and you’ll see them in the console straight after the question.
name = input("What is your name? ")
print(f"Nice to meet you, {name}!")
Turning text into numbers
input() always gives back a string, even when someone types a number like 14. To do maths with it, turn it into a number first:
int("14")gives the whole number14
float("2.5")gives the decimal number2.5
str(14)goes the other way and gives the string"14"
age = int(input("How old are you? "))
print(f"Next year you will be {age + 1}.")
Recap
What Level 2 covers, in a few lines.
TrueandFalse(with capital letters) are booleans. Comparisons give booleans:==equal,!=not equal,<,>,<=and>=.=stores a value;==asks whether two values are equal.andis true when both sides are;orwhen at least one is;notflips true and false.if condition:runs the indented lines under it only when the condition is true.elifandelsehandle the other cases. Python uses the indentation (4 spaces) to know what’s inside.input()asks a question and gives back what was typed, always as a string.int()andfloat()turn it into a number.
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.