Operators, Input-Output & Debugging
Operators in Python
An operator is a symbol that tells Python to do a calculation or comparison on values called operands. Shops, banks and schools use these every day — adding GST, comparing marks, checking eligibility.
Arithmetic operators
price, gst = 1250, 18
tax = price * gst / 100
print('GST =', tax, '| Total =', price + tax)
print(29 // 4, 29 % 4, 2 ** 10, 9 / 2)
minutes = 135
print(minutes // 60, 'h', minutes % 60, 'min')| Operator | Name | Example | Result |
|---|---|---|---|
| + | Addition | 1250 + 225 | 1475 |
| - | Subtraction | 50 - 12 | 38 |
| * | Multiplication | 45.5 * 3 | 136.5 |
| / | Division | 9 / 2 | 4.5 |
| // | Floor division | 29 // 4 | 7 |
| % | Modulus (remainder) | 29 % 4 | 1 |
| ** | Exponent | 2 ** 10 | 1024 |
Relational, logical and membership operators
Relational operators compare values and return True or False. Logical operators combine conditions. Membership operators check if a value is present in a string, list or other collection.
marks, attendance = 72, 68
print(marks >= 33, attendance >= 75)
eligible = marks >= 33 and attendance >= 75
print('Eligible for exam:', eligible)
print(10 > 3 or 5 < 2, not 10 > 3)
print('IP' in 'Informatics Practices', 'ip' in 'Informatics Practices')Note that 'IP' in 'Informatics Practices' is False — the capital letters "IP" do not appear together; membership is case-sensitive.
Assignment operators
stock = 50
stock -= 12 # 12 sold
stock += 30 # 30 arrived
stock *= 2 # second branch has the same
print('Stock =', stock)Expressions and precedence
An expression is a combination of operands and operators that gives one value. When many operators appear, Python follows precedence: brackets first, then **, then * / // %, then + -, then comparisons, then not, and, or. Operators of the same level work left to right, except ** which works right to left.
print(5 + 3 * 2 - 8 / 4) print((5 + 3) * 2) print(2 ** 2 ** 3) print(15 - 6 // 4)
Input and output
input(prompt) reads text typed by the user. print() displays values; use sep and end to control spacing.
name = input('Customer name: ')
amount = float(input('Purchase amount: '))
if amount > 2000:
discount = amount * 10 / 100
else:
discount = amount * 5 / 100
print('Dear', name + ', you pay Rs.', amount - discount)Type conversion
Implicit conversion is automatic (int + float → float). Explicit conversion (type casting) uses int(), float(), str(), bool().
a = '5'
print(a * 3, int(a) * 3) # string repeat vs number multiply
print(int(9.99), float('12'), str(3.0), int('0012'))
print(round(1234.5678, 2), round(1234.5678, -2))c = float(input('Temperature in Celsius: '))
f = c * 9 / 5 + 32
print('Fahrenheit =', round(f, 2))Errors and debugging
Runtime error – program stops
- Dividing by zero
- Using a variable that was never created
- Converting "abc" with int()
- Python shows the error name and line
Logical error – wrong answer
- Program finishes without any message
- Wrong formula or forgotten conversion
- Found only by checking the output
- Hardest to find
total = 450
items = 0
print('Average price:', total / items)A classic logical error — forgetting to convert input. The user types 3, so the "total" should be 120, but the string "3" is repeated 40 times:
qty = input('Quantity: ')
print('Total:', qty * 40) # bug: qty is a string!Fix: qty = int(input('Quantity: ')). Debug by printing variables and their types: print(qty, type(qty)).
Summary & practice
- Arithmetic: + − * / // % **; relational give True/False; logical: and, or, not.
- Precedence: () → ** → * / // % → + − → comparisons → not → and → or.
input()returns str; convert with int()/float().- Errors: syntax, runtime, logical.
1) Evaluate 20 - 3 ** 2 // 4 * 2. 2) Write a program to input the cost price and selling price and print profit or loss amount and percentage. 3) Write a program to convert seconds into hours, minutes and seconds.
Answer 1: 3 ** 2 = 9 → 9 // 4 = 2 → 2 * 2 = 4 → 20 − 4 = 16.
