On my fourth day of learning Python, I explored operators, the symbols that let us perform calculations, compare values, and work with logic. Operators are the backbone of programming because they allow us to manipulate data and make decisions.
1. đź§® Arithmetic Operators
Arithmetic operators are used for basic math operations.
These are the same operations you use in everyday math, but now you can automate them with code.
a = 10
b = 3
print("Addition:", a + b) # 13
print("Subtraction:", a - b) # 7
print("Multiplication:", a * b) # 30
print("Division:", a / b) # 3.333...
print("Integer division:", a // b) # 3
print("Remainder:", a % b) # 1
print("Power:", a ** b) # 1000
- + adds values
- - subtracts
- * multiplies
- / divides (returns a float)
- // divides but returns only the whole number part
- % gives the remainder
- ** raises a number to a power
2. 🔍 Comparison Operators
Comparison operators check relationships between values. They return either True or False.
print("Equal?", a == b) # False
print("Not equal?", a != b) # True
print("Greater than?", a > b) # True
print("Less than or equal?", a <= b) # False
- == checks if two values are equal
- != checks if they are not equal
- > checks if one is greater
- < checks if one is smaller
- >= and <= check greater/less than or equal
These are essential for decision‑making in programs, like checking if a user’s score is high enough to pass.
3. âś… Logical Operators
Logical operators combine or invert conditions.
x = True
y = False
print("x and y:", x and y) # False
print("x or y:", x or y) # True
print("not x:", not x) # False
- and returns True only if both conditions are true
- or returns True if at least one condition is true
- not flips the value (True becomes False, False becomes True)
Logical operators are the glue that connects multiple conditions together. For example, you might check if a user is logged in and has admin rights before allowing access.
🎯My Take
Operators are the tools that let Python do the heavy lifting:
- Arithmetic operators handle calculations.
- Comparison operators let us check relationships.
- Logical operators help us combine conditions.
Together, they make Python powerful enough to handle everything from simple math to complex decision‑making.
Top comments (0)