How Can We Help?
In this tutorial, we will explain how to use Python Operators with basic syntax and many examples for better understanding. now let’s see first the definition of operator in python down below :
What is an Operator in Python? Python Operators are special symbols in Python that carry out arithmetic or logical computation. The value that the operator operates on is called the operand.
For example:
>>> 2+3
5
Here, + is the operator that performs addition, 2 and 3 are the operands, and 5 is the operation’s output.
Python Operator Types
Python supports the following types of operators.
- Arithmetic Operators
- Comparison (Relational) Operators
- Logical Operators
- Bitwise Operators
- Assignment Operators
- Membership Operators
- Identity Operators
Python Arithmetic Operators
We use arithmetic operators to perform mathematical operations like addition, subtraction, multiplication, division, etc.
Python supports the following arithmetic operators.
1) Addition (+)
It adds two operands or unary plus.
Syntax
x + y or +1 or +2
2) Subtraction (-)
It subtracts the right operand from the left or unary minus.
Syntax
x – y or –1 or -2
3) Multiplication (*)
It multiplies two operands.
Syntax
x * y
4) Division (/)
It divides the left operand by the right one. It always returns a float value.
Syntax
x/y
5) Modulus (%)
It returns the remainder of the division of the left operand by the right.
Syntax
x%y
( or the remainder of x/y)
6) Floor Division (//)
It performs floor division calculations on operands. Floor division is a division of operands where the result is the quotient in which the digits after the decimal point are removed.
E.g., If the quotient value is 3.10 or 3.6, the result would be 3.
But if one of the operands is negative, the result is floored, i.e., rounded away from zero (which is towards negative infinity in scale as -9…..-1 …0 ….1 ……9). E.g., If the quotient value is -3.10 or -3.6, the result would be -4
Syntax
x//y
6) Exponent (**)
It performs exponential (power) calculations on operands as the left operand is raised to the power of the right operand.
Syntax
x**y (x to the power y)
Examples
x = 15
y = 4
print('x+y=', x+y) # addition
print('x-y=', x-y) # substraction
print('x*y=', x*y) # multiplication
print('x/y=', x/y) # division
print('x%y=',x%y) # modulus
print('x//y=', x//y) # floor division
print('x**y=', x**y) # exponent
Output
x+y = 19 # 15+ 4
x-y = 11 # 15-11
xy = 60 #154
x/y = 3.75 #15/4
x%y=3 #15/4 = 3 as a remainder 15/4 = 3.75 is a quotient value, take only 3 as a floor division
x//y = 3
x**y = 50625 # 15 to the power of 4
In the above example, if we use the mathematical operator with variables x and y, we will get the above result.
Python Comparison Operators
We use them to compare values. It is also called a relational operator because it decides the relation among operands. It either returns “True” or “False” according to the condition. Python supports the following comparison operators.
1) Greater than (>)
It returns “True” If the left operand is greater than the right operand.
Syntax
x > y
2) Less than (<)
It returns “True” if the left operand is less than the right.
Syntax
x < y
3) Equals (= =)
It returns “True” if both operands are equal.
Syntax
x = = y
4) Not Equal (!=)
It returns True if both operands are not equal.
Syntax
x != y
5) Grater than or Equal (>=)
It returns “True” if the left operand is greater than or equal to the right operand.
Syntax
x >= y
6) Less than or Equal (<=)
It returns “True” if the left operand is less than or equal to the right operand.
Syntax
x <= y
Examples
x = 15
y = 4
print('x > y is ', x>y) # greater than
print('x < y is ', x<y) # less than
print('x = = y is ', x==y) # equal
print('x != y is ', x!=y) # not equal
print('x >= y is ', x>=y) # grater than or equal
print('x <= y is ', x<=y) # less than or equal
Output
x > y is True # 15 is greater than 4
x < y is False # 15 is less than 4
x = = y is False # 15 is equal to 4
x != y is True # 15 is not equal to 4
x >= y is True # 15 is greater than or equal to 4
x <= y is False #15 is less than or equal to 4
In the above example, if we compare variables x and y using different operators, The result would be True or False.
Python Logical Operators
Python supports “AND“, “OR” and “NOT” logical operators. It works with conditions.
1) AND
The condition returns “True” if both the operands/expressions are true.
Syntax
x and y
AND Operator Truth Table
In the below table, A and B are conditions and status as 0=false, 1= true.
AND Operator Example
x = 5>4 # 5 is greater than 4 is True
y = 8>7 # 8 is greater than 7 is True
z= 3<2 # 3 is less than 2 is False
print("The result of x and z (x=1 and z=0): ", x and z)
print("The result of x and y (x=1 and y=1): ", x and y)
Output
The result of x and z (x=1 and z=0): False
The result of x and z (x=1 and y=1): True
In the above example, variables x, y, and z are 3 different conditions, and the output shows the result according to the above truth table of “AND“.
2) OR
The condition returns “True” if either of the operands/ expressions is true.
Syntax
x or y
OR Operator Truth Table
In the below table, A and B are conditions and status as 0=false, 1= true.
OR Operator Example
x = 5>4 # 5 is greater than 4 is True
y = 6>7 # 6 is greater than 7 is False
z= 3<2 # 3 is less than 2 is False
print("The result of x or y (x=1 or y=0): ", x or y)
print("The result of y or z (y=0 or z=0): ", y or z)
Output
The result of x or y (x=1 or y=0): True
The result of y or z (y=0 or z=0): False
In the above example, variables x, y, and z are 3 different conditions, and the output shows the result according to the above truth table of “OR“.
3) NOT
The condition returns “True” if the operand/expression is false (complements the operand)
NOT Operator Truth Table
In the below table, A and B are conditions and status as 0=false, 1= true.
Syntax
not x
In the above example, if we compare variables x and y using different operators, The result would be True or False.
NOT Operator Example
x = 5>4 # 5 is greater than 4 is True
y = 6>7 # 6 is greater than 7 is False
print("The result of not (x) : ", not (x))
print("The result of not (x and y): ", not (x and y))
Output
The result of not (x): False
The result of not (x and y): True
In the above example, variables x and y are 2 different conditions, and the output shows the inverted result, respectively.
Python Bitwise Operators
Bitwise operator acts on operands as if they are a string of binary digits and operates bit by bit.
Python supports the following bitwise operators.
In the below example, x = 10 and y = 4.
Binary value of x = 0000 1010 (10) and y = 0000 0100 (4).
1) Bitwise AND (&)
It performs bitwise logical AND. If the same bit is in both operands, it returns the same bit value. Eg. a=0,b=0 so a&b = 0, a=1, b=1 so a&b = 1
Syntax
x & y
2) Bitwise OR (|)
It performs bitwise logical OR. If the true bit exists in either operand, it returns the true bit value. Eg. a=0,b=1 so a| b = 1
Syntax
x | y
3) Bitwise NOT (~)
It performs bitwise logical NOT. It inverts the bit value of the operand.
Syntax
~x
4) Bitwise XOR (^)
It performs bitwise logical XOR. It copies the bit if it is set in one operand but not both.
Syntax
x ^ y
5) Bitwise Right Shift (>>)
It shifts the bits of the number to the right by the number of bits specified by the right operand.
Syntax
x>> 2
6) Bitwise Left Shift (<<)
It shifts the bits of the number to the left by the number of bits specified by the right operand.
Syntax
x<< 2
Examples
x=10 # binary value of 10 is 0000 1010
y=4 # binary value of 4 is 0000 0100
print("Bitwise AND (x & y) : ", x & y)
print("Bitwise OR (x | y) : ", x | y)
print("Bitwise NOT (~x) : ", ~x)
print("Bitwise XOR (x^y) : ", x^y)
print("Binary right shift (x >> 2) : ", x >> 2)
print("Binary left shift (x << y) : ", x << y)
Output
Bitwise AND (x & y) : 0 # binary value of 0 is 0000 0000
Bitwise OR (x | y) : 14 # binary value of 14 is 0000 1110
Bitwise NOT (~x) : -11 # binary value of -11 is 1111 0101
Bitwise XOR (x^y) : 14 # binary value of 14 is 0000 1110
Binary right shift (x >> 2) : 2 # binary value of 2 is 0000 0010
Binary left shift (x << 2) : 40 # binary value of 40 is 0010 1000
Eg. | Binary Format | Decimal Value |
---|---|---|
x | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 0 | 10 |
y | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 4 |
x&y | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
x>>2 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 2 |
x<<2 | 0 | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 40 |
Python Assignment Operators
Assignment operators are used in Python to assign a value to variables. “a = 5” is a sample assignment operator that assigns the value 5 on the right to the variable “a“.
Various compound operators in Python, like “a += 5” add to the variable and later assign the same. It is equivalent to a = a + 5.
1) Assign (=)
It assigns values from the right side operands to the left side operand.
Syntax
x = y
2) Add Assign (+=)
It adds the right operand to the left operand and assigns the result to the left operand.
Syntax
x += y # Equivalent to (x = x + y)
3) Subtract Assign (–=)
It subtracts the right operand from the left operand and assigns the result to the left operand.
Syntax
x –=y # Equivalent to (x = x – y)
4) Multiply Assign (*=)
It multiplies the right operand with the left operand and assigns the result to the left operand.
Syntax
x *= y # Equivalent to (x = x * y)
5) Divide Assign (/=)
It divides the left operand with the right operand and assigns the result to the left operand.
Syntax
x /= y # Equivalent to (x = x / y)
6) Modulus Assign (%=)
It takes modulus using two operands and assigns the result to the left operand.
Syntax
x %= y # Equivalent to (x = x % y)
7) Floor Assign (//=)
It performs floor division on operators and assigns value to the left operand.
Syntax
x //= y # Equivalent to (x = x // y)
8) Exponential Assign (**=)
It performs exponential (power) calculations on operators and assigns value to the left operand.
Syntax
x **= y # Equivalent to (x = x ** y)
Examples
x = 4
y = 5
z = 0
z = x + y
print ("The value of z=x+y is ", z)
z += x
print ("The value of z += x is ", z)
y -= x
print ("The value of y -= x is ", y)
z *= x
print ("The value of z *= x is ", z)
z /= x
print ("The value of z /= x is ", z)
z = 6
z %= x
print ("The value of z %= x is ", z)
z **= x
print ("The value of z **=x is ", z)
z //= x
print ("The value of z //= x is ", z)
Output
The value of z=x+y is 9
The value of z += x is 13
The value of y -= x is 1
The value of z *= x is 52
The value of z /= x is 13.0
The value of z %= x is 2
The value of z **=x is 16
The value of z //= x is 4
Python Identity Operators
“is” and “is not” are the identity operators in Python. These operators compare the memory locations of two objects.
We are using these operators to check if two variables or values are located on the same part of the memory. Two equal variables do not hint that they are identical.
1) is
It returns “True” if the operands are identical objects like x equals y.
Syntax
x is y
2) is not
It returns True if the operands are not identical or not the same object, like x is not equal to y.
Syntax
x is not y
Examples
x1=6
y1=6
x2='Python'
y2='Python'
x3=[1,2,3,4]
y3=[1,2,3,4]
print('x1 is not y1 is ',x1 is not y1)
print('x2 is y2 ',x2 is y2)
print('x3 is y3 ',x3 is y3)
Output
x1 is not y1 is False
x2 is y2 True
x3 is y3 False
In the above example, variables x1 and y1 are integers of the same values, so they are equal and identical. The same is the case with variable x2 and y2 strings. But variables x3 and y3 are listed. They are equal but not identical, and this is because the interpreter locates them separately in memory, although they are equal.
Python Membership Operators
“in” and “not in” are the membership operators in Python. We use them to test whether a value or variable is found in a given sequence (string, list, tuple, set, and dictionary).
Note: In a dictionary, we can only test for the presence of a key, not the value.
1) in
It returns “True” if the value/variable is found in the given sequence.
Syntax
x in y
2) not in
It returns “True” if the value/variable is not found in the given sequence.
Syntax
x not in y
In the above basic syntax, x is an element, and y is a sequence.
Examples
x= 'Hello python'
y= {1:'A', 2:'B', 3:'C'}
print("e in x is ",'e' in x)
print("hello not in x is ",'hello' not in x)
print("1 in y is ",1 in y)
print("A in y is ",'A' in y)
Output
H in x is True
hello not in x is True
1 in y is True
A in y is False
In the above example, ‘e’ is in x but ‘hello’ is not present in x, so the result is true for both. 1 is a key, and ‘A’ is the value in dictionary y, so the result of ” ‘A’ in y ” returns False and “1 in y” returns True.
Python Operator Precedence
In python, operator precedence affects how an expression is evaluated. Operators with the lowest precedence appear at the bottom of the table, and those with the highest appear at the top.
For example, x = 8 + 3 * 2
Here, x is assigned 48, not 22, because operator * has higher precedence than +, so it first multiplies 3*2 and then adds into 8.
Symbol | Operator |
---|---|
** | Exponent |
~ + – | Bitwise NOT, plus and minus |
* / % // | Multiply, divide, modulus, and floor division |
+ – | Addition and subtraction |
>> << | Right and left bitwise shift |
& | Bitwise AND |
^ | | Bitwise XOR and regular OR |
<= < > >= | Comparison operators |
<> == != | Equality operators |
= %= /= //= -= += *= **= | Assignment operators |
is is not | Identity operators |
in not in | Membership operators |
not or and | Logical operators |
Examples
w = 30
x = 15
y = 9
z = 4
print ("Value of (w + x) * y / z is ", (w + x) * y / z ) #( 45 * 9 ) / 4
print ("Value of ((w + x) * y) / z is ", ((w + x) * y) / z )# (45 * 9 ) / 4
print ("Value of (w + x) * (y / z) is ", (w + x) * (y / z))# (45) * (9/4)
print ("Value of w + (x * y) / z is ", w + (x * y) / z)# 30 + (135/4)
Output
Value of (w + x) * y / z is 101.25
Value of ((w + x) * y) / z is 101.25
Value of (w + x) * (y / z) is 101.25
Value of w + (x * y) / z is 63.75
The above example shows the value against the operator’s higher precedence to the lowest precedence.
In this tutorial, you have learned how to use python Operators with explaining different types of them.
If you have an inquiry or doubt don’t hesitate to leave them in the comment section. we are waiting for your feedback.But remember to understand the concept very well, you need to practice more.
Hopefully, it was clear and concise.