Python has many operators which can perform specific operations. These operators can manipulate numbers or check conditions.
And the best part, they are easy to understand and implement.
Types of Operator:
Python language supports the following types of operators.
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
- Membership Operators
- Bitwise Operators
1. Arithmetic Operators:
These operators are meant for number data types only. There perform the basic arithmetic operations:
- + for addition
- – for subtraction
- * for multiplication
- / for division
Python also has 3 arithmetic operators that are special. We will discuss them.
a. % (Modulus operator) :
This operator gives the remainder of the division. And if there is no remainder it returns zero.
For example: 22 % 7 = 1, 21 % 7 = 0
b. ** (Exponent operator) :
This operator performs exponential (power) calculation on numbers. The first number is raised to the power of the second number.
For example: 2**3 = 8
c. // (Floor Division) :
This operator performs division but truncates off decimal part.
For example: 44 // 7 = 6
# Let's declare variable variable1 with value 200, variable2 with value 30 | |
variable1 = 200 | |
variable2 = 3 | |
print("value of variable1 is", variable1) | |
print("value of variable2 is", variable2) | |
# Performing modulus | |
modulusOutput = variable1 % variable2 | |
print("Value of modulus is", modulusOutput) | |
# output: Value of modulus is 2 | |
# Performing Exponent function | |
exponentOutput = variable1 ** variable2 | |
print("Value of exponent is", exponentOutput) | |
#output: Value of exponent is 8000000 | |
# Performing floor division | |
floorDivisionOutput = variable1 // variable2 | |
print("Value of floor division is", floorDivisionOutput) | |
# output: Value of floor division is 66 |
2. Relational Operators:
These Operators used for comparing values. If the condition of comparison holds correct it gives True as output otherwise False.
There are some basic relational operators:
- < less than
- > greater than
- <= less than or equal to
- >= greater than or equal to
There are 2 special relational operators which are going to discuss.
a. == (Equal comparison):
Compares the 2 values and returns True if both values are equal.
For example: 12 == 13 will give False
b. != (Not Equal to) :
Compares the 2 values and returns True if both values are not equal.
For example: 12 != 13 will give True
# lets declare a variable, variable1 with value 100 | |
variable1 = 100 | |
print("Value of variable1 is", variable1) | |
# performing equality | |
EqualityOutput = variable1 == 100 | |
print("is variable1 == 100", EqualityOutput) | |
# result: is variable1 == 100 True | |
# performing not equal to | |
output = variable1 !=100 | |
print("is variable1 != 100", output) | |
# result: is variable1 != 100 False |
3. Logical Operators:
In Programming we have to check for multiple conditions to reach a conclusion. And for that we use Logical Operators.
A logical operator works on the conditions and gives output as True or False.
Following are the logical operators:
a. and :
If the value of both the condition is True then, “and” operator will True as output. If the value of any of the condition is False then, “and” operator will give False as output.
For Example: 12> 13 and 12<13 will give False
b. or :
If the value of any of the condition is True then, “or” operator will give True as output. If both the condition is False then, “or” operator will give False as output.
For Example: 12>13 or 12<13 will give True
c. not :
“not” operator reciprocates the output of condition. It converts True to False and vice versa.
For example: not 12> 13 will give True as output
# Declare variable1 as 100 and variable as 200 | |
variable1 = 100 | |
variable2 = 200 | |
print("value of variable1", variable1) | |
print("value of variable2", variable2) | |
# performing and operation | |
andOutput = variable1 > 90 and variable1 < 105 | |
print("is vairable1 greater than 90 and less than 105", andOutput) | |
# output: is vairable1 greater than 90 and less than 105 True | |
# performing or operation | |
orOperation = variable1 > variable2 or variable2 < 12 | |
print("variable1 is greater than variable2 or variable2 less than 12", orOperation) | |
# output: variable1 is greater than variable2 or variable2 less than 12 False | |
# performing not operation | |
notOperation = not variable1 > variable2 | |
print(" not variable1 > variable2", notOperation) | |
# output: not variable1 > variable2 True |
4. Assignment Operators:
These operators are used to assign the values into a variable. The assignment is done with the help of “=” (equal to ) operator.
For example: variable1 = 12
There is also a variation in the assignment called as short-hand operators. They are used to reduce the code and perform the operation while assigning.
For example: variable2 = variable2 + 12 will be written as variable2 +=12
Both the statements have the same meaning, but the second one is the short version of the first statement. Following are the short-hand operators used in python:
- += adds the value to the variable before assigning
- -= subtracts the value from variable before assigning
- *= multiplies the value to the variable before assigning
- /= divides the value to the variable before assigning
- %= perform modulus to the variable by value before assigning
- **= perform exponential to the variable by value before assigning
- //= performs floor division to the variable by value before assigning
# DeoutputOperationlare the variable, variable1 as 21 and variable2 as 4 | |
# we are going to perform series of operations and the result will be stored in outputOperation variable | |
variable1 = 21 | |
variable2 = 4 | |
print("variable1 =", variable1) # output: variable1 = 21 | |
print("variable2 =", variable2) # output: variable2 = 4 | |
# performing addition | |
outputOperation = variable1 + variable2 | |
print("outputOperation = ", outputOperation ) # output: outputOperation = 25 | |
# performing shorthand addition | |
outputOperation += variable1 | |
print("outputOperation = ", outputOperation ) # output: outputOperation = 46 | |
# performing shorthand multiplication | |
outputOperation *= variable1 | |
print("outputOperation = ", outputOperation ) # output: outputOperation = 966 | |
# performing shorthand divison | |
outputOperation /= variable1 | |
print("outputOperation = ", outputOperation ) # output: outputOperation = 46.0 | |
# performing shorthand modulus | |
outputOperation %= variable1 | |
print("outputOperation = ", outputOperation ) # output: outputOperation = 4.0 | |
# performing shorthand exponent | |
outputOperation **= variable2 | |
print("outputOperation = ", outputOperation ) # output: outputOperation = 256.0 | |
# performing shorthand floor division | |
outputOperation //= variable1 | |
print("outputOperation = ", outputOperation ) # output: outputOperation = 12.0 |
5. Membership Operators:
These operators are used to find out whether a value is part of a variable or not. These operators are not for numbers. These operators can be used for all other data types. These are
a. in :
This operator returns True if the value is part of the variable.
# Declare a string1 variable | |
# Find "H1" in string 1 | |
string1 = "Hello World" | |
print("value of string1 is", string1) | |
# output: value of string1 is Hello World | |
inOutput = "H" in string1 | |
print("is 'H' in string1", inOutput) | |
# output: is 'H' in string1 True |
b. not in :
This operator returns False if the value is part of the variable.
# Declare a list list1 with value [12,23,34] | |
list1 = [12,23,34] | |
print("value os list1 is", list1) | |
# output: value os list1 is [12, 23, 34] | |
# using not in operator for 25 | |
notInOperator = 25 not in list1 | |
print("is 25 not in list1", notInOperator) | |
# output: is 25 not in list1 True |
Note: in and not in operators will work for Dictionary Keys and not values.
6. Bitwise Operators:
These operators are used on numbers only and they are used to perform bit-level manipulation.
These operators first convert a number into the binary form and then perform specific operations. The result is then converted back into decimal number format and given as output.
We can check the binary value of a number by using bin function. Following are the operators:
- & (Binary AND) – performs “and” operations on bits of both variables
- | (Binary OR) - performs “or” operations on bits of both variables
- ^ (Binary XOR) – gives output as 1 bit if one bit
- ~ (Binary Complement) – used for a single variable, this operator complements the binary bits of a variable
- << (Binary Left Shift) – variable binary value is moved to shifted to the left side and 0 is added in the end
- >> (Binary Right Shift) – variable binary value is moved towards the right side and the rightmost bit is discarded
# declare 2 variables, variable1 with value 78, and variable2 with value 341 | |
# we are going to perform series of operations and the result will be stored in output variable | |
variable1 = 78 | |
variable2 = 341 | |
print("variable1 =",variable1," - binary value =",bin(variable1)) | |
print("variable2 =",variable2," - binary value =",bin(variable2)) | |
# output: variable1 = 78 - binary value = 0b1001110 | |
# output: variable2 = 341 - binary value = 0b101010101 | |
# performing binary & operation | |
output = variable1 & variable2 | |
print ("output of binary & operation is ", output,' -',bin(output)) | |
# output : output of binary & operation is 68 - 0b1000100 | |
# performing binary or operation | |
output = variable1 | variable2 | |
print ("output of binary or operation is ", output,' -',bin(output)) | |
# output : output of binary or operation is 351 - 0b101011111 | |
# performing binary xor operation | |
output = variable1 ^ variable2 | |
print ("output of binary xor operation is ", output,' -',bin(output)) | |
# output: output of binary xor operation is 283 - 0b100011011 | |
# performing binary complement operation | |
output = ~variable1 | |
print ("output of binary complement is ", output,' -',bin(output)) | |
# output: output of binary complement is -79 - -0b1001111 | |
# performing binary left shift operation | |
output = variable2 << 2 | |
print ("output of binary left shift is ", output,' -',bin(output)) | |
# output: output of binary left shift is 1364 - 0b10101010100 | |
# performing binary right shift operation | |
output = variable2 >> 2 | |
print ("output of binary right shift is ", output,' -',bin(output)) | |
# output: output of binary right shift is 85 - 0b1010101 |
Operator Precedence:
What will happen if in an expression there is more than one operator? In that case, operators will follow the following precedence.
- ** Exponentiation (raise to the power)
- ~ Binary Complement
- * / % // Multiply, divide, modulo and floor division
- + - Addition and subtraction
- >> << Right and left bitwise shift
- & Bitwise 'AND'
- ^| Bitwise exclusive `OR' and regular `OR'
- <= < > >= Comparison operators
- <> == != Equality operators
- = %= /= //= -= += *= Assignment operators
- in not in Membership operators
- not or and Logical operators
Key Takeaways:
- Arithmetic operators are used only by the number data types.
- Relational operators compare the values and give the output as True or False.
- Logical Operators compares the conditions and give output as True or False.
- Assignment operators are used to assigning values and short-hand operators are used to manipulate value before assigning.
- A membership variable tells whether a value is part of a variable or not.
- Bitwise operator operates on the binary bits of number data type only.
Programming exercises:
- Perform the following steps:
- Declare a variable variable1 assign value 200 to it
- Perform short-hand subtraction of the value 20 on variable1
- Perform short-hand modulus with the value 27 on variable1
- Perform left shit bitwise operator with 2 as a value on variable1
- Compare equality operator on variable1 with 72 and assign the value in a variable output
- Print value of output, it should be True.
- Perform the following steps:
- Declare a variable, variable2 with the value 23
- Perform short-hand multiplication with the value 456 on variable2
- Perform short-hand floor division with the value 34 on variable2
- Perform right shit bitwise operator with 2 as a value on variable2
- Declare a list, list1 with the value [7, 77, 777]
- Perform variable2 "not in" operator on list1 and assign the value in a variable output.
- Print the value of output, it should be False.
You can download the solution files Python operators