Operators in Python (1)
Published in
1 min readApr 3, 2024
Lesson#12
Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations.
Python has the following arithmetic operators:
Addition: +
Subtraction: -
Multiplication: *
Division: /
Modulus: %
Exponentiation: **
Floor Division: //
Examples:
x = 10
y = 3
print(x + y) # 13
print(x-y) # 7
print(x*y) # 30
print(x/y) # 3.33333
print(x%y) # 1
print(x**2) # 100
print(x//2) # 5
Bitwise operators
Bitwise operators are used to compare binary numbers. List of bitwise operators is given below:
& : AND
| : OR
^ : XOR
~ : Not
<< : Zero fill left shift
>> : Zero fill right shift
Examples:
x = 10
y = 3
print(x & y) # 2
print(x | y) # 11
print(x ^ y) # 9
print(~x) # -11
print(x >> 1) # 5
print(x << 1) # 20
Compound Assignment Operators
Compound assignment operator is a combination of assignment and arithmetic or unary operator.
x = 5
x = x + 2
# The statement x = x + 2 can be re-written using
# compound assignment operator as:
x += 2
List of compound assignment operators is as follows:
+=
-=
*=
%=
/=
//=
**=
&=
|=
^=
>> =
<< =