Operators - Python Basics
Operators in Python are special symbols or keywords that perform operations on variables and values. They are the building blocks of expressions and logic in programming. In this guide, we will explore different types of Python operators with clear examples so you can use them effectively in your projects.
Types of Operators
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Identity Operators
- Membership Operators
- Bitwise Operators
1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical operations such as addition, subtraction, multiplication, division, and more.
Operator | Description | Example |
---|---|---|
+ | Addition | 5 + 3 → 8 |
- | Subtraction | 5 - 3 → 2 |
* | Multiplication | 5 * 3 → 15 |
/ | Division | 5 / 2 → 2.5 |
// | Floor Division | 5 // 2 → 2 |
% | Modulus | 5 % 2 → 1 |
** | Exponentiation | 2 ** 3 → 8 |
# Arithmetic Operators Example
x = 10
y = 3
print(x + y) # 13
print(x - y) # 7
print(x * y) # 30
print(x / y) # 3.333...
print(x // y) # 3
print(x % y) # 1
print(x ** y) # 1000
//
when you want an integer result without decimals, especially in loops and indexes.
2. Assignment Operators
Assignment operators are used to assign values to variables, with options to perform operations simultaneously.
Operator | Description | Example |
---|---|---|
= | Assign value | x = 5 |
+= | Add and assign | x += 3 → x = x + 3 |
-= | Subtract and assign | x -= 3 → x = x - 3 |
*= | Multiply and assign | x *= 3 → x = x * 3 |
/= | Divide and assign | x /= 3 → x = x / 3 |
//= | Floor divide and assign | x //= 3 |
%= | Modulus and assign | x %= 3 |
**= | Exponent and assign | x **= 3 |
# Assignment Operators Example
a = 5
a += 2 # a = 7
a -= 1 # a = 6
a *= 3 # a = 18
a /= 2 # a = 9.0
print(a)
3. Comparison Operators
Comparison operators compare two values and return a Boolean result (True
or False
).
Operator | Description | Example |
---|---|---|
== | Equal to | 5 == 3 → False |
!= | Not equal to | 5 != 3 → True |
> | Greater than | 5 > 3 → True |
< | Less than | 5 < 3 → False |
>= | Greater or equal to | 5 >= 3 → True |
<= | Less or equal to | 5 <= 3 → False |
# Comparison Operators Example
x = 10
y = 20
print(x == y) # False
print(x != y) # True
print(x > y) # False
print(x < y) # True
print(x >= 10) # True
4. Logical Operators
Logical operators are used to combine conditional statements.
and
– Returns True if both statements are true.or
– Returns True if at least one statement is true.not
– Reverses the result, returns False if result is true.
# Logical Operators Example
x = 5
print(x > 3 and x < 10) # True
print(x > 8 or x == 5) # True
print(not(x > 3)) # False
5. Identity Operators
Identity operators compare memory locations, not just values.
is
– Returns True if both variables point to the same object.is not
– Returns True if they are not the same object.
# Identity Operators Example
a = ["apple", "banana"]
b = a
c = ["apple", "banana"]
print(a is b) # True (same object)
print(a is c) # False (different objects)
print(a is not c) # True
6. Membership Operators
Membership operators check if a value exists in a sequence.
in
– Returns True if the value exists.not in
– Returns True if the value does not exist.
# Membership Operators Example
fruits = ["apple", "banana"]
print("apple" in fruits) # True
print("grape" not in fruits) # True
7. Bitwise Operators
Bitwise operators work on binary representations of integers.
Operator | Description | Example |
---|---|---|
& | 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 |
# Bitwise Operators Example
x = 5 # 0101
y = 3 # 0011
print(x & y) # 1 (0001)
print(x | y) # 7 (0111)
print(x ^ y) # 6 (0110)
print(~x) # -6 (two's complement)
print(x << 1) # 10 (1010)
print(x >> 1) # 2 (0010)
Final Thoughts
Understanding Python operators is essential for writing efficient and powerful code. From basic arithmetic to advanced bitwise manipulation, these tools allow you to process data, control logic, and create smarter programs. Practice them with small projects to solidify your skills.
s
0 Comments