Input & Arithmetic Operations
Programs become interactive when they accept data from the user. Python's
input() function reads keyboard input, and arithmetic operators let you perform
calculations. Together, they form the foundation of every useful program.
Learning Objectives
- 11.1.1.5 Organize keyboard inputs
- 11.1.1.6 Use arithmetic operations when solving problems
Conceptual Anchor
The Calculator Analogy
A program is like a smart calculator. The input() function is the
keyboard — it accepts what you type. Arithmetic operators (+,
-, *, /) are the buttons. And
print() is the display that shows the result. But unlike a regular
calculator, you must tell Python the type of number you're entering (integer or decimal).
Rules & Theory
The input() Function
# input() always returns a STRING
name = input("Enter your name: ") # Returns str
age = input("Enter your age: ") # Returns str ("16", not 16)
# Converting input to numbers
age = int(input("Enter your age: ")) # str → int
price = float(input("Enter the price: ")) # str → float
# Multiple inputs on separate lines
x = int(input("x = "))
y = int(input("y = "))Arithmetic Operators
| Operator | Name | Example | Result |
|---|---|---|---|
+ |
Addition | 7 + 3 |
10 |
- |
Subtraction | 7 - 3 |
4 |
* |
Multiplication | 7 * 3 |
21 |
/ |
Division (float) | 7 / 3 |
2.333... |
// |
Integer division (floor) | 7 // 3 |
2 |
% |
Modulo (remainder) | 7 % 3 |
1 |
** |
Exponentiation (power) | 2 ** 3 |
8 |
Operator Precedence (PEMDAS)
| Priority | Operators | Description |
|---|---|---|
| 1 (highest) | () |
Parentheses |
| 2 | ** |
Exponentiation |
| 3 | * / // % |
Multiplication, division, floor div, modulo |
| 4 (lowest) | + - |
Addition, subtraction |
/ vs // vs %
17 / 5 = 3.4 (exact division). 17 // 5 = 3
(whole part only). 17 % 5 = 2 (remainder). These three operators are
tested frequently in exams!
Worked Examples
1 Simple Calculator
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
print("Sum:", a + b)
print("Difference:", a - b)
print("Product:", a * b)
print("Quotient:", a / b)
# Input: 10, 3
# Sum: 13.0
# Difference: 7.0
# Product: 30.0
# Quotient: 3.33333333333333352 Using // and % — Time Converter
# Convert total minutes to hours and minutes
total_minutes = int(input("Enter minutes: ")) # e.g., 135
hours = total_minutes // 60 # 135 // 60 = 2
minutes = total_minutes % 60 # 135 % 60 = 15
print(total_minutes, "minutes =", hours, "h", minutes, "min")
# 135 minutes = 2 h 15 min3 Operator Precedence
# Without parentheses — follows PEMDAS
result = 2 + 3 * 4 # 3*4=12, then 2+12=14
print(result) # 14
# With parentheses — override precedence
result = (2 + 3) * 4 # 2+3=5, then 5*4=20
print(result) # 20
# Exponentiation before multiplication
result = 2 * 3 ** 2 # 3**2=9, then 2*9=18
print(result) # 184 Extracting Digits with %
# Extract the last digit of any number
number = int(input("Enter a number: ")) # e.g., 347
last_digit = number % 10 # 347 % 10 = 7
print("Last digit:", last_digit)
# Check if the number is even or odd
if number % 2 == 0:
print("Even")
else:
print("Odd")Pitfalls & Common Errors
Division by Zero
x / 0 or x // 0 or x % 0 — all cause a
ZeroDivisionError. Always validate that the divisor is not zero before dividing.
Forgetting to Convert Input
If you write x = input("Number: ") and then x + 5, you'll get a
TypeError because you can't add a string to an integer. Always use
int() or float() on input.
Negative Floor Division
-7 // 2 = -4, not -3! Floor division rounds
down (toward negative infinity), not toward zero.
Pro-Tips for Exams
Must-Know for Exams
//and%are the most commonly tested operators- To test if a number is even:
n % 2 == 0 - To get the last digit:
n % 10 - To remove the last digit:
n // 10 - Always apply PEMDAS when tracing expressions
Graded Tasks
List all 7 arithmetic operators in Python and give one example of each.
Explain the difference between /, //, and % using the
example 17 divided by 5.
Write a program that reads the radius of a circle and outputs its area (π × r²). Use
3.14159 for π.
Write a program that converts a total number of seconds into hours, minutes, and remaining
seconds using // and %.
Without running, evaluate: 2 + 3 * 4 ** 2 - 10 // 3. Show step-by-step working.
Create a program that asks for 3 test scores and calculates the average. Display the result rounded to 1 decimal place.
Self-Check Quiz
input() always return?17 // 5?17 % 5?2 + 3 * 4?-7 // 2?