Operators in Python: Like other programming languages python also implements the concepts of operators. Operators are special symbols that represent operations like addition, multiplication etc.
Operand: The values required by the operator to operate is called operand.
Following are some valid expressions:
32+20hour-1hour*60+minutes
When the variable name appears at the place of an operand, it is replaced with its (variable) value. Like as you see above three examples are there. The first one is very clear ie. 32+20 but the second one little bit confusing because it’s showing hour-1. It does not return any meaning here, what does hour indicate?. I will let you know that here hour is a variable whose value can be 2,3,4 or anything. It is more clear in the example below:
Example 1:
# Operator and operand example
minute = 50
result = minute/2
print(result)
# output
25
Detailed Explanation:
Here variable minute and 2 is operands and ‘/’ is the operator.
We have initialized variable minute with the value 50. so wherever we will put variable minute, it will act as 50.
The result variable will store the value 25 after the operation ‘minute/2’ hence, the output is returned.
Priority of Operations: In most cases, sometimes more than one operator appears in an expression, In that case, the order of operator evaluation depends upon rules of Precedence. Python follows the same precedence rules for its mathematical operators that mathematics does. We follow the PEMDAS acronym to remember that order of operations.
P: Parenthesis have the highest precedence. E: Exponent has the next highest precedence. M: Multiplication and division have the same precedence. S: Precedence of subtraction and addition is next to the multiplication and division.
Example 2:
# Example on Priority of Operators
print(2**1+1)
print(3*1**3)
# output
3
3
Types of Operators in Python
Python Arithmetic Operators
Arithmetic operations in Python are used to perform mathematical operations. Some of the mathematical operations are addition, subtraction and multiplication etc. We can combine these operators to perform more complex operations as well.
Operator
Name
Example
Output
+
Addition
1+1
2
–
Subtraction
3-2
1
*
Multiplication
2*2
4
/
Division (returns quotient)
10/2
5
%
Modulus (returns remainder)
4%3
1
**
Exponentiation
2**3
8
//
Floor Division
5//2
2
Example 1: Arithmetic Operators in Python
a = 20
b = 10
# Addition
print("Addition of a and b is: ",a+b)
# Subtraction
print("Subtraction of a and b is: ",a-b)
# Multiplication
print("Multiplication of a and b is: ",a*b)
# Division
print("Division of a and b is: ",a/b)
# Modulus ( Modulus returns remainder after divison )
print("Modulus of a and b is: ",a%b)
# Exponentiation
print("Exponentiation of a and b is: ",a**b)
# Floor Devision ( it skips the value of answer after decimal )
print("Floor Division of a and b is: ",a//b)
# output
# Addition of a and b is: 30
# Subtraction of a and b is: 10
# Multiplication of a and b is: 200
# Division of a and b is: 2.0
# Modulus of a and b is: 0
# Exponentiation is: 1024000
# Floor Division of a and b is: 2
Explanation:
Addition: It is used to add two or more values same as that in mathematics.
Subtraction: It is used to subtract two values same as that in mathematics.
Multiplication: It is used to multiply or product two or more values same as that follows in mathematics.
Division: The purpose of using division is also the same as that rule follows in mathematics. It returns the quotient after division.
Modulus: This is new concerning the name. This operator is the same as that of division but returns the remainder as a result after division.
Exponentiation: The exponent (**) operator in Python is used to raise the number on the left to the power of the exponent of the right. Let’s assume the above-given exponentiation example ie. 2**3, it means 23 (2x2x2) that results in 8.
Floor division: Floor division is the same as that of the normal division as it also returns a quotient but terminates or remove the digits after the decimal. Suppose if we perform division operation 5/3 then the answer is 1.67. But in terms of floor division, the value after the decimal is removed ie. the answer is 1.
Assignment Operators in Python
Assignment operations in Python are mainly used to assign some values to variables. A very simple assignment operation is x = 10 that assigns value 10 to variable x.
There are many assignment operators in Python i.e x *= 5. This operation is same as x = x*5
Operator
Example
Same As
=
y = 5
y = 5
+=
y += 3
y = y + 3
-=
y -= 3
y = y – 3
*=
y *= 3
y = y * 3
/=
y /= 3
y = y / 3
%=
y %= 3
y = y % 3
//=
y //= 3
y = y // 3
**=
y **= 3
y = y ** 3
&=
y &= 3
y = y & 3
| =
y |= 3
y = y | 3
^=
y ^= 3
y = y ^ 3
>>=
y >>= 3
y = y >> 3
<<=
y <<= 3
y = y << 3
Example1
a = 20
print("value of a is: ", a)
# output
# value of a is: 20
a = a - 10
print("value of a is: ", a)
# output
# value of a is: 10
a = a * 10
print("value of a is: ", a)
# output
# value of a is: 100
Explanation:
In the first operation, we have just assigned value 20 to variable a.
In the second operation, we subtracted value 10 from variable a. Hence 10 is the output.
Note: Each time the operation is performed on the variable ‘a’ it is getting updated. Now the value of a is 10, not 20. So, if we will perform any operation on a. It is to be performed by assuming the latest value of a that is 10.
In the last operation, we multiplied variable ‘a’ with 10. The value of ‘a’ before was 10 so after multiplied by 10 it became 100. Hence the output 100 is printed.
Comparison Operators in Python
Comparison operators are used to compare two values. If returns the result as True or False. Suppose we have two variables a and b. If both values of both variables are equal to each other then it returns the result as True and if the values of both differ from each other then it returns False.
Operator
Name
Example
==
Double Equal
x == y
!=
Not Equal
x != y
>
Greater than
x > y
<
Less than
x < y
>=
Greater than or equal to
x <= y
<=
Less than or equal to
x <= y
Example 1
>>> a = 10
>>> b = 20
>>> a == b
False
>>> b = 10
>>> a == b
True
Explanation:
We assigned 10 and 20 as value to variable a and b respectively.
Next, we implemented the use of == (comparison operator) that checks the value of both variables is equal or not.
The value of a differs from b. Hence the result obtained is False
Now in the next step, we override the value of b with 10. Now the value of b is updated to 10 and the value of a is already 10.
We repeated the operation. And now this time the result is True
Logical Operators in Python
Logical operators in Python are used in combining conditional statements. It also returns True or False based on conditions. It works on the principle of Logic gates i.e. and, or, not. Find the list of available logical operators in the below table.
Operator
Output
Syntax
and
Returns True if both statements are true
x < 5 and x < 10
or
Returns True if one of the statement is true
x < 5 or x < 4
not
Reverse the result, returns False if the result is true
not(x < 5 and x < 10)
Example 1:
a = False
b = True
print(a and b)
print(a or b)
print(not b)
# output
# False
# True
# False
Explanation:
Note: Logical operators follows the basic concept of Logic gates.
and logical operator is used to check between values. It may be two or more than two. Minimum two values or condition is required to implement and logical operation. and operator returns True only if all the condition is True else, it returns false. I know it is confusing let me show you with the help of one more example.
>>> 5 > 4 and 6 > 5
True
>>> 5 > 4 and 5 == 5 and 3 < 6
True
>>> 5 < 4 and 5 == 5 and 3 < 6
False
or logical operator is also used to check between values or conditions. It may be two or more than two. Here also a minimum of two value or conditions is required for implementation. or logical operator returns True if any of the condition returns True. Please refer to the below example for a better understanding.
>>> 5 > 4 or 5 > 4
True
>>> 3 < 2 or 1 > 2
False
>>> 4 == 4 or 1 > 2 or 1 == 0
True
not logical operator reverses the actual value or conditions i.e. if we use not logic operator on True condition or value it will convert it to False. example below:
>>> not True
False
>>> not False
True
Identity Operators in Python
Identity operators in Python are used to compare the objects. is and is not are the identical operators in Python. It does not check the equality but it checks the same object with the same memory allocation. Two equal values do not mean that they are identical. It returns True or False as an output.
Operator
Description
Example
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
Example 1:
a = "PythonLobby"
b = "PythonLobby"
print(a is b)
# memory location
print("\n",id(a))
print(id(b))
# output
# True
# 56086304
# 56086304
Explanation:
Here we have just assigned the same values to variable a and b. And then used the identity operator to check the condition. But here identity operator is not checking the equal values. It checks for the object of the variable at the same memory location although their values are same. You can also see that the memory location of a and b is same.
Example 2:
a = [1,2,3,4,5]
b = [1,2,3,4,5]
print(a is b)
# memory alocation
print("\n",id(a))
print(id(b))
# output
# False
# 55964200
# 10543240
Explanation:
List in Python always allocated to different memory locations. Variable a and b holding the values of a list type. Hence output displayed is False. Because the memory location of a and b is different.
You can see, the memory location of variable a and b in the above example 2.
is not is the vice versa of is identity operator.
>>> a = "PythonLobby"
>>> b = "PythonLobby"
>>> a is not b
False
>>> c = [1,2,3,4,5]
>>> d = [1,2,3,4,5]
>>> c is not d
True
Python Membership Operators
Python membership operators are used to check whether the value or sequence of values are present in an object or not. i.e. (string, list, tuple, set and dictionary). It returns the result in boolean value i.e. True or False. in and not in are the membership operators in Python.
Operaor
Description
Example
in
Returns True if a sequence with the specified value is present in the object
x in y
not in
Returns True if a sequence with the specified value is not present in the object
x not in y
Example 1:
a = 'Hello Python Programmer'
b = [2,3,4,5]
print('P' in a)
# Output: True
print('pro' not in a)
# Output: True
print(6 in b)
# Output: False
print('a' in b)
# Output: False
Explanation:
Membership operators are used to check whether the value or sequence exists in the object or not.
Here we have initialized two variables a and b with string values and list type values respectively.
In the first step, we checked whether the letter P exists in the string or not. As we can see, the letter P exists in the string hence result returned is True.
In the second step, we checked that the word pro should not exist in the string i.e true hence the result returned is True. (Pro and pro is not identical i.e. case sensitive).
Next, we checked for whether the number 6 exists in the list or not. The number 6 does not exist in the list hence the result returned is False.
At final, letter a also not exist in the list so the result again is False.
Python Bitwise Operators
Bitwise operators in python are mainly used to compare Binary numbers.
Operator
Name
Description
&
AND
Sets each bit to 1 if both bits are 1
|
OR
Sets each bit to 1 if one of two bits is 1
^
XOR
Sets each bit to 1 if only one of two bits is 1
~
NOT
Inverts all the bits
<<
Zero fill left shift
Shift left by pushing zeros in from the right and let the
leftmost bits fall off
>>
Signed right shift
Shift right by pushing copies of the leftmost bit in from