<, >, <=, >=, ==
True or False is returned.
>>> 3 < 8 True >>> 3 >= 4 False >>> angle = math.radians(60); x, y = math.cos(angle), math.sin(angle) >>> x < y True >>> x**2 > y**2 False
True and False are literal values of type boolean; the corresponding Python function is bool.
>>> False False >>> type(False) <type 'bool'> >>> bool(0) False >>> bool(5) True
== operator asks whether its two operands are equal.
Be careful to distinguish it from the assignment operator, =.
Note that == can be used with values other than numbers.
>>> x, y = 3, 4 >>> x == y False >>> x = y >>> x == y True >>> 'a string' == 'A STRING' False >>> [1, 2, 3] == [1, 2, 3] True
is, which is stricter than ==.
(What this means will be clear later on.)
!= operator means "is not equal to?".
The stricter version of inequality is is not.
>>> isinstance(3, float) False
def are_same_type(obj1, obj2):
"""Are obj1 and obj2 of the same type?"""
return type(obj1) == type(obj2)
>>> are_same_type(3, 6.0)
False
>>> are_same_type('abc', '123')
True
and, or, and not return booleans, behaving like you would expect them to from formal logic.
>>> x, y = -2, 2 >>> x**2 == y**2 and x**3 == y**3 False >>> x**2 == y**2 or x**3 == y**3 True >>> not x**3 > y**3 True
in and not in.
These test whether the left-hand element is (or is not) a member of the right-hand element.
This works with strings as both operands, for example.
It also works with lists.
>>> vowels = 'aeiou' >>> 'd' in vowels False >>> 'd' not in vowels True >>> 3 in [1, 2, 3, 4] True >>> vowels1 = ['a', 'e', 'i', 'o', 'u'] >>> vowels2 = ['ai','ay', 'au', 'aw', 'ea', 'ee', 'ei', 'ey','ie', 'oa', 'ou', 'ow', 'oi', 'oo', 'oy'] >>> string = 'uw' >>> string in vowels1 or string in vowels2 False
if condition:
statement
True.
The condition must be followed by a colon, and the statement must be indented
relative to the if line.
def print_same_type(obj1, obj2):
'''Print out the type of obj1 and obj2 if it's the same.'''
if are_same_type(obj1, obj2):
print(type(obj1))
>>> print_same_type(3, 5)
>>> print_same_type(3, 'three')
>>> print_same_type(are_same_type, print_same_type)
<class 'function'>
if together
with else:
if condition:
statement1
else:
statement2
if/else expressions with the
keyword elif ("else if"):
if condition1:
statement1
elif condition2:
statement2
elif condition3:
statement3
...
else:
statement4
def print_rel_value(x, lower, higher):
'''Print out whether x is between, below, or above the other two args.'''
if lower <= x <= higher:
print(x, 'is between', lower, 'and', higher)
elif x < lower:
print(x, 'is below', lower)
else:
print(x, 'is above', higher)
if
expression, we can replace it with a block statement,
which includes multiple statements which are to be executed in sequence.
if condition:
statement1
statement2
statement3
elif:
...
def smaller(x, y):
'''Return the smaller of two numbers or strings and print out a message.'''
if x < y:
print('First is smaller:', x)
return x
else:
print('Second is smaller:', y)
return y
return is (are) immediatedly returned; any statements that follow the return statement are not executed.
def rel_value_string1(x, lower, higher):
'''Return a string specifying whether x is between, below, or above the other two args.'''
if lower <= x <= higher:
return 'between'
elif x < lower:
return 'below'
else:
return 'above'
def rel_value_string2(x, lower, higher):
'''Return a string specifying whether x is between, below, or above the other two args.'''
if lower <= x <= higher:
return 'between'
if x < lower:
return 'below'
return 'above'
if expression,
the empty string (''), the empty list ([]) and the number zero (0 or 0.0) are all treated as False.
>>> x, y = -1, 1
>>> if x + y:
print('Something non-zero')
Answers to problems in these notes