Let’s learn about what are Python operators.
In the Python programming language, the operators are used to perform operations on the values or variables. The operators can return results based on the type of operator being used.
The operators in Python are grouped based on the functionality they provide. Here is the list of types of operators in Python
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Identity Operators
- Membership Operators
- Bitwise Operators
Here is the source code to demonstrate different Python operators
var1 = 10 var2 = 20 var3 = var1 + var2 print(var3) var3 = var2 - var1 print(var3) var1 = "Hello," var2 = " World!" var3 = var1 + var2 print(var3)
Below is the output of the above code
30
10
Hello, World!
As you can see from the above output that the operators perform operations on the values and return a result based on the type of operator being used and the types of values being provided as input for operations.
Python Arithmetic Operators
Python arithmetic operators are mostly used with numbers or numeric values to perform the mathematical operations or evaluations that are required in the program logic. For example, if you are writing a code for a banking application then these operations will be required to support the banking operations.
Here is the list of arithmetic operators available in the Python programming language
Name | Symbol | Example |
---|---|---|
Addition | + | var1 + var2 |
Subtraction | – | var1 – var2 |
Division | / | var1 / var2 |
Multiplication | * | var1 * var2 |
Modulus | % | var1 % var2 |
Exponentiation | ** | var1 ** var2 |
Floor division | // | var1 // var2 |
Here is the source code to demonstrate the use of Python arithmetic operators
var1 = 10 var2 = 20 print(var1 + var2) print(var2 - var1) print(var1 / var2) print(var1 * var2) print(var1 % var2) print(var2 ** var1) print(var2 // var1)
Below is the output of the above code
30
10
0.5
200
10
10240000000000
2
As you can see from the above output we can use arithmetic operators in Python to perform mathematical calculations on the values and variables.
Python Assignment Operators
In Python programming language the assignment operators as the name suggest are used for assigning values to the variables.
Here is the list of Python assignment operators available in the Python programming language
Name | Symbol | Example |
---|---|---|
Assign | = | a = b |
Add & assign | += | a += b |
Subtract & assign | -= | a -= b |
Multiply & assign | *= | a *= b |
Divide & assign | /= | a /= b |
Modulus & assign | %= | a %= b |
Divide & assign | //= | a //= b |
Exponent & assign | **= | a **= b |
Bitwise AND on operands and assign | &= | a &= b |
Bitwise OR on operands and assign | |= | a |= b |
Bitwise xOR on operands and assign | ^= | a ^= b |
Bitwise right shift on operands and assign | >>= | a >>= b |
Bitwise left shift on operands and assign value | <<= | a <<= b |
Here is the source code to demonstrate the use of Python assignment operators
b = 5 a = b #Assign the value of the right side of the expression to the left operand print(a) a = 50 a += b #Add the right side operand with the left side operand and then assign the result to the left side operand print(a) a = 50 a -= b #Subtract the right side operand from the left side operand and then assign the result to the left side operand print(a) a = 50 a *= b #Multiply the right side operand with the left side operand and then assign the result to the left side operand print(a) a = 50 a /= b #Divide the left side operand with the right side operand and then assign the result to the left side operand print(a) a = 50 a %= b #Takes modulus using the left side and the right side operand and assign the result to left operand print(a) a = 50 a //= b #Divide the left side operand with the right side operand and then assign the floor of the result to the left side operand print(a) a = 50 a **= b #Calculate the exponent (raise power) value using operands and then assign the result to left operand print(a) a = 50 a &= b #Perform Bitwise AND on the left side and the right side operand and then assign the result to left operand print(a) a = 50 a |= b #Perform Bitwise OR on the left side and the right side operand and then assign the result to left operand print(a) a = 50 a ^= b #Perform Bitwise XOR on the left side and the right side operand and then assign the result to left operand print(a) a = 50 a >>= b #Perform Bitwise right shift on the left side and the right side operand and then assign the result to left operand print(a) a = 50 a <<= b #Perform Bitwise left shift on the left side and the right side operand and then assign the result to left operand print(a)
Below is the output of the above code
5
55
45
250
10.0
0
10
312500000
0
55
55
1
1600
As you can see from the above output Python assignment operators are used to perform value assignments to the variables and also some mathematical calculations can also be done before assigning the values.
Python Comparison Operators
In Python programming language the comparison operators as the name suggest are used to compare values in the variables. All the Python comparisons using comparison operators return a boolean i.e. either true or false based on the values being compared.
Here is the list of Python comparison operators available in the Python programming language
Name | Symbol | Example |
---|---|---|
Equal | == | a == b |
Not equal | != | a != b |
Greater than | > | a > b |
Less than | < | a < b |
Greater than or equal to | >= | a >= b |
Less than or equal to | <= | a <= b |
Equal – Returns true if both operands are equal
Not equal – Returns true if operands are not equal
Greater than – Returns true if the left operand is greater than the right
Less than – Returns true if the left operand is less than the right
Greater than or equal to – Returns true if the left operand is greater than or equal to the right
Less than or equal to – Returns true if the left operand is less than or equal to the right
Here is the source code to demonstrate the use of Python comparison operators
a = 10 b = 20 print(a == b) print(a != b ) print(a > b) print(a < b) print(a >= b) print(a <= b)
Below is the output of the above code
False
True
False
True
False
True
As you can see from the above output that comparison operators return only boolean values either true or false.
Python Logical Operators
In Python programming language the logical operators are used to combine conditional statements. The Python logical operators perform the logical AND, logical OR, and logical NOT operations.
Here is the list of Python logical operators available in the Python programming language
Description | Operator | Example |
---|---|---|
Returns True if both statements are true | and | a < 10 and a < 20 |
Returns True if one of the statements is true | or | a < 10 or a < 20 |
Reverse the result, returns False if the result is true | not | not(a < 10 and a < 20) |
and – returns true if both the conditional statements are true
or – returns true if either of the conditional statement is true
not – returns true if the operand is false
Here is the source code to demonstrate the use of Python logical operators
# This is a sample Python script. a = True b = False print(a and b) print(a or b) print(not a)
Below is the output of the above code
False
True
False
As you can see from the above output that “and” returns false if one statement is false, “or” returns true if any one statement is true & “not” returns false in operand is true.
Python Identity Operators
In Python programming language the identity operators are used to compare the objects. The Python identity operators do not compare if the objects are equal but compare if the objects are the same or not with the matching memory location.
Here is the list of Python identity operators available in the Python programming language
Description | Operator | Example |
---|---|---|
Returns True if both variables are the same object | is | a is b |
Returns True if both variables are not the same object | is not | a is not b |
is – return true if the objects are identical
is not – return true if the objects are not identical
Here is the source code to demonstrate the use of Python identity operators
a = 10 b = 11 c = a print(a is b) print(a is c) print(b is c)
Below is the output of the above code
False
True
False
As you can see from the above output object a is identical to c but a is not identical to b and also b is not identical to c
Python Membership Operators
In Python programming language there are two membership operators and they are used to check or validate the presence of a value in a sequence such as a string, list, tuple, etc.
Here is the list of Python membership operators available in the Python programming language
Description | Operator | Example |
---|---|---|
Returns True if a specified value is present in the object | in | a in b |
Returns True if a specified value is not present in the object | not in | a not in b |
in – The in operator evaluates to true if the specified character, string or element is present in a sequence.
not in – The not in operator evaluates to true if the specified character, string or element is not present in a sequence.
Here is the source code to demonstrate the use of Python membership operators
seq1 = [2, 4, 6, 8, 12] a = 6 b = 7 print(a in seq1) print(b in seq1) print(a not in seq1) print(b not in seq1)
Below is the output of the above code
True
False
False
True
As you can see from the above output the Python membership operator is checking if the specified value is present in the sequence (list) or not.
Python Bitwise Operators
In Python programming language the bitwise operators as the name suggests are used to act on bits and perform bit-by-bit operations. The Python bitwise operators are used to perform the binary numbers.
Here is the list of Python bitwise operators available in the Python programming language
Name | Symbol | Example |
---|---|---|
AND | & | a & b |
OR | | | a | b |
XOR | ^ | a ^ b |
NOT | ~ | ~a |
Zero fill left shift | << | a << |
Signed right shift | >> | a >> |
AND – Returns 1 If both the bits are 1 else returns 0
OR – Returns 1 If any of the bits is 1 else returns 0
XOR – Returns 1 if any one of the bits is 1 and the other is 0
NOT – Inverts all the bits
Zero fill left shift – Shifts the bits of the number to the left and fills 0 on voids right as a result
Signed right shift – Shifts the bits of the number to the right and fills 0 on voids left as a result
Here is the source code to demonstrate the use of Python bitwise operators
a = 10 b = 4 print(a & b) print(a | b) print(a ^ b) print(~a) print(a << 1) print(a >> 1)
Below is the output of the above code
0
14
14
-11
20
5
From the above results, you can see the workings of the Python bitwise operators
Summary
You can also check my series on Python for Beginners – Series: Python Tutorial – Learn Python Programming for Beginners
References – Python Documentation
Please provide your suggestions & feedback in the comments section below
Hope you found this article useful. Please support the Author
