Home » Learn Python » Conditions » If … Elif … Else

If … Elif … Else

The most general form off the Python if statement can check multiple conditions.

The first condition to check comes after the if keyword, then we can check one or more further conditions using the elif keyword (short for “else if”).

Python will work down the conditions until it finds one that’s true. If none of the conditions are true, and if there is an else block, it will execute the else block.

It’s important to realise that only one of these code blocks will execute: the first one from the top that is true, or the else block (if present) when none of them are true.

name = input("Enter your name: ")

if name == 'Bob':
    print("Hello Bob!")
elif name == "Clare":
    print("Hi Clare!")
    print("You're not Bob or Clare!")
Enter your name: Bob
Hello Bob!
Enter your name: Clare
Hi Clare!
Enter your name: Dave
You're not Bob or Clare!

Leave a Reply

Blog at WordPress.com.