else

KeywordPython 2.0+Beginner

Catch-all branch when preceding if/elif conditions are all false

Quick Info

Documentation
Official Docs
Python Version
2.0+

Learn by Difficulty

Quick Example

python
x = 3
if x > 5:
    print("Greater than 5")
else:
    print("5 or less")

# else with a function
def check_password(pw):
    if len(pw) >= 8:
        return "Strong enough"
    else:
        return "Too short"

print(check_password("abc"))
print(check_password("secure123"))

else runs when the if condition is False. Every if can have at most one else.

Try in Playground

Tags

languagesyntaxcorecontrol-flowconditional

Related Items