Operator | Purpose | Example | Result |
---|---|---|---|
+ |
Addition | 2 + 3 |
5 |
- |
Subtraction | 3 - 2 |
1 |
* |
Multiplication | 8 * 12 |
96 |
/ |
Division | 100 / 7 |
14.28.. |
// |
Floor Division | 100 // 7 |
14 |
% |
Modulus/Remainder | 100 % 7 |
2 |
** |
Exponent | 5 ** 3 |
125 |
x = 10
y = 3
# Output: x + y
print('x + y =',x+y)
# Output: x - y
print('x - y =',x-y)
# Output: x * y
print('x * y =',x*y)
# Output: x / y
print('x / y =',x/y)
# Floor Division: Output: x // y
print('x // y =',x//y)
# Output: x ^ y
print('x ** y =',x**y)
# Output: x % y
print('x % y =',x%y)
x + y = 13 x - y = 7 x * y = 30 x / y = 3.3333333333333335 x // y = 3 x ** y = 1000 x % y = 1
#Assignment operators in Python
x = 4
x += 5 # <-> x = x + 5
print(x)
# x = x - 5
# x -= 5
# x = x * 5
# x *= 5
# x = x / 5
# x /= 5
# x = x % 5
# x %= 5
# x = x // 5
# x //= 5
# x = x ** 5
# x **= 5
# x = x & 5
# x &= 5
# x = x | 5
# x |= 5
# x = x ^ 5
# x ^= 5
# x = x >> 5
# x >>= 5
# x = x << 5
x <<= 5
9
#Comparison operators in Python
x = 10
y = 12
# Output: x > y
print('x > y is',x>y)
# Output: x < y
print('x < y is',x<y)
# Output: x == y
print('x == y is',x==y)
# Output: x != y
print('x != y is',x!=y)
# Output: x >= y
print('x >= y is',x>=y)
# Output: x <= y
print('x <= y is',x<=y)
a= x<=y
print(a)
x > y is False x < y is True x == y is False x != y is True x >= y is False x <= y is True True
The logical operators and
, or
and not
operate upon conditions and True
& False
values. The and
and or
operate on two conditions, whereas not
operates on a single condition.
The and
operator returns True
when both the conditions evaluate to True
. Otherwise, it returns False
.
a |
b |
a and b |
---|---|---|
True |
True |
True |
True |
False |
False |
False |
True |
False |
False |
False |
False |
or
operator returns True
if at least one of the conditions evaluates to True
. It returns False
only if both conditions are False
.a |
b |
a or b |
---|---|---|
True |
True |
True |
True |
False |
True |
False |
True |
True |
False |
False |
False |
not
operator returns False
if a condition is True
and True
if the condition is False
.x = True
y = False
print('x and y is',x and y)
print('x or y is',x or y)
print('not x is',not x)
Logical operators can be combined to form complex conditions. Use round brackets or parentheses (
and )
to indicate the order in which logical operators should be applied.
numb = 3
(2 > 3 and 4 <= 5) or not (numb < 0 and True)
x >= 2 and (x/y) > 2
, it evaluates the expression from left to right.# A short circuit happens in 'and' operation, when the first condition evaluates to False
x = 3
y = 0
z = ((x>=6) and (x/y))
z
x = 8
y = 0
z = ((x>=6) and (x/y))
z
--------------------------------------------------------------------------- ZeroDivisionError Traceback (most recent call last) /var/folders/1t/g3ylw8h50cjdqmk5d6jh1qmm0000gn/T/ipykernel_36190/1253562860.py in <module> 1 x = 8 2 y = 0 ----> 3 z = ((x>=6) and (x/y)) 4 z ZeroDivisionError: division by zero
# A short circuit happens in 'or' operation, when the first condition evaluates to True
x = 7
y = 0
z = ((x>=6) or (x/y))
z
True
# Now short circuit will not happen
x = 3
y = 0
z = ((x<=6) and (x/y))
z
# to overcome the above scenario use guard evaluation
x = 3
y = 0
z = ((x <= 6) and (y != 0) and (x/y))
z
a = -5
b = a >> 1
b
#Bitwise operators in Python
x = 10 # 00001010
y = 4 # 00000100
# Bitwise and
print('x & y is',x&y)
# Bitwise or
print('x | y is',x|y)
# Bitwise not
print('~x is',~x)
# Bitwise XOR
print('x^y is',x^y)
# Bitwise right shift
print('x>>3 is',x>>3)
# Bitwise left shift
print('x<<3 is',x<<3)
x & y is 0 x | y is 14 ~x is -11 x^y is 14 x>>3 is 1 x<<3 is 80
#Bitwise operators in Python
x = -10
y = 4
print(~x)
print(x^y)
print(x>>3)
9 -14 -2
is
and is not
#Identity operators in Python
a = 5
b = 5.0
print(a is b)
print(a==b)
a = 'Hello'
b = 'Hello'
# Output: False
print(a is not b)
# Output: True
print(a is b)
in
and not in
a = 10
b = 4
list = [1, 2, 3, 4, 5 ]
rv = a in list
print(rv)
rv = b in list
print(rv)
-left-associative (means the operations are grouped from the left) -right-associative (Exponent operator ** has right-to-left associativity in Python)
- may be associative (means the operations can be grouped arbitrarily)
- non-associative (meaning operations cannot be chained, often because the output type
# Run interactive help and type "OPERATORS" to get information about precedence
help('OPERATORS')
print(5 + 3 * 2)
print((5 + 3) * 2)
print(2 ** 3 ** 2)
print((2 ** 3) ** 2)
512 64
num1, num2, num3 = 2, 3, 4
print ((num1 + num2) * num3)
num1, num2, num3 = 2, 3, 4
print (num1 ** num2 + num3)
12
num1, num2 = 15, 3
print (~num1 + num2)
-13
Try answering the following questions to test your understanding of the topics covered in this notebook:
/
and the //
operators?*
and the **
operators?=
and ==
in Python?and
and or
operators?