Python Operators - Ryzen Hunt
Operators in Python
Operators are symbols, such as +, –, =, >, and <, that perform certain mathematical or logical operations to manipulate data values and produce a result based on some rules. An operator manipulates the data values called operands. Python language supports a wide range of operators. They are :
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
1. Arithmetic Operators
Arithmetic operators are used to execute arithmetic operations such as addition, subtraction, division, multiplication, etc. All the arithmetic operators are shown below.

For example,
>>> 10+35
45
>>> −10+35
25
>>> 4*2
8
>>> 4**2
16
>>> 45/10
4.5
>>> 45//10.0
4.0
>>> 2025%10
5
>>> 2025//10
202
2. Assignment operators
Assignment operators are used for assigning the values generated after evaluating the right operand to the left operand. Assignment operation always works from right to left. All the assignment operators are shown below.

For example,
>>> p = 10
>>> q = 12
>>> q += p
>>> q
22
>>> q *= p
>>> q
220
>>> q /= p
>>> q
22.0
>>> q %= p
>>> q
2.0
>>> q **= p
>>> q
1024.0
>>> q //= p
>>> q
102.0
3. Comparison Operators
When the values of two operands are to be compared then comparison operators are used. The output of these comparison operators is always a Boolean value, either True or False. All the comparison operators are shown below.

For example,
>>>10 == 12
False
>>>10 != 12
True
>>>10 < 12
True
>>>10 > 12
False
>>>10 <= 12
True
>>>10 >= 12
False
>>> "P" < "Q"
True
>>> "Aston" > "Asher"
True
>>> True == True
True
4. Logical Operators
The logical operators are used for comparing or negating the logical values of their operands and to return the resulting logical value. The values of the operands on which the logical operators operate evaluate to either True or False. The result of the logical operator is always a Boolean value, True or False. All the logical operators are shown below.

The Boolean logic Truth table is given below.

For example,
>>> True and False
False
>>> True or False
True
>>> not(True) and False
False
>>> not(True and False)
True
>>> (10 < 0) and (10 > 2)
False
>>> (10 < 0) or (10 > 2)
True
>>> not(10 < 0) or (10 > 2)
True
>>> not(10 < 0 or 10 > 2)
False
5. Bitwise Operators
Bitwise operators treat their operands as a sequence of bits (zeroes and ones) and perform bit by bit operation. For example, the decimal number ten has a binary representation of 1010. Bitwise operators perform their operations on such binary representations, but they return standard Python numerical values. All the bitwise operators are shown below.

The Bitwise Truth table is shown below.

For example,
>>> p =60
>>> p << 2
240
>>> p = 60
>>> p >> 2
15
>>> q = 13
>>> p & q
12
>>> p | q
61
>>> ~p
–61
>>> p << 2
240
>>> p >> 2
15
There are two more operators named :
-
Identity Operators (is, is not)
-
Membership Operators (in, in not)
Precedence and Associativity
Operator precedence determines the way in which operators are parsed with respect to each other. Operators with higher precedence become the operands of operators with lower precedence. Associativity determines the way in which operators of the same precedence are parsed. Almost all the operators have left-to-right associativity. Operator precedence is listed in TABLE 2.9 starting with the highest precedence to lowest precedence.

Hope you learned something new here. Please share this with your friends and Don't forget to share your comments and valuable suggestions and feedback.
For visiting our Python Course series click here, and for HTML course series click here.
Suggested Product:
Arduino Uno Board with USB cable with length of 1 foot. This board can be used in any of the projects related to Robotics or IOT easily. Click Here or on the given image to purchase it at a special price.