- 1Introduction to Python
- 2Python Hello World Program
- 3Python Comments Explained - Beginner’s Guide with Examples
- 4Python Variables
- 5Python Constants
- 6Python Data Types
- 7Python Strings
- 8Python Type Conversions
- 9Python Basic Input and Output - Learn to Take User Input and Print Output
- 10Python Operators
- 11Python Operator Precedence – Evaluation Order with Examples
Python Operators
Operators in Python are symbols or keywords that help you perform different kinds of actions, like calculations, comparisons, or checking conditions. Let’s explore each type of operator with easy-to-understand examples.
1. Arithmetic Operators
These are used to do basic math like addition, subtraction, multiplication, etc.
Operator | Name | Example | Result |
---|---|---|---|
+ | Addition | 3 + 2 | 5 |
- | Subtraction | 5 - 2 | 3 |
* | Multiplication | 4 * 2 | 8 |
/ | Division | 6 / 2 | 3.0 |
% | Modulus (Remainder) | 5 % 2 | 1 |
// | Floor Division | 5 // 2 | 2 |
** | Exponent (Power) | 2 ** 3 | 8 |
Example
a = 10
b = 3
print(a + b) # 13
print(a ** b) # 1000 (10³)
2. Comparison Operators
These are used to compare two values. The result is always either True
or False
.
Operator | Meaning | Example | Result |
---|---|---|---|
== | Equal to | 3 == 3 | True |
!= | Not equal to | 5 != 2 | True |
> | Greater than | 5 > 3 | True |
< | Less than | 2 < 4 | True |
>= | Greater or equal | 4 >= 4 | True |
<= | Less or equal | 3 <= 5 | True |
Example
x = 7
y = 10
print(x < y) # True
print(x == y) # False
3. Assignment Operators
These are used to assign or update values of variables.
Operator | Meaning | Example |
---|---|---|
= | Assign | x = 5 |
+= | Add and assign | x += 3 (same as x = x + 3) |
-= | Subtract and assign | x -= 2 |
*= | Multiply and assign | x *= 4 |
/= | Divide and assign | x /= 2 |
%= | Modulus and assign | x %= 3 |
//= | Floor divide and assign | x //= 2 |
**= | Power and assign | x **= 2 |
Example
x = 4
x += 1
print(x) # 5
4. Logical Operators
Used to combine multiple conditions.
Operator | Meaning | Example | Result |
---|---|---|---|
and | True if both are True | 5 > 3 and 4 > 2 | True |
or | True if at least one is True | 5 > 3 or 2 > 4 | True |
not | Reverse the result | not(5 == 5) | False |
Example
a = 7
print(a > 5 and a < 10) # True
print(not a == 7) # False
5. Identity Operators
Used to check if two variables point to the same object.
Operator | Meaning | Example | Result |
---|---|---|---|
is | True if both are same object | a is b | True or False |
is not | True if not same object | a is not b | True or False |
Example
a = [1, 2]
b = a
print(a is b) # True
print(a is not b) # False
6. Membership Operators
Used to check if a value is inside a group (like a list or string).
Operator | Meaning | Example | Result |
---|---|---|---|
in | True if value is present | "a" in "apple" | True |
not in | True if value not present | "x" not in "apple" | True |
Example
colors = ["red", "blue"]
print("red" in colors) # True
print("green" not in colors) # True
7. Bitwise Operators (Just a Peek)
These work on binary (0s and 1s). You won’t need them yet, but here’s what they do:
Operator | Name |
---|---|
& | Bitwise AND |
| | Bitwise OR |
^ | Bitwise XOR |
~ | Bitwise NOT |
<< | Left Shift |
>> | Right Shift |
Conclusion
Operators are the tools that help you build logic in your Python programs. Now that you’ve learned the basics, try using them in your own mini programs!