All lessons

Hello, Python · step 3 of 22

Numbers & math

8 minWhole numbers, decimals and the seven operators you will use constantly.

Two kinds of numbers

Whole numbers are int (7, -3, 0). Numbers with a decimal point are float (7.5, 0.25). Python chooses the type for you.

slices = 8
price = 4.5

The operators

+ add · - subtract · * multiply · / divide · // whole-number division · % remainder · ** power.

print(7 / 2)    # 3.5
print(7 // 2)   # 3
print(7 % 2)    # 1
print(2 ** 5)   # 32

/ always gives a float. round(value, 2) tidies a float to two decimals, which you will use constantly for money. Multiplication and division happen before addition and subtraction, just like in maths class.

Remember this

  • int = whole number, float = decimal
  • / gives a float, // and % split a division
  • round(value, 2) is your friend for money

Quick quiz

Three questions. 3 correct answers and you have the idea — you can retry as often as you like.

  1. 1. What is 9 // 2?

  2. 2. Which operator gives the remainder?

  3. 3. What does round(3.14159, 2) give?

Answer all three to check them.

Now write it yourself

0/3 passed
1

Practice 1

Use Python as a calculator: print the result of 7 * 6.

practice_1.py
2

Practice 2

A pizza has 8 slices shared equally by 3 friends. Print how many whole slices each friend gets, then how many slices are left over — each on its own line.

practice_2.py
3

Practice 3

A bill of 84.50 with an 18% tip. Print the tip amount and then the total bill, each rounded to 2 decimals, one per line.

practice_3.py

Finish with two of three quiz answers correct and every exercise passing, and this lesson is marked complete.