Python Operators Explained: Types, Examples & Uses (Beginner Guide)
Python
Operators
Explained
All 7 types · Simple words · Real examples
An operator is a symbol that tells Python to do something with values.
For example, + tells Python to add two numbers.
The values it works on are called operands.
+ does the same thing.
a = 10 # a is an operand b = 3 # b is an operand print(a + b) # + is the operator → 13
10 or 3). Operator = the action symbol (like +).
Python has 7 types of operators. Each type has a different job. We will learn all of them one by one.
These are used for basic math — add, subtract, multiply, and divide.
Python also has three special ones:
Floor division // gives only the whole number.
Modulus % gives the leftover (remainder).
Exponent ** raises a number to a power.
3 * 10 = 30. Share 10 chocolates among 3 friends → each gets 10 // 3 = 3, and 10 % 3 = 1 is left over.
| Symbol | Name | Example | Result |
|---|---|---|---|
| + | Addition | 10 + 3 | 13 |
| - | Subtraction | 10 - 3 | 7 |
| * | Multiply | 10 * 3 | 30 |
| / | Division | 10 / 3 | 3.33… |
| // | Floor Div | 10 // 3 | 3 |
| % | Modulus | 10 % 3 | 1 |
| ** | Exponent | 2 ** 3 | 8 |
a = 10 b = 3 print(a + b) # 13 print(a - b) # 7 print(a * b) # 30 print(a / b) # 3.3333... print(a // b) # 3 (no decimal) print(a % b) # 1 (remainder) print(2 ** 3) # 8 (2×2×2)
0 gives a ZeroDivisionError. Always make sure the bottom number is not zero.
These compare two values and always return either True or False.
They are mostly used inside if statements to make decisions in your program
— like "Is the user old enough?" or "Did the score pass?"
| Symbol | Meaning | Example | Result |
|---|---|---|---|
| == | Equal to | 5 == 5 | True |
| != | Not equal | 5 != 3 | True |
| > | Greater than | 7 > 3 | True |
| < | Less than | 2 < 9 | True |
| >= | Greater or equal | 5 >= 5 | True |
| <= | Less or equal | 4 <= 6 | True |
age = 18 print(age == 18) # True — age is 18 print(age != 20) # True — 18 is not 20 print(age > 15) # True — 18 > 15 print(age < 10) # False — 18 is not < 10 print(age >= 18) # True — 18 >= 18
= stores a value. == compares values. Inside if, always use ==, never =.
Logical operators combine two conditions.
Use and when both conditions must be true.
Use or when at least one must be true.
Use not to reverse a condition — turns True into False.
and at work!
| Operator | True when | Example | Result |
|---|---|---|---|
| and | Both are True | True and True | True |
| and | One is False | True and False | False |
| or | At least one True | False or True | True |
| or | Both are False | False or False | False |
| not | Flips the value | not True | False |
age = 20 has_id = True print(age >= 18 and has_id) # True print(age < 18 or has_id) # True print(not has_id) # False
and, if the first condition is False, Python skips the second — it already knows the result is False.
Use = to store a value in a variable.
Shortcut operators like += do math and store in one step.
For example, x += 5 means "add 5 to x, then save it back to x".
These are used a lot inside loops.
x = 10 fills a bucket with 10. x += 5 adds 5 more to the bucket. The bucket now holds 15.
| Operator | Same as | x=10 → Result |
|---|---|---|
| = | Store value | x = 10 |
| += | x = x + 5 | x = 15 |
| -= | x = x - 3 | x = 7 |
| *= | x = x * 2 | x = 20 |
| /= | x = x / 4 | x = 2.5 |
| //= | x = x // 3 | x = 3 |
| %= | x = x % 3 | x = 1 |
| **= | x = x ** 2 | x = 100 |
x = 10 print(x) # 10 x += 5 # same as x = x + 5 print(x) # 15 x *= 2 # same as x = x * 2 print(x) # 30
x first before using x += 1. If x does not exist yet, Python will give an error.
Computers store numbers as binary — a series of 0s and 1s called bits.
Bitwise operators work directly on those bits.
For example, 5 in binary is 0101 and 3 is 0011.
These are used in advanced topics like games, security, and hardware.
x << 1 doubles the number. x >> 1 halves it. So 5 << 1 = 10 and 5 >> 1 = 2.
| Symbol | Name | Example | Result |
|---|---|---|---|
| & | AND | 5 & 3 | 1 |
| | | OR | 5 | 3 | 7 |
| ^ | XOR | 5 ^ 3 | 6 |
| ~ | NOT | ~5 | -6 |
| << | Left Shift | 5 << 1 | 10 |
| >> | Right Shift | 5 >> 1 | 2 |
a = 5 # binary: 0101 b = 3 # binary: 0011 print(a & b) # 1 (AND) print(a | b) # 7 (OR) print(a << 1) # 10 (doubled) print(a >> 1) # 2 (halved)
These check if a value exists inside a collection — like a list, string, or set.
in returns True if the value is found.
not in returns True if the value is NOT found.
Very readable — almost like plain English!
'milk' in shopping_list. Yes → True. No → False.
| Operator | Checks | Example | Result |
|---|---|---|---|
| in | Value IS inside | 'a' in 'cat' | True |
| not in | Value NOT inside | 5 not in [1,2] | True |
fruits = ['apple', 'mango'] print('apple' in fruits) # True print('grape' in fruits) # False print('grape' not in fruits) # True # Works on strings too! print('Py' in 'Python') # True
in checks keys only. Example: 'name' in {'name': 'Ali'} → True.
These check if two variables are pointing to the exact same object in memory.
This is different from ==, which only checks if the values are the same.
is → same object. is not → different objects.
== is True) but they are two different people (is is False). Identity checks if it is literally the same person.
| Operator | Checks | Example | Result |
|---|---|---|---|
| is | Same object in memory | a is b | True/False |
| is not | Different objects | a is not c | True/False |
a = [1, 2, 3] b = a # b points to same list c = [1, 2, 3] # c is a brand new list print(a is b) # True — same object print(a is c) # False — different objects print(a == c) # True — same values x = None print(x is None) # True ✓ correct way
x is None — never x == None. This is the correct Python style (PEP 8 rule).
Comments
Post a Comment