Python lesson 11
Numbers & Arithmetic
Use Python as a calculator. Add, subtract, divide, and more!
Python Handles Two Kinds of Numbers
Integers (int) are whole numbers like 5 or -3. Floats are decimals like 3.14. The basic operators are + (add), - (subtract), * (multiply), and / (divide). Dividing always gives a float. Two extra operators: // gives you the integer part of a division (floor division), and % gives you the remainder (modulo).
10 // 3 # → 3 (whole part only)
10 % 3 # → 1 (what is left over)
print(10 // 3)
print(10 % 3)
Two Kinds of Divide
print(7 / 2)
print(7 // 2)
print(7 % 2)
The Four Basic Operators
Click Run to see addition, subtraction, multiplication, and division all at once.
Hint: Click Run! Notice that 10 / 3 gives a decimal even though both numbers are integers.
a = 10
b = 3
print("Add:", a + b)
print("Subtract:", a - b)
print("Multiply:", a * b)
print("Divide:", a / b)
Packing Boxes
You have 25 items and each box holds 4. Use // to find the number of full boxes and % to find the leftovers. Run the code to see the result!
Hint: // gives the whole-number part of the division. % gives what is left over after filling complete boxes.
items = 25
box_size = 4
full_boxes = items // box_size
leftover = items % box_size
print("Full boxes:", full_boxes)
print("Leftover items:", leftover)
Area and Perimeter
Calculate the area and perimeter of a rectangle. Then try changing the length and width and run again to see different results!
Hint: Area = length x width. Perimeter = 2 x (length + width). Change the numbers and observe how the results shift!
length = 8
width = 5
area = length * width
perimeter = 2 * (length + width)
print("Area:", area)
print("Perimeter:", perimeter)
What you can do on CodeIt
Run the example, change one part, and use the result to check your understanding before moving to the next lesson.