What are Python Operators?
In Python, operators are symbols used to perform operations on variables and values. They are the foundation of most programming tasks, helping you perform mathematical calculations, comparisons, logical operations, and more. Python supports a wide variety of operators, categorized into different types based on their functionality.
Types of Python Operators
Python operators are broadly classified into the following categories:
- Arithmetic Operators
- Comparison (Relational) Operators
- Logical Operators
- Assignment Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
1. Arithmetic Operators
Used for basic mathematical operations like addition, subtraction, multiplication, etc.
Operator | Description | Example |
---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
% | Modulus (remainder) | a % b |
** | Exponentiation | a ** b |
// | Floor Division | a // b |
Example: Arithmetic Operators.
a = 10
b = 3
print("Addition:", a + b) # Output: 13
print("Subtraction:", a - b) # Output: 7
print("Multiplication:", a * b) # Output: 30
print("Division:", a / b) # Output: 3.3333333333333335
print("Modulus:", a % b) # Output: 1
print("Exponentiation:", a ** b) # Output: 1000
print("Floor Division:", a // b) # Output: 3
2. Comparison Operators
Used to compare values and return a Boolean (True
or False
).
Operator | Description | Example |
---|---|---|
== | Equal to | a == b |
!= | Not equal to | a != b |
> | Greater than | a > b |
< | Less than | a < b |
>= | Greater than or equal to | a >= b |
<= | Less than or equal to | a <= b |
Example:
x = 5
y = 10
print(x == y) # Output: False
print(x != y) # Output: True
print(x > y) # Output: False
print(x <= y) # Output: True
3. Logical Operators
Used to combine conditional statements.
Operator | Description | Example |
---|---|---|
and | Returns True if both conditions are True | x and y |
or | Returns True if at least one condition is True | x or y |
not | Reverses the result | not x |
Example:
a = True
b = False
print(a and b) # Output: False
print(a or b) # Output: True
print(not a) # Output: False
4. Assignment Operators
Used to assign values to variables.
Operator | Description | Example |
---|---|---|
= | Assign | x = 5 |
+= | Add and assign | x += 3 |
-= | Subtract and assign | x -= 2 |
*= | Multiply and assign | x *= 2 |
/= | Divide and assign | x /= 2 |
Example:
x = 5
x += 3 # Same as x = x + 3
print(x) # Output: 8
5. Membership Operators
Used to check if a value is in a sequence like a string, list, or tuple.
Operator | Description | Example |
---|---|---|
in | Returns True if found | 'a' in 'apple' |
not in | Returns True if not found | 'x' not in 'apple' |
Example:
fruits = ["apple", "banana", "cherry"]
print("apple" in fruits) # Output: True
print("grape" not in fruits) # Output: True
6. Identity Operators
Used to compare memory locations of two objects.
Operator | Description | Example |
---|---|---|
is | Returns True if same object | x is y |
is not | Returns True if not same object | x is not y |
Example:
x = ["apple", "banana"]
y = x
z = ["apple", "banana"]
print(x is y) # Output: True
print(x is z) # Output: False
print(x == z) # Output: True
Python operators are essential for performing various operations on data. Whether you’re calculating numbers, comparing values, or checking conditions, operators make your code more dynamic and functional. Understanding these operators will help you write efficient and powerful Python programs.
Keep Learning 🙂