The Python operators can be defined as the special symbols that are used for logical and arithmetical computations i.e. they are responsible for carrying out a particular operation amongst two operands. Python Operators are regarded as the pillars of a program in a particular programming language on which the logic is built. Let us see first what are operators and operands.
Operators- These refer to the specific symbols in Python that help in carrying out the logical or arithmetic computation.
Operand- The value operated by the operators is referred to an operand.
Python offers the number of operators described in the following categories below.
- Arithmetic operators
- Comparison operators
- Assignment Operators
- Logical Operators
- Bitwise Operators
- Arithmetic operators
As suggested by the name Arithmetic operators in Python are preferably used to carry out simple mathematical calculations like addition (+), subtraction (-), multiplication (*) and division (/).
For Example:
X=15
Y=10
Output:
x – y = 5
Program:
print(‘x – y =’,x-y)
Assignment operators
Assignment operators in Python are used for assigning values to the variables. The assignment operators are inclusive of the symbols =, +=, -=, *=, /=, %=, //=, **=, &=, I=, ^=, >>=, <<=.
Example:
a= 5 is a simple assignment operator in Python assigning the value 5 to the variable a.
However, the assignment operators in Python like a += 5 first add to the variable and then later on assigns the same to it. It is equivalent to a = a+5.
- Comparison operators
As suggested by the name itself the function of the comparison operators in Python is to compare the values as per the condition to check if it’s true or false. These operators are inclusive of the symbols of less than (<), greater than (>), equal to (==), not equal to (!=), greater than or equal to (>=), smaller than or equal to (<=).
Example:
# Examples of Comparison Operators
a = 13 b = 33 # a > b is False print(a > b) # a < b is True print(a < b) # a == b is False print(a == b) # a != b is True print(a != b) # a >= b is False print(a >= b) # a <= b is True print(a <= b) |
Output:
False
True
False
True
False
True
- Logical operators
Logical operators in Python are used for joining the conditional statements. These comprise of the operators called Logical AND, Logical OR and Logical NOT.
Logical AND- True when both operands are true
Logical OR- True when either of the operands is supposed to be true
Logical NOT- True when the operand is false
Example:
# Examples of Logical Operator
a = True b = False # Print a and b is False print(a and b) # Print a or b is True print(a or b) # Print not a is False print(not a) |
Output:
False
True
False
- Bitwise operators
Bitwise operators perform the function via bit by bit operation and act upon bits. Thus, they derive their name as per the function. The bitwise operators are inclusive of Bitwise AND (&), Bitwise OR (l), Bitwise NOT (~), Bitwise XOR (^), Bitwise right shift (>>) and Bitwise left shift (<<)
Example:
# Examples of Bitwise operators
a = 10 b = 4 # Print bitwise AND operation print(a & b) # Print bitwise OR operation print(a | b) # Print bitwise NOT operation print(~a) # print bitwise XOR operation print(a ^ b) # print bitwise right shift operation print(a >> 2) # print bitwise left shift operation print(a << 2) |
Output:
0
14
-11
14
2
40
APTRON is known to be the best Python training institute in Noida as it offers excellent Python training which is job oriented training and aims to impart professional skills to the students by making them work on live Python projects. APTRON’s Python training in Noida is incorporated with an interactive session and delivers the best python course content to make you proficient in skills and knowledge along with 100 % placement support. The institute offers the best Python course in Noida at a reasonable Python course fee. Industry experts with several years of experience in the field of Python conduct APTRON’s Python training course in Noida. The classes for Python course in Noida here are scheduled during weekdays as well as weekends both during daytime as well as evening. In addition to these, the fast-track Python training course in Noida is also provided by APTRON.