Lesson 2 of 35Python
Python · Lesson 2 · 20 minutes
Decisions with if
What you'll learn
- Read input with
input()and turn it into numbers withint() - Compare values (
<,>,==, …) - Take different paths with
if,elifandelse
The computer asks back
With input() your program can ask you something. Try it – an input field appears below the editor:
name = input("What's your name? ")
print(f"Hi {name}, great to have you here!")Important: input() always returns text, even if you type a number. To calculate you have to turn it into a whole number with int():
number = int(input("Enter a number: "))
print(f"Double that is {number * 2}.")Comparing
A comparison always gives True or False:
| Sign | Meaning | Example |
|---|---|---|
< | less than | 3 < 5 → True |
> | greater than | 3 > 5 → False |
<= | less than or equal | 5 <= 5 → True |
>= | greater than or equal | 4 >= 5 → False |
== | equal | 2 + 2 == 4 → True |
!= | not equal | "a" != "b" → True |
Watch out: One
=stores a value, two==compare. Everybody mixes them up at the start.
if, elif, else
With if your program decides which code runs. Everything that is indented (4 spaces) belongs to the if.
temperature = 28
if temperature > 25:
print("Off to the pool!")
elif temperature > 15:
print("Perfect for cycling.")
else:
print("Better stay in and code.")
print("This line always runs.")Python checks from top to bottom and takes the first path that fits – after that the others are skipped. Change the temperature to 20 and to 5 and use Steps to see which lines are skipped.
Remember: After
if,elifandelsethere’s always a colon:. The next line is indented.
Quick check
Before you start the task: what does this program print? Type your guess – then Python works it out.
points = 7
if points > 8:
print("great")
elif points > 5:
print("good")
else:
print("keep practising")Your task
Your turn
A cinema sells tickets by age. Ask for the age with input() and print the right ticket: under 14 years Child ticket: 5 euros, 65 and over Senior ticket: 6 euros, everyone else Standard ticket: 9 euros.
age = int(input("How old are you? "))
# Decide here which ticket fits:- Something to think about
At age 10 exactly “Child ticket: 5 euros” should appear – and no other ticket.
- Which command helps?
14 is no longer “under 14”. Check whether you use
<or<=.“65 and over” means 65 is included – so
>=65. - Gap code – fill in the ___
age = int(input("How old are you? ")) if ___: print(___) elif ___: print(___) else: print(___)
Solve it without hints to earn a ⭐ on your progress page.
Show solution🔒 Unlocks after all hints or 2 checks
Try it yourself first – mistakes teach you the most. Here is one possible solution:
age = int(input("How old are you? "))
if age < 14:
print("Child ticket: 5 euros")
elif age >= 65:
print("Senior ticket: 6 euros")
else:
print("Standard ticket: 9 euros")For fast learners
Pro task
Is year a leap year? The rule: a year is a leap year if it can be divided by 4 – unless it can be divided by 100. But if it can be divided by 400, it's a leap year after all. Print 2024 is a leap year. or 2023 is not a leap year. (with the real year).
year = 2024
# Leap year or not?Show pro solution
year = 2024
if year % 4 == 0 and (year % 100 != 0 or year % 400 == 0):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.")