Basic Operators
Operators are special symbols that perform operations on variables and values. Python supports various kinds of operators.
Each type of operator has a specific use. Arithmetic operators are used for everyday mathematical calculations. Comparison operators produce boolean values (True/False) and are essential for decision making with if. Logical operators combine multiple conditions — for example, checking whether two requirements are met at the same time with and.
Membership operators (in, not in) are very practical for checking whether a value exists in a collection such as a list, tuple, or string. Identity operators (is, is not) are different from ==; is compares object identity in memory, not their values. These operators are useful when working with None or other singletons.
Operator Types:
- Arithmetic:
+ - * / // % ** - Comparison:
== != > < >= <= - Assignment:
= += -= *= /= //= %= **= - Logical:
and or not - Membership:
in not in - Identity:
is is not
Code Example
# Arithmetic Operators
a, b = 17, 5
print(a + b) # 22 (addition)
print(a - b) # 12 (subtraction)
print(a * b) # 85 (multiplication)
print(a / b) # 3.4 (division)
print(a // b) # 3 (floor division)
print(a % b) # 2 (modulo/remainder)
print(a ** b) # 1419857 (exponent)
# Comparison Operators
print(a == b) # False
print(a > b) # True
# Logical Operators
print(True and False) # False
print(True or False) # True
Code Explanation
The two variables a = 17 and b = 5 are used as operands for all the arithmetic operators. The +, -, *, and / operators work like ordinary math. The // operator (floor division) divides and rounds down — 17 // 5 = 3. The % operator (modulo) returns the remainder of a division — 17 % 5 = 2. The ** operator raises a number to a power — 17 ** 5. The comparison operator == checks value equality, producing False because 17 is not equal to 5. The > operator produces True. The logical operator and produces True only if both operands are True; or produces True if either operand is True.
Everyday Example
You are building a cash register program for a bakery. Calculate the total price, apply a discount if the purchase is above $100,000, and check whether the payment is sufficient:
# Item prices
bread = 15000
milk = 8000
coffee = 12000
# Calculate the total
bread_count = 4
milk_count = 2
coffee_count = 3
total = (bread * bread_count) + (milk * 2) + (coffee * coffee_count)
print(f"Shopping total: ${total}")
# 10% discount if total > 100000
if total > 100000:
discount = total * 0.1
total -= discount
print(f"Discount: ${int(discount)}")
print(f"Total after discount: ${int(total)}")
# Payment
paid = 150000
change = paid - total
print(f"Amount paid: ${paid}")
if change >= 0:
print(f"Change: ${int(change)}")
else:
print(f"Insufficient funds, short by ${abs(int(change))}")
Summary
Operators are the foundation of programming logic. By mastering arithmetic, comparison, and logical operators, you can build programs that make decisions based on data. Keep practicing with real-world cases to strengthen your understanding.