Operators are used to perform operations on variables and values.
Python provides different types of operators such as arithmetic, assignment, comparison, logical, identity, membership, and bitwise operators.
Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
| Operator | Name | Example |
|---|---|---|
+ | Addition | x + y |
- | Subtraction | x - y |
* | Multiplication | x * y |
/ | Division | x / y |
% | Modulus | x % y |
** | Exponentiation | x ** y |
// | Floor Division | x // y |
Example:
x = 10
y = 3
print(x + y)
print(x - y)
print(x * y)
print(x / y)
print(x % y)
print(x ** y)
print(x // y)
Output:
13
7
30
3.3333333333333335
1
1000
3
Assignment Operators
Assignment operators are used to assign values to variables.
| Operator | Example |
|---|---|
= | x = 5 |
+= | x += 3 |
-= | x -= 3 |
*= | x *= 3 |
/= | x /= 3 |
%= | x %= 3 |
Example:
x = 5
x += 3
print(x)
Output:
8
Comparison Operators
Comparison operators compare two values and return True or False.
| Operator | Name |
|---|---|
== | Equal |
!= | Not Equal |
> | Greater Than |
< | Less Than |
>= | Greater Than or Equal |
<= | Less Than or Equal |
Example:
x = 10
y = 5
print(x == y)
print(x > y)
print(x < y)
Output:
False
True
False
Logical Operators
Logical operators combine conditional statements.
| Operator | Description |
|---|---|
and | Returns True if both statements are true |
or | Returns True if one statement is true |
not | Reverses the result |
Example:
x = 10
print(x > 5 and x < 20)
print(x > 20 or x < 20)
print(not(x > 20))
Output:
True
True
True
Identity Operators
Identity operators compare the memory location of two objects.
| Operator | Description |
|---|---|
is | Returns True if both variables are the same object |
is not | Returns True if both variables are not the same object |
Example:
x = ["apple", "banana"]
y = x
print(x is y)
Output:
True
Membership Operators
Membership operators check whether a value exists in a sequence.
| Operator | Description |
|---|---|
in | Returns True if value exists |
not in | Returns True if value does not exist |
Example:
fruits = ["apple", "banana", "mango"]
print("apple" in fruits)
print("orange" not in fruits)
Output:
True
True
Bitwise Operators
Bitwise operators work on binary numbers.
| Operator | Name |
|---|---|
& | AND |
| ` | ` |
^ | XOR |
~ | NOT |
<< | Left Shift |
>> | Right Shift |
Example:
x = 5
y = 3
print(x & y)
Output:
1
Operator Precedence
Python follows a specific order when evaluating expressions.
Example:
print(5 + 2 * 3)
Output:
11
Multiplication is performed before addition.
Use parentheses to change the order:
print((5 + 2) * 3)
Output:
21
Summary
- Operators are used to perform operations on values and variables.
- Arithmetic operators perform mathematical calculations.
- Assignment operators assign values to variables.
- Comparison operators return boolean values.
- Logical operators combine conditions.
- Identity operators compare object identities.
- Membership operators check for values in collections.
- Bitwise operators work with binary numbers.
- Operator precedence determines the order of execution.