Making decisions

Level 2 of Python School: 3 lessons, explained from scratch, with examples.

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.

Python
print(5 > 3)
print(2 == 3)
print("cat" == "cat")
print(10 != 7)

and, or, not

  • a and b is True only when both are true
  • a or b is True when at least one is true
  • not a flips it: not True is False
Python
sunny = True
warm = False
print(sunny and warm)
print(sunny or warm)
print(not warm)

Practise “True or false” in the game

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.

Python
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.

Python
score = 72
if score >= 90:
    print("Gold")
elif score >= 70:
    print("Silver")
else:
    print("Keep going")

Practise “if, elif and else” in the game

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.

Python
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 number 14
  • float("2.5") gives the decimal number 2.5
  • str(14) goes the other way and gives the string "14"
Python
age = int(input("How old are you? "))
print(f"Next year you will be {age + 1}.")

Practise “Asking with input()” in the game

Recap

What Level 2 covers, in a few lines.

  • True and False (with capital letters) are booleans. Comparisons give booleans: == equal, != not equal, <, >, <= and >=.
  • = stores a value; == asks whether two values are equal.
  • and is true when both sides are; or when at least one is; not flips true and false.
  • if condition: runs the indented lines under it only when the condition is true. elif and else handle 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() and float() 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.

Start learning