9.11.6. If and else statements#
9.11.6.1. if Statements#
Perhaps the most well-known statement type is the if statement. For example:
1x = int(input("Please enter an integer: "))
2
3if x < 0:
4 print('Negative')
5elif x == 0:
6 print('Zero')
7else:
8 print('Positive')
There can be zero or more elif parts, and the else part is optional. The keyword ‘elif’ is short for ‘else if’, and is useful to avoid excessive indentation. An if … elif … elif … sequence is a substitute for the switch
or case
statements found in other languages.