Like any other programming language, Python also provides several operators like arithmetic, comparison, logical, assignment, and bitwise that are used to perform specific operations on values & variables.
Table of Contents
- Introduction to Python operators
- Arithmetic operators
- Comparison operators
- Logical operators
- Assignment operators
- Bitwise operators
- Membership operators
- Identity operators
- Conclusion
Introduction to Python Operators
Understanding the different types of Python operators and their usage is important for writing efficient and effective Python code. Python operators are special symbols that are used to perform specific operations on values and variables. These operators operate on values called operands.
Below is a simple example.
# Operator example
>>> 5-2
3
In the above example, Opeator – is the arithmetic operator, 5 and 2 are operands, and 3 is the result of the operation.
Arithmetic operators
These arithmetic operators in Python are used for performing addition, subtraction, multiplication, division operations, etc.
Operator | Meaning | Example |
---|---|---|
+, addition | Adds two operands | 3+4=7 |
-, subtraction | Subtracts right operand from the left | 4-1=3 |
*, multiplication | Multiplies two operands | 2*3=6 |
/, division | Divides the left operand by the right one | 15/4=3.75 |
%, modulus | Returns the remainder when the first operand is divided by the second | 6%4=2 |
Comparison operators
These comparison operators are used to compare values.
Operator | Meaning | Example |
---|---|---|
>, greater than | Returns True if the left operand is greater than the right | 3>1=True |
<, less than | Returns True if the left operand is less than the right | 3<1=False |
==, equal to | Returns True if both operands are equal | 3==1=False |
!=, not equal to | Returns True if both operands are not equal | 3!=1=True |
>=, greater than or equal to | Returns True if the left operand is greater than or equal to the right | 3>=1=True |
<=, less than or equal to | Returns True if the left operand is less than or equal to the right | 3<=1=False |
Logical operators
These logical operators perform logical operations
Operator | Meaning | Example |
---|---|---|
And | Returns True if both operands are true | True and True = True |
or | Returns True if either one of the operands is true | True or False = True |
not | Returns True if the operand is false or vice versa | not True = False |
Assignment operators
These assign operators are used to assign values to variables
Operator | Description | Example | Equivalent to |
---|---|---|---|
= | Assigns a value to a variable | x=2 | x=2 |
+= | Adds the value on the right to the variable on the left, then assigns the result to the left variable | x+=2 | x=x+2 |
-= | Subtracts the value on the right from the variable on the left, then assigns the result to the left variable | x-=2 | x=x-2 |
*= | Multiplies the variable on the left by the value on the right, then assigns the result to the left variable | x*=2 | x=x*2 |
/= | Divides the variable on the left by the value on the right, then assigns the result to the left variable | x/=2 | x=x/2 |
%= | Divides the variable on the left by the value on the right, then assigns the remainder to the left variable | x%=2 | x=x%2 |
//= | Divides the variable on the left by the value on the right, then assigns the integer quotient to the left variable | x//=2 | x=x//2 |
**= | Raises the variable on the left to the power of the value on the right, then assigns the result to the left variable | x**=2 | x=x**2 |
Bitwise operators
Bitwise operators are used for performing bitwise operations on integers. The following table lists the bitwise operators in Python.
Operator | Name | Description |
---|---|---|
& | Bitwise AND | Sets each bit to 1 if both bits are 1 |
| | Bitwise OR | Sets each bit to 1 if at least one of the bits is 1 |
^ | Bitwise XOR | Sets each bit to 1 if only one of the bits is 1 |
~ | Bitwise NOT | Inverts all the bits |
<< | Left shift | Shifts the bits of the first operand to the left by the second operand |
Membership operators
These Python operators are used to test whether a value is found in a sequence e.g. string, list, tuple, dictionary, set, etc.
Operator | Meaning | Example |
---|---|---|
in | Returns True if the value is found in sequence and vice versa | ‘h’ in ‘hello’ = True1 in [1, 2] = True |
not in | Returns True if the value is not found in the sequence | 3 not in [1, 2] = True‘H’ not in ‘hello’ = True |
Identity operators
Operator | Meaning | Example |
---|---|---|
is | Returns True if both operands are the same object | 5 is 5 = True |
is not | Returns True if both operands are not of the same object | 5 is not 5 = False |
These operators are used to compare the objects if they are the same and have the same memory location
Conclusion
Understanding operators in Python is a must-have skill for any developer because these operators play a very crucial role as you are creating programs that require data manipulation, mathematical calculations, or logical comparisons. We hope you will keep practicing and experimenting with these concepts to enhance your skills and become a better programmer.