if condition: # do thing1 elif condition2: # do thing2 else: # do thing3
a = 50 b = 35 if b > a: print("b is greater than a") elif a == b: print("a and b are equal") else: print("a is greater than b")
We can create boolean expressions using relational operators:
andoperator takes two arguments, and evaluates as True if both of its arguments are True. Otherwise, it evaluates to False.
oroperator also takes two arguments. It evaluates to True if either (or both) of its arguments are True, and False if both arguments are False.
We can use
except statements to check for possible errors that a user might encounter.
try: # some statement except ErrorName: # some statement finally: print("The 'try except' is finished")
First, the statement under
try will be executed. If there’s an error such as NameError or a ValueError, and
try statement will terminate and the
except statement will execute.
def divides(a,b): try: result = a / b print (result) except ZeroDivisionError: print ("Can't divide by zero!")