Operators
Python Operators
The operator can be defined as a symbol which is responsible for a particular operation between two operands. Operators are the pillars of a program on which the logic is built in a specific programming language. Python provides a variety of operators, which are described as follows.
Arithmetic operators
Comparison operators
Assignment Operators
Logical Operators
Bitwise Operators
Membership Operators
Identity Operators
Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations between two operands. It includes + (addition), - (subtraction), *(multiplication), /(divide), %(reminder), //(floor division), and exponent (**) operators.
Comparison Operators
These operators compare the values on either side of them and decide the relation among them. They are also called Relational operators.
Assume variable a holds the value 10 and variable b holds the value 20, then-
Assignment Operators
Logical Operators
The following logical operators are supported by Python language. Assume variable a holds True and variable b holds False then-
Bitwise Operators
Bitwise operator works on bits and performs bit-by-bit operation. Assume if a = 60; and b = 13; Now in binary format they will be as follows- a = 0011 1100 b = 0000 1101
a&b = 0000 1100
a|b = 0011 1101 a^b = 0011 0001
~a = 1100 0011
Pyhton's built-in function bin() can be used to obtain binary representation of an integer number.
The following Bitwise operators are supported by Python language-
Membership Operators
Python’s membership operators test for membership in a sequence, such as strings, lists, or tuples. There are two membership operators as explained below-
Identity Operators
Identity operators compare the memory locations of two objects. There are two Identity operators as explained below:
Operator Precedence
The precedence of the operators is essential to find out since it enables us to know which operator should be evaluated first. The precedence table of the operators in Python is given below.
Last updated