You can use the else statement to specify code to be executed if none of the conditions specified
by the if and elif statements are true:
patientid = 42446
if patientid == 42113:
print"The patient ID is 42113"elif patientid == 42007:
print"The patient ID is 42007"else:
print"The patient ID is some other number"
Here, Python checks whether patientid is 42113, then checks whether patientid is 42007. If neither of these is true, the
statements after the else are executed.
The statements included with the else condition must be indented, and the else statement must
appear after the if statement and after any elif statements.
You do not have to supply elif statements to use else. The following is perfectly legal:
patientid = 42446
if patientid == 42113:
print"The patient ID is 42113"else:
print"The patient ID is not 42113"