We use logical operators to combine boolean expressions.
Keywords:
and : both the left and the right side of [and] must evaluate to True for the entire statement to be True
or : either one of or both the left right side of [or] must evaluate to True for the entire statement to be True
not : turns a True to False, and False to True
The most basic boolean expressions possible are True and False.
# Logical Examples
expr1 = True
expr2 = False
print('expr1 and expr2:', expr1 and expr2)
print('expr1 or expr2:', expr1 or expr2)
print('not(expr1):', not(expr1))
print('not expr2:', not expr2)
Output:
expr1 and expr2: False
expr1 or expr2: True
not(expr1): False
not expr2: True
Membership Operators
Membership operation allow us to check for the exitence of data from an iterable data type.
Keywords:
- in : Checks if the left operand exists in the right operand
- not in : Checks if the left operand does not exist in the right operand
We will look at some examples with Strings and Lists.
# Membership Example 1
letter1 = 'a'
letter2 = 'ell'
word = 'hello'
print('a in hello:', letter1 in word)
print('a not in hello:', letter1 not in word) # equivalent to print('not (a in hello)', not (letter1 in word))
print('----')
print('ell in hello:', letter2 in word)
print('ell not in hello:', letter2 not in word)
Output:
a in hello: False
a not in hello: True
----
ell in hello: True
ell not in hello: False
# Membership Example 2
fruits = ['Oranges', 'Kiwis', 'Apples', 'Watermelons']
print('Strawberries in fruits:', 'Strawberries' in fruits)
print('kiwis in fruits:', 'Kiwis' in fruits)
Output:
Strawberries in fruits: False
kiwis in fruits: True
Order of Precedence/Operations in Python 3
During assignment or boolean expression evaluation, the order of calculation follows: