python and operator code example
Example 1: python and operator returns
'''
Now the interesting part.
The and and or operators actually return values!
With the and operator, each argument is evaluated, and if they all evaluate to True,
the last argument is returned. Otherwise the first False argument is returned.
'''
a = 1
b = 5
print a and b
print b and a
print a and False
print a and True
print a and None
print False and a
print None and a
print True and 'a' and 0 and True
Example 2: operators in python
Operator Name Example
+ Addition x + y
- Subtraction x - y
* Multiplication x * y
/ Division x / y
% Modulus x % y
** Exponentiation x ** y
// Floor division x // y
Example 3: |= operator python
>>> s1 = {"a", "b", "c"}
>>> s2 = {"d", "e", "f"}
>>>
>>> s1 | s2
{'a', 'b', 'c', 'd', 'e', 'f'}
>>> s1
{'a', 'b', 'c'}
>>>
>>> s1 |= s2
>>> s1
{'a', 'b', 'c', 'd', 'e', 'f'}
Example 4: ** in python
print(2 ** 3)
Example 5: python math operators
2 ** 3
22 % 8
22 // 8
22 / 8
3 * 3
5 - 2
2 + 2
Example 6: or in python
exp1 or exp2