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	# 5
print b and a	# 1
print a and False	# False
print a and True	# True
print a and None	# None
print False and a	# False
print None and a	# None
print True and 'a' and 0 and True # first False item is zero, 0

Example 2: operators in python

# Python Operators
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"}

>>> # OR, | 
>>> s1 | s2
{'a', 'b', 'c', 'd', 'e', 'f'}
>>> s1                                                     # `s1` is unchanged
{'a', 'b', 'c'}

>>> # In-place OR, |=
>>> s1 |= s2
>>> s1                                                     # `s1` is reassigned
{'a', 'b', 'c', 'd', 'e', 'f'}

Example 4: ** in python

#** is the exponent symbol in Python, so:
print(2 ** 3)
#output: 8

Example 5: python math operators

# Below follow the math operators that can be used in python
# ** Exponent	
2 ** 3 # Output: 8
# % Modulus/Remaider	
22 % 8 # Output: 6
# // Integer division	
22 // 8 # Output: 2
# / Division	
22 / 8 # Output: 2.75
# * Multiplication	
3 * 3 # Output: 9
# - Subtraction	
5 - 2 # Output: 3
# + Addition	
2 + 2 # Output: 4

Example 6: or in python

# Syntax for Boolean expression with or in Python
exp1 or exp2