Numbers and Arithmetic
Learning Objectives
- You can distinguish between integers and floating-point numbers in Python.
- You can write arithmetic expressions using
+,-,*,/,//,%, and**. - You can convert text input into a number using
int(...)orfloat(...). - You can predict what happens when strings and numbers are mixed inappropriately.
So far, the values your programs have handled have mostly been text. Strings are fine for greetings and names, but as soon as the program starts to count minutes, sum sessions, or compute averages, it needs to work with numbers. This chapter is about how Python represents numbers and how to do arithmetic with them.
Two Kinds of Numbers
Python has two main number types in the kinds of programs you will write this part.
An integer, written without a decimal point, represents a whole number.
minutes = 42year = 2026score = -3A float, written with a decimal point, represents a number with a fractional part.
height = 1.78ratio = 0.5distance = 12.0The two types look similar in many situations, but they are not the same. 5 and 5.0 print as different things and behave slightly differently in arithmetic.
Floats are how Python stores numbers that may have a fractional part. The name comes from “floating-point,” which refers to how the computer represents the number internally. For everyday programs, the main thing to know is that floats can hold decimal values and integers cannot.
Integer or float
0 / 5 points
Which value is a float in Python?
Arithmetic Operators
Python uses familiar symbols for arithmetic, plus a few that might be new.
| Operator | Meaning | Example |
|---|---|---|
+ | addition | 3 + 2 → 5 |
- | subtraction | 7 - 4 → 3 |
* | multiplication | 6 * 5 → 30 |
/ | division (always a float) | 7 / 2 → 3.5 |
// | floor division (drops the remainder) | 7 // 2 → 3 |
% | remainder (modulo) | 7 % 2 → 1 |
** | exponentiation | 2 ** 10 → 1024 |
Most early programs only need the first three or four. The remainder operator (%) is unusually useful once you start grouping things — for example, “is this number even?” is number % 2 == 0.
price = 8quantity = 3total = price * quantityprint(total)This program prints 24. Expressions can be combined, and Python follows the ordinary rules of precedence: multiplication before addition, parentheses to group.
total = (10 + 5) * 2print(total)This prints 30, not 20.
Total pages
0 / 10 points
Ask the user how many pages they read per day and how many days they studied.
Convert both answers to integers, multiply them, and print the total number of pages.
Example input:
125Required output text:
60Division Has Two Versions
Division behaves slightly differently from the other operators.
7 / 2is3.5(a float, even though both inputs are integers).7 // 2is3(the integer part, the remainder dropped).7 % 2is1(the remainder that was dropped).
This split surprises many beginners. The short version is: / always gives you the mathematical answer (possibly with a decimal), while // gives you the floor (rounded toward negative infinity). Most everyday programs use /.
Division operators
0 / 5 points
What are the values of 7 / 2, 7 // 2, and 7 % 2?
Strings and Numbers Are Not the Same
This was already mentioned in the Print and Input chapter, and it returns here because it is one of the most common sources of confusion.
These two lines look almost identical:
value = "5"value = 5The first stores the text "5". The second stores the number 5. They behave very differently.
print("5" + "3")print(5 + 3)The first prints 53 — concatenation. The second prints 8 — addition. Python does not guess. The operator + means “concatenate” when both operands are strings, and “add” when both are numbers. If one is a string and one is a number, Python raises an error rather than guessing what you wanted.
print("5" + 3)# TypeError: can only concatenate str (not "int") to strThis is a useful error to recognize, because the value coming back from input(...) is always a string.
Arithmetics and natural-language programming
0 / 5 points
Drag and drop the natural-language programming steps to the area marked with “Program”. Arrange the steps so that the program first prints the value 7 and then the value 13.
Converting Input to Numbers
Because input(...) returns text, you need to convert it before doing arithmetic on it.
The two main conversion functions are:
int(...)converts a string of digits into an integer.float(...)converts a string of digits (possibly with a decimal point) into a float.
minutes_text = input("How many minutes did you study? ")minutes = int(minutes_text)print(f"That is {minutes / 60} hours.")A common shorthand is to do the conversion inline, on the same line as the input:
minutes = int(input("How many minutes did you study? "))This is fine, and you will see it in many places. The downside is that if the user types something that is not an integer — for example, forty-five — the program crashes with a ValueError. Later in the course you will learn how to handle that more gracefully. For now, you can assume the user gives a valid answer.
Minutes to hours
0 / 10 points
Ask the user how many minutes they studied.
Convert the answer to an integer, compute the number of hours, and print the result.
Example input:
90Required output text:
1.5Mixing Numbers in Output
In an f-string, Python automatically converts numbers to text when it inserts them.
minutes = 42print(f"You studied {minutes} minutes today.")This prints You studied 42 minutes today. The integer is converted to the text 42 as part of building the message. The same is true for floats:
hours = 1.5print(f"That is {hours} hours.")This is one reason f-strings tend to be more comfortable than + for building messages: they handle the conversion for you, so the operator-mismatch error from the previous section never comes up.
If you want to control how many decimal places a float shows, you can add a format specifier inside the braces:
average = 7 / 3print(f"Average: {average:.2f}")This prints Average: 2.33. The :.2f part means “format as a float with 2 decimal places.” You will see more format specifiers later. For now, this one alone covers most cases where the default looks ugly.
Format an average
0 / 10 points
Ask for total study minutes and number of sessions.
Convert both answers to integers, compute the average minutes per session, and print the average with two decimal places.
Example input:
1003Required output text:
33.33A Small Example: Weekly Study Summary
To bring this together, here is a small program that asks the user how many minutes they studied this week and across how many sessions, then prints a summary including the average session length:
total_minutes = int(input("How many minutes did you study this week? "))sessions = int(input("Across how many sessions? "))
total_hours = total_minutes / 60average_minutes = total_minutes / sessions
print(f"You studied {total_minutes} minutes across {sessions} sessions.")print(f"That is {total_hours:.1f} hours total, or {average_minutes:.0f} minutes per session on average.")If the user types 240 and 5, this program prints:
You studied 240 minutes across 5 sessions.That is 4.0 hours total, or 48 minutes per session on average.Read the program line by line and notice where the types switch from string to integer to float, and where each switch happens.
Check Your Understanding
- What is the difference between
7 / 2and7 // 2in Python? - Why does
int(input(...))exist? What problem is it solving? - What is the output of
print("3" + "4"), and why is it not7? - What does
f"{value:.2f}"do whenvalueis1.234567?