Lesson 8 of 34

if / else: Programs that Decide

8 min 25 XP

if means “if this is true, do this”. The if line ends with a : (colon), and the lines under it are indented by 4 spaces.

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

else means “otherwise”. When the condition is False, the else part runs. Change marks to 20 and press Run.

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

You can decide based on what the user types. Put a different age in the Input box and press Run.

Example: edit me and press Run

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.

Python uses indentation to know which lines belong to the if. Use the Tab key.

Challenge

+25 XP

Read a number with input(). Print Even if it is even, otherwise print Odd.

Output Loading Python (first time takes a few seconds)
Press Run to see the result here.