Operators
Operators
These
operators are needed to perform various operations in Python such as arithmetic calculations, logical
evaluations, bitwise manipulations, etc.
What are Operators in Python?
In Python, operators are special symbols or keywords that carry out operations on values and python
variables. They serve as a basis for expressions, which are used to modify data and execute
computations. Python contains several operators, each with its unique purpose.
Types of Python Operators
Python language supports various types of operators, which are:
1. Arithmetic Operators
2. Comparison (Relational) Operators
3. Assignment Operators
4. Logical Operators
5. Bitwise Operators
6. Membership Operators
7. Identity Operators
+ Addition 10 + 20 = 30
- Subtraction 20 – 10 = 10
* Multiplication 10 * 20 = 200
/ Division 20 / 10 = 2
% Modulus 22 % 10 = 2
** Exponent 4**2 = 16
# Floor Division
print ("a // b : ", a // b)
This code defines the two variables "a" and "b." It then applies several arithmetic operations to them
(including addition, subtraction, multiplication, division, modulus, exponentiation, and floor division) and
outputs the results.
Output
a + b : 31
a - b : 11
a * b : 210
a / b : 2.1
a%b:1
a ** b : 16679880978201
a // b : 2
Python Comparison Operators
To compare two values, Python comparison operators are needed.
Based on the comparison, they produce a Boolean value (True or False).
This code compares the values of python variables 'a' and 'b' and prints if they are equal, not equal,
greater than, less than, more than or equal to, and less than or equal to each other.
Output
a == b : False
a != b : True
a > b : False
a < b : True
a >= b : False
a <= b : True
3. Python Assignment Operators
Python assignment operators are used to assign values to variables in Python.
The single equal symbol (=) is the most fundamental assignment operator.
It assigns the value on the operator's right side to the variable on the operator's left side.
= Assignment Operator a = 10
Binary Ones
~ Inverts all the bits
Complement
Binary Ones
~ Inverts all the bits
Complement
Shift left by pushing zeros in from the right and let the leftmost
<< Binary Left Shift
bits fall off
Binary Right Shift right by pushing copies of the leftmost bit in from the left,
>>
Shift and let the rightmost bits fall off
Example of Python Bitwise Operators
a = 60 # 60 = 0011 1100
b = 13 # 13 = 0000 1101
# Binary AND
c = a & b # 12 = 0000 1100
print ("a & b : ", c)
# Binary OR
c = a | b # 61 = 0011 1101
print ("a | b : ", c)
# Binary XOR
c = a ^ b # 49 = 0011 0001
print ("a ^ b : ", c)
# Binary Ones Complement
c = ~a; # -61 = 1100 0011
print ("~a : ", c)
# Binary Left Shift
c = a << 2; # 240 = 1111 0000
print ("a << 2 : ", c)
# Binary Right Shift
c = a >> 2; # 15 = 0000 1111
print ("a >> 2 : ", c)
The binary representations of the numbers 'a and b' are subjected to bitwise operations in this code. It
displays the results of binary AND, OR, XOR, Ones Complement, Left Shift, and Right Shift operations.
Output
a & b : 12
a | b : 61
a ^ b : 49
~a : -61
a >> 2 : 240
a >> 2 : 15
5. Python Logical Operators
Python logical operators are used to compose Boolean expressions and evaluate their truth
values.
They are required for the creation of conditional statements as well as for managing the flow of
execution in programs.
Python has three basic logical operators: AND, OR, and NOT.
not in Evaluates to true if it does not find a x not in y, here not in results in a 1
variable in the specified sequence and false
otherwise. if x is not a member of sequence y.
Evaluates to false if the variables on either side x is not y, there are no results
is not of the operator point to the same object and true in 1 if id(x) is not equal to
otherwise id(y).