Preview lessons, content and tests

Computer Science & Programming solved. All in one platform.

1. To trial the platform and take tests, please take a few seconds to SIGN UP and SET UP FREE.
2. Searching for something specific? See our text overview of all tests. Scroll right for levels, and lists.

Join 36000+ teachers and students using TTIO.

IF -ELSE Statements

It helps to think of the three of them like this:

  • An If statement gives the computer one option: if  is True, then do something. That’s all.
  • An If-Else statement gives the computer two options: if  is True, then do something. If  if False, do some other thing!
  • An If-Elif-Else statement gives the computer several options, where you can say “Check all of these conditions until you find one that’s True.”

 

Suggested Video

Each kind of statement is indented in the same way - with 4 spaces. Here’s an example of each:

If Statement:

if x == 5:
        print("x is 5!")

If-Else Statement:

if x == "Penny":
        print("Your name is Penny!")
else:
        print("Looks like your name isn't Penny!")

If-Elif-Else Statement:

if age == 50:
        print("You're really old!")
elif age == 20:
        print("You're kind of young!")
elif age == 10:
        print("You're a kid!")
else:
        print("I wonder how old you are?")

www.teachyourselfpython.com