A Beginner's Guide to Python Operators
Understand the different types of operators in Python with clear examples.
Python Operators: Simplifying Operations
In Python, operators are special symbols that perform operations on variables and values. This guide will explore different types of operators you can use in your Python code.
Types of Operators in Python
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Identity Operators
- Membership Operators
- Bitwise Operators
1. Arithmetic Operators
Arithmetic operators allow you to perform mathematical operations on numbers.
Operator | Name | Example | Description |
---|---|---|---|
+ | Addition | 10 + 5 | Adds two values |
- | Subtraction | 10 - 5 | Subtracts one value from another |
* | Multiplication | 10 * 5 | Multiplies two values |
/ | Division | 10 / 5 | Divides one value by another |
% | Modulus | 10 % 3 | Returns the remainder of division |
** | Exponentiation | 2 ** 3 | Raises a number to the power of another |
// | Floor Division | 10 // 3 | Divides and rounds down to the nearest whole number |
Example:
print(10 + 5) # Output: 15
2. Assignment Operators
Assignment operators are used to assign values to variables.
Operator | Example | Equivalent Expression |
---|---|---|
= | x = 5 | Assigns 5 to x |
+= | x += 3 | x = x + 3 |
-= | x -= 3 | x = x - 3 |
*= | x *= 3 | x = x * 3 |
/= | x /= 3 | x = x / 3 |
%= | x %= 3 | x = x % 3 |
**= | x **= 3 | x = x ** 3 |
Example:
x = 5
x += 3 # x is now 8
print(x) # Output: 8
3. Comparison Operators
Comparison operators compare two values and return a Boolean result (True or False).
| Operator | Name | Example | Description |
|----------|------------------|----------|------------------------------------------|
| == | Equal | x == y | Checks if x is equal to y |
| != | Not equal | x != y | Checks if x is not equal to y |
| > | Greater than | x > y | Checks if x is greater than y |
| < | Less than | x < y | Checks if x is less than y |
| >= | Greater or equal | x >= y | Checks if x is greater than or equal to y|
| <= | Less or equal | x <= y | Checks if x is less than or equal to y |
Example:
print(10 == 10) # Output: True
4. Logical Operators
Logical operators combine conditional statements.
Operator | Description | Example |
---|---|---|
and | Returns True if both statements are true | x < 5 and x < 10 |
or | Returns True if one of the statements is true | x < 5 or x < 4 |
not | Reverses the result | not(x < 5 and x < 10) |
Example:
print(5 < 10 and 10 < 15) # Output: True
5. Identity Operators
Identity operators check if two variables point to the same object.
Operator | Description | Example |
---|---|---|
is | Returns True if both are the same object | x is y |
is not | Returns True if both are not the same object | x is not y |
Example:
x = [1, 2, 3]
y = x
print(x is y) # Output: True
6. Membership Operators
Membership operators test for membership in a sequence (like lists, tuples, and strings).
Operator | Description | Example |
---|---|---|
in | Returns True if a value is found in a sequence | x in y |
not in | Returns True if a value is not found in a sequence | x not in y |
Example:
fruits = ["apple", "banana", "cherry"]
print("banana" in fruits) # Output: True
7. Bitwise Operators
Bitwise operators perform operations on binary numbers.
| Operator | Name | Description | Example |
|----------|------------|-------------------------------------|----------|
| & | AND | Sets each bit to 1 if both bits are 1 | x & y |
| \| | OR | Sets each bit to 1 if one of two bits is 1 | x \| y |
| ^ | XOR | Sets each bit to 1 if only one of two bits is 1 | x ^ y |
| ~ | NOT | Inverts all the bits | ~x |
| << | Left shift | Shifts bits to the left | x << 2 |
| >> | Right shift| Shifts bits to the right | x >> 2 |
Example:
print(5 & 3) # Output: 1 (binary 0101 & 0011 = 0001)
Operator Precedence
Operator precedence determines the order in which operations are evaluated. Higher precedence operators are evaluated first.
Order of Precedence
- Parentheses
()
- Exponentiation
**
- Unary operations
+x, -x, ~x
- Multiplication, Division, Floor Division, Modulus
*, /, //, %
- Addition, Subtraction
+, -
- Bitwise Shifts
<<, >>
- Bitwise AND
&
- Bitwise XOR
^
- Bitwise OR
|
- Comparison Operators
==, !=, >, <, >=, <=, is, is not, in, not in
- Logical NOT
not
- Logical AND
and
- Logical OR
or
Example:
print((6 + 3) - (6 + 3)) # Output: 0
print(100 + 5 * 3) # Output: 115 (5 * 3 evaluated first)
If two operators have the same precedence, they are evaluated from left to right.
Example:
print(5 + 4 - 7 + 3) # Output: 5 (evaluated left to right)