Python Operators : Helpful Guide to Arithmetic, Comparison, Logical & More Operations
PythonSep 11, 2024
Python is a versatile and powerful programming language, widely known for its simplicity and readability. One of the most essential features of Python is its operators, which allow you to perform a variety of operations on variables and data.
Table of Contents
From basic arithmetic operations like addition and subtraction to more advanced operations like logical and bitwise computations, Python operators form the foundation of many programming tasks.
This article will delve into the various types of operators in Python, explaining how they work and when to use them, along with examples to illustrate their usage.
What are Python Operators:
Python operators are special symbols or keywords that tell the Python interpreter to perform specific operations on one or more operands. These operations range from mathematical computations to logical evaluations, comparisons, and even bitwise manipulations.
Python Operators act on variables and values to manipulate data, allowing you to create more complex logic and functionality in your code.
List of Python Operators:
Generally, Python Operators are used to perform operations on values and variables. The programming operators of Python are listed below:
- Arithmetic Operators
- Assignment Operators
- Relational Or Comparison Operators
- Logical Operators
- Identity Operators
- Bitwise Operators
- Membership Operators
Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations. Python supports all the basic arithmetic operations like addition, subtraction, multiplication, division, and more.
Operator | Description | Example | Result |
---|---|---|---|
+ |
Addition | 5 + 3 |
8 |
- |
Subtraction | 5 - 3 |
2 |
* |
Multiplication | 5 * 3 |
15 |
/ |
Division | 5 / 2 |
2.5 |
% |
Modulus (remainder) | 5 % 2 |
1 |
** |
Exponentiation | 5 ** 2 |
25 |
// |
Floor Division | 5 // 2 |
2 |
Examples:
a = 10 b = 3 # Addition print(a + b) # Output: 13 # Subtraction print(a - b) # Output: 7 # Multiplication print(a * b) # Output: 30 # Division print(a / b) # Output: 3.3333333333333335 # Modulus print(a % b) # Output: 1 # Exponentiation print(a ** b) # Output: 1000 # Floor Division print(a // b) # Output: 3
Comparison Operators
Comparison operators are used to compare values. They return either True
or False
depending on the condition.
Operator | Description | Example | Result |
---|---|---|---|
== |
Equal to | 5 == 5 |
True |
!= |
Not equal to | 5 != 3 |
True |
> |
Greater than | 5 > 3 |
True |
< |
Less than | 5 < 3 |
False |
>= |
Greater than or equal to | 5 >= 3 |
True |
<= |
Less than or equal to | 5 <= 3 |
False |
Examples:
x = 5 y = 10 # Equal to print(x == y) # Output: False # Not equal to print(x != y) # Output: True # Greater than print(x > y) # Output: False # Less than print(x < y) # Output: True # Greater than or equal to print(x >= y) # Output: False # Less than or equal to print(x <= y) # Output: True
Logical Operators
Logical Python operators are used to combine conditional statements. They are mostly used in control flow statements like if
, while
, and for
.
Operator | Description | Example | Result |
---|---|---|---|
and |
Logical AND | (x > 5) and (y < 15) |
False |
or |
Logical OR | (x > 5) or (y < 15) |
True |
not |
Logical NOT | not(x > 5) |
True |
Examples:
x = 5 y = 10 # Logical AND print(x > 3 and y < 15) # Output: True # Logical OR print(x > 6 or y < 15) # Output: True # Logical NOT print(not(x > 6)) # Output: True
Bitwise Operators
Bitwise Python operators operate on bits and perform bit-by-bit operations. These operators are less commonly used but are powerful in specific scenarios, like low-level programming or optimizing performance.
Operator | Description | Example | Result (Binary) | Result (Decimal) |
---|---|---|---|---|
& |
AND | 5 & 3 |
0001 |
1 |
` | ` | OR | `5 | 3` |
^ |
XOR | 5 ^ 3 |
0110 |
6 |
~ |
NOT | ~5 |
...11111010 |
-6 |
<< |
Left Shift | 5 << 1 |
1010 |
10 |
>> |
Right Shift | 5 >> 1 |
0010 |
2 |
Examples:
a = 5 # Binary: 101 b = 3 # Binary: 011 # Bitwise AND print(a & b) # Output: 1 (Binary: 001) # Bitwise OR print(a | b) # Output: 7 (Binary: 111) # Bitwise XOR print(a ^ b) # Output: 6 (Binary: 110) # Bitwise NOT print(~a) # Output: -6 (Binary: ...11111010) # Left Shift print(a << 1) # Output: 10 (Binary: 1010) # Right Shift print(a >> 1) # Output: 2 (Binary: 10)
Assignment Operators
Assignment python operators are used to assign values to variables. In addition to the standard assignment =
, Python supports several compound assignment operators.
Operator | Description | Example | Equivalent to |
---|---|---|---|
= |
Assignment | x = 5 |
x = 5 |
+= |
Add and assign | x += 5 |
x = x + 5 |
-= |
Subtract and assign | x -= 5 |
x = x - 5 |
*= |
Multiply and assign | x *= 5 |
x = x * 5 |
/= |
Divide and assign | x /= 5 |
x = x / 5 |
%= |
Modulus and assign | x %= 5 |
x = x % 5 |
//= |
Floor division and assign | x //= 5 |
x = x // 5 |
**= |
Exponentiation and assign | x **= 5 |
x = x ** 5 |
&= |
Bitwise AND and assign | x &= 5 |
x = x & 5 |
` | =` | Bitwise OR and assign | `x |
^= |
Bitwise XOR and assign | x ^= 5 |
x = x ^ 5 |
<<= |
Left shift and assign | x <<= 5 |
x = x << 5 |
>>= |
Right shift and assign | x >>= 5 |
x = x >> 5 |
Example
x = 5 # Add and assign x += 3 # Equivalent to x = x + 3 print(x) # Output: 8 # Subtract and assign x -= 2 # Equivalent to x = x - 2 print(x) # Output: 6 # Multiply and assign x *= 4 # Equivalent to x = x * 4 print(x) # Output: 24 # Divide and assign x /= 6 # Equivalent to x = x / 6 print(x) # Output: 4.0 # Modulus and assign x %= 3 # Equivalent to x = x % 3 print(x) # Output: 1.0 # Exponentiation and assign x **= 3 # Equivalent to x = x ** 3 print(x) # Output: 1.0
Membership Operators
Membership operators check for the presence of a value within a sequence, such as strings, lists, or tuples.
Operator | Description | Example | Result |
---|---|---|---|
in |
Returns True if a value is found in the sequence |
5 in [1, 2, 5] |
True |
not in |
Returns True if a value is not found in the sequence |
5 not in [1, 2, 3] |
True |
Example:
my_list = [1, 2, 3, 4, 5] # Check membership print(3 in my_list) # Output: True print(6 not in my_list) # Output: True
Credits:
- Photo by Douglas Lopes on Unsplash
Conclusion
Operators are the foundation of any Python program. They enable you to perform various operations, from basic arithmetic to advanced bit manipulation and object comparisons. Understanding how to use Python operators effectively is a key step towards becoming proficient in the language.