Legends ofPythos
Claim your name
Python Basics

Numbers and arithmetic

Lesson 5 of 13

Watch the lesson1:06 ยท with Sigrid
Python makes math straightforward with operators. You already know + for addition. Here are its siblings: - subtracts, * multiplies, and / divides. These symbols work exactly as you expect from school.
print(10 + 5)
print(10 - 3)
print(4 * 2)
print(9 / 2)
Basic arithmetic operations
Look closely at the last line. 9 / 2 gives you 4.5. In Python, / always returns a float (a number with decimals), even if the result looks like a whole number. This behavior is consistent and predictable.
Sometimes you want only the whole part of a division or the leftover remainder. Use // for integer division and % for modulo. Integer division rounds down to the whole number below, so 7 // 2 is 3. Modulo gives you what is left over after making full groups.
# 7 // 2: how many full groups of 2 in 7?
print(7 // 2)

# 7 % 2: what is left over after making those groups?
print(7 % 2)
Integer division and remainder
You will use // when packing items into boxes. For example, if you have 17 items and boxes hold 6, integer division tells you how many full boxes fit. You can also use % to check parity: if a number divided by 2 has no remainder, it is even.

Exponentiation

The ** operator raises a number to a power. For example, 2 ** 3 means three twos multiplied together: 2 * 2 * 2, which is 8. It reads as "two to the third".
print(5 ** 2)
print(10 ** 1)
Exponentiation

Order of Operations

Python follows standard math rules. Multiplication and division happen before addition and subtraction, just like in school. If you need to change this order, use parentheses ().
# Python does multiplication first: (3 * 4) + 2
print(2 + 3 * 4)

# Parentheses force addition first: (2 + 3) * 4
print((2 + 3) * 4)
Using parentheses to control order

Converting and Rounding

You can switch between integers (int) and floats using int() and float(). Use round() to clean up messy decimals. This is useful when you need a whole number for display.
# Convert float to int (cuts off the decimal)
print(int(9.8))

# Round to the nearest whole number
print(round(1.567))
Type conversion and rounding

Your turn

0 of 3 solved

Exercise 1

+25 XP
Store the width 8 in a variable called w and the height 5 in a variable called h. Multiply them together with * and print the area.
Write your Python here.

Run your code to check it against the tests.

Exercise 2

+25 XP
You have total (17) items and want to pack them into boxes of size size (6). How many full boxes can you fill? Use integer division (//) and print the result.
Write your Python here.

Run your code to check it against the tests.

Exercise 3

+25 XP
Take a number n (1.978) and round it to the nearest integer using round(). Print the result.
Write your Python here.

Run your code to check it against the tests.