--Originally published at Programming Fundaments
An “else” conditional is dependent of an “if” statment, it usually executes if the “if” statement goes to 0 or is false.
#!/usr/bin/python var1 = 100 if var1: print "1 - Got a true expression value" print var1 else: print "1 - Got a false expression value" print var1 var2 = 0 if var2: print "2 - Got a true expression value" print var2 else: print "2 - Got a false expression value" print var2 print "Good bye!"
Resulting in this:
1 - Got a true expression value 100 2 - Got a false expression value 0 Good bye!
The “elif” conditional allows you to check multiple times for a “true” statement inside your block of code and execute one as soon as one turn “true” Example:
var = 100 if var == 200: print "1 - Got a true expression value" print var elif var == 150: print "2 - Got a true expression value" print var elif var == 100: print "3 - Got a true expression value" print var else: print "4 - Got a false expression value" print var print "Good bye!"
Examples and more from: https://www.tutorialspoint.com/python/python_if_else.htm