4.1 conditional flow uses if, elif, else
def main(): x, y = 100, 100 # conditional flow uses if, elif, else if(x < y): st = "x is less than y" elif (x == y): st = "x is same as y" else: st = "x is greater than y" print st if __name__ == "__main__": main()
Careful with the indentation of the conditional statements.
the elif
operator is specific to Python (elseif
in Javascript.)
Note the ==
comparator. In Python this means equal to (not ===
as in Javascript )
4.2 conditional statements let you use "a if C else b"
If its just an if and else, you write it like this:
def main(): x, y = 100, 100 # conditional statements let you use "a if C else b" st = "x is less than y" if (x < y) else "x is greater than or equal to y" print st if __name__ == "__main__": main()
Thats nice and concise.