← Python for Beginners
0 of 27 lessons done0%

Conditions · Lesson 10 of 27

if, elif, else

Use if to run code only when something is true.

age = 20
if age >= 18:
    print("Adult")
else:
    print("Minor")

Two important rules:

  • The line with if ends with a colon :.
  • The code inside is indented (4 spaces). Indentation is how Python knows what is inside the if.

Use elif ("else if") to check more conditions, in order:

marks = 72
if marks >= 90:
    print("A")
elif marks >= 75:
    print("B")
elif marks >= 50:
    print("C")
else:
    print("Fail")

Comparison operators

== equal, != not equal, > greater, < less, >= greater or equal, <= less or equal.

= stores a value, == compares two values. Mixing them up is the most common beginner mistake.

Your task

Read a whole number. Print Positive if it is greater than 0, Negative if it is less than 0, and Zero if it is 0.