Integers and floats

Numbers & Math, lesson 1 of 5

🔢 Numbers & Math, lesson 1 of 5

Integers and floats

Python's two kinds of numbers and the basic arithmetic operators.

8 min

2 exercises

2 quizzes

0/4 solved

Getting Python ready… examples can run in a moment.

Python has two main kinds of numbers:

  • int (integer): whole numbers like 7, 0 or -42
  • float (floating point): numbers with a decimal point like 3.14 or 2.0

You can do math with the usual operators: + add, - subtract, * multiply and / divide.

Example

A pocket calculator

Python is a great calculator. Try your own sums, like 365 * 24.

Notice that / always gives a float, even when the result is a whole number: 10 / 2 is 5.0. And any math that involves a float produces a float.

Integers can be as big as you like. For readability you may write underscores in long numbers: 1_000_000 is one million.

Example

Types of results

** means "to the power of". Python handles huge integers without breaking a sweat.

Quiz

What does this print?

Quiz

What does this print?

Exercise

Snack run

A sandwich costs 3.5 and you buy 4 of them. Compute the cost and store it in total.

Use the price and quantity variables rather than typing the answer.

Exercise

Test average

Compute the average of the three scores (add them up, then divide by 3) and store it in average.