This tutorial will learn about all the different types of python operators and their syntax and use them in real-time with examples.
First, let’s understand what exactly Operators are.
What are Python Operators?
Python Operators are special symbols used to perform mathematical and logical operations and computations on values or variables.
Values used by python operators are called the operand
Types of Python Operators
Python programming language supports seven types of operators.
- Arithmetic Operators
- Comparison (Relational) Operators
- Assignment Operators
- Logical Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
Let us study all the operators one by one.
You can also see the Python Type Casting and Type Conversion.
Python Arithmetic Operators
Arithmetic operators are used to doing mathematical calculations on values and variables.
|+ Addition||An addition Operator is used to make the addition of two or more variables or values.||X + Y = 2
1 + 1 = 2
|– Subtraction||Subtraction operator is used to subtracting two or more variables or values.||Y – X = 1
3 – 1 = 1
|* Multiplication||Multiplication operator is used to multiplying two or more variables or values.||X * Y = 4
2 * 2 = 4
|/ Division||Division operator is used to divide two or more variables or values.||X / Y = 2
4 / 2 = 2
|% Modulus||The modulus operator is used to find the remainder of two or more variables or values.||X % Y = 1
10 % 3 = 1
|** Exponent||The exponent operator is used to find the power of the two or more variables or values.||X ** Y = 64
2 ** 6 = 64
|// Floor Division||The Floor Division is used to divide the first operand by the second.||X // Y = 3
10 // 3 = 3
Now let’s take an example of all the arithmetic operators in one program.
As we can see in the above example how we can use Arithmetic Operators on multiple variables and values
Python Comparison or Relational Operators
Python Comparison or Relational operators are used to compare the value or variable. It will return True Or False as output based on the condition.
|>||Greater Then: operator will return True when the left operand is greater than the right.||X > Y
2 > 3
|<||Less Then: operator return True when left operand is less than the right.||X < Y
3 < 2
|==||Equal To: will return True when both operands are equal.||X == Y
3 == 3
|!=||Not Equal To: operator will return True when two operands are not equal.||X != Y
3 != 2
|>=||Greater than or Equal To operator will return True when left operand if greater than right or equal to the right||X >= Y
2 >= 3
|<=||Less Then Equal To Operator will return True when the left operand is less than or equal to the right operand.||X <= Y
2 <= 3
Now let take the example of Relational Operators.
Python Assignment Operators are used to assign values to variables:
|=||X = 5||X = 5|
|+=||X += 5||X = X + 5|
|-=||X -= 5||X = X – 5|
|*=||X *= 5||X = X * 5|
|/=||X /= 5||X = X / 5|
|%=||X %= 5||X = X % 5|
|//=||X //= 5||X = X // 5|
|**=||X **= 5||X = ** 5|
|&=||X &= 5||X = & 5|
||=||X |= 5||X = X | 5|
|^=||X ^= 5||X = X ^ 5|
|>>=||X >>= 5||X = X >> 3|
|<<=||X <<= 5||X = X << 5|
Python Logical Operators
Python logical operators are used for the conditional statement.
|AND||Return True when both statements are true||X < 5 AND X < 10|
|OR||Return True when one of the statement is true||X < 5 or X < 5|
|NOT||Return False if the result is not true||not(X < 5 or X < 10)|
Python Bitwise Operators
Same As Logical operators are used to comparing data, and it uses special symbols, but bitwise operators act on operands as binary digits. They operate bit by bit.
For Example, 2 is 10 in binary form, and 111 is for 7.
|&||Bitwise AND Operator: Return True when both statements are true||X < 5 & X < 10|
||||Bitwise OR Operator: Return True when one of the statement is true||X < 5 | X < 5|
|~||Bitwise NOT Operator: Return False if the result is not true||~X|
|^||Bitwise XOR operators: work with binary values and return one if one of the bits is 0 and the other bit is one, and if both the bits are 0 or 1, then it returns 0.||X ^ Y|
|<<||Bitwise Left Shift: Shift left by pushing zeros in from the right and let the leftmost bits fall off.||X << Y|
|>>||Bitwise Right Shift: Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off||X >> Y|
Bitwise operators will return Boolean if we use Bitwise operator with other operators, and it will return numeric values if we only use a single bitwise operator. Let take an example of each.
Let’s take X as 3, and binary will be 0011, and Y as 8 as 1000.
Python Membership Operators
Python Membership operators are used to test if a sequence is present in an object or not.
For example, if a value is present in a list, set, and other objects.
|in||Returns True if a specified value is present in the sequential object||X in Y|
|not in||Return True if a specified value is not present in the sequential object.||X not in Y|
Let see an example of a Python membership operator.
Python Identity Operators
Python identity operators are used to compare the objects, not if they are equal, but if they are the same object, with the same memory location.
|is||Returns True if both variables are the same object||X is Y|
|is not||Returns True if both variables are not the same object||X is Not Y|