Operators in Python are used to perform operations on values and variables in general. Standard symbols for logical and mathematical operations are represented by these symbols. We’ll look at numerous types of Python operators in this tutorial.
Types of Operator
Python language supports the following types of operators.
- Arithmetic Operators
- Comparison (Relational) Operators
- Assignment Operators
- Logical Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
Let us have a look on all operators one by one.
To conduct arithmetic operations between two operands, arithmetic operators are employed. There are operators for + (addition), – (subtraction), (multiplication), /(divide), percent (reminder), /(floor division), and exponent (*).
Consider the following table for a detailed explanation of arithmetic operators.
|+ (Addition)||It is used to add two operands. to add two operands. For example, if a = 20, b = 10 => a+b = 30|
|– (Subtraction)||Subtract right operand from the left or unary minus. For example, if a = 20, b = 10 => a – b = 10|
|/ (divide)||divides the first operand by the second. For example, if a = 20, b = 10 => a/b = 2.0|
|* (Multiplication)||It is used to multiply one operand with the other. For example, if a = 20, b = 10 => a * b = 200|
|% (reminder)||It returns the remainder when the first operand is divided by the second. For example, if a = 20, b = 10 => a%b = 0|
|** (Exponent)||It is an exponent operator represented as it calculates the first operand power to the second operand.|
|// (Floor division)||It gives the floor value of the quotient produced by dividing the two operands.|
Comparison operators are used to compare values. It returns either
False according to the condition.
|>||Greater than – True if left operand is greater than the right||x > y|
|<||Less than – True if left operand is less than the right||x < y|
|==||Equal to – True if both operands are equal||x == y|
|!=||Not equal to – True if operands are not equal||x != y|
|>=||Greater than or equal to – True if left operand is greater than or equal to the right||x >= y|
|<=||Less than or equal to – True if left operand is less than or equal to the right|
To assign values to variables in Python, assignment operators are used.
a = 10 is a simple assignment operator that assigns the value 5 on the right to the variable a on the left.
There are various compound operators in Python like
a +=10 that adds to the variable and later assigns the same. It is equivalent to
a = a + 10 .
|=||x = 10||x = 10|
|+=||x += 10||x = x + 10|
|-=||x -= 10||x = x – 10|
|*=||x *= 10||x = x * 10|
|/=||x /= 10||x = x / 10|
|%=||x %= 10||x = x % 10|
|//=||x //= 10||x = x // 10|
|**=||x **= 10||x = x ** 10|
|&=||x &= 10||x = x & 10|
||=||x |= 10||x = x | 10|
|^=||x ^= 0||x = x ^ 10|
|>>=||x >>= 10||x = x >> 10|
|<<=||x <<= 10||x = x << 10|
Bitwise operators act on bits and perform the bit-by-bit operations. These are used to operate on binary numbers.
|&||Bitwise AND||x & y|
||||Bitwise OR||x | y|
|^||Bitwise XOR||x ^ y|
|>>||Bitwise right shift||x>>|
|<<||Bitwise left shift||x<<|
Logical operators perform Logical AND, Logical OR, and Logical NOT operations. It is used to combine conditional statements.
|and||Logical AND: True if both the operands are true||x and y|
|or||Logical OR: True if either of the operands is true||x or y|
|not||Logical NOT: True if the operand is false||not x|
In Python, the membership operators are in and not in. They check whether a value or variable exists in a sequence (string, list, tuple, set, and dictionary).
In a dictionary we can only test for presence of key, not the value.
|in||True if value/variable is found in the sequence||5 in x|
|not in||True if value/variable is not found in the sequence||5 not in x|
The identity operators are used to decide whether an element certain class or type.
|is||It is evaluated to be true if the reference present at both sides point to the same object.|
|is not||It is evaluated to be true if the reference present at both sides do not point to the same object.|