Tutorials › Python Tutorial › Python Numbers and Casting

Python Numbers and Casting

11 min read · Lesson 6 of 24

Numbers are at the heart of most programs: prices, distances, scores, interest rates. Python has three numeric types and a simple way to convert between types, called casting.

The three number types

  • int – whole numbers, positive or negative, with no decimal point: 7, -40, 2500000.
  • float – numbers with a decimal point: 3.5, -0.25, 98.6.
  • complex – numbers with an imaginary part, written with j: 2+3j. These are mostly used in engineering and science.
Example PYTHON
students = 45
temperature = 23.7
signal = 3 + 4j
print(type(students).__name__)
print(type(temperature).__name__)
print(type(signal).__name__)
Result
int
float
complex
Try it Yourself »

Big numbers

Python integers can be as large as you like. To make big numbers easier to read, you may put underscores between digits. Python ignores them.

Example PYTHON
kenya_population = 52_400_000
budget = 3_900_000_000_000
print(kenya_population)
print(budget * 2)
Result
52400000
7800000000000
Try it Yourself »

Arithmetic operators

OperatorMeaningExampleResult
+addition7 + 29
-subtraction7 - 25
*multiplication7 * 214
/division7 / 23.5
//floor division7 // 23
%remainder (modulus)7 % 21
**power7 ** 249
Example PYTHON
mandazi = 50
per_box = 12
print(mandazi // per_box, "full boxes")
print(mandazi % per_box, "left over")
print(2 ** 10)
print(10 / 4)
Result
4 full boxes
2 left over
1024
2.5
Try it Yourself »
Note: The / operator always gives a float, even when the answer is whole: 10 / 2 is 5.0, not 5. Use // when you want a whole number.

Floats and rounding

Computers store floats in binary, so some decimals cannot be stored exactly. This leads to small surprises:

Example PYTHON
print(0.1 + 0.2)
print(round(0.1 + 0.2, 2))
print(round(1234.5678, 1))
Result
0.30000000000000004
0.3
1234.6
Try it Yourself »

Use round(number, digits) when showing results to people. For serious money calculations, professionals often store amounts in cents (as integers) or use the decimal module.

Useful number functions

Example PYTHON
print(abs(-350))
print(max(120, 450, 80))
print(min(120, 450, 80))
print(pow(3, 4))
print(divmod(17, 5))
Result
350
450
80
81
(3, 2)
Try it Yourself »

divmod(a, b) returns both the floor division and the remainder in one go. For more maths, such as square roots, see the math module in Python Modules.

Casting: converting between types

Casting means turning a value of one type into another type. You do it by calling the type name like a function.

int()

Example PYTHON
print(int(9.99))
print(int("250"))
print(int(-3.7))
Result
9
250
-3
Try it Yourself »

int() on a float simply chops off the decimal part; it does not round.

float()

Example PYTHON
print(float(5))
print(float("12.5"))
print(float("3"))
Result
5.0
12.5
3.0
Try it Yourself »

str()

Example PYTHON
amount = 1500
message = "You paid KES " + str(amount)
print(message)
Result
You paid KES 1500
Try it Yourself »

Without str(), joining text and a number with + would raise a TypeError.

Warning: Casting fails if the text is not a valid number. int("12 bob") and int("3.5") both raise a ValueError. You will learn to catch such errors in Python Try...Except.

A practical example

Here we calculate a phone bill with 16% VAT, starting from values that arrived as text:

Example PYTHON
minutes_text = "135"
rate_text = "4.3"
minutes = int(minutes_text)
rate = float(rate_text)
subtotal = minutes * rate
vat = subtotal * 0.16
total = subtotal + vat
print("Subtotal:", round(subtotal, 2))
print("VAT:", round(vat, 2))
print("Total:", round(total, 2))
Result
Subtotal: 580.5
VAT: 92.88
Total: 673.38
Try it Yourself »

Exercises

Exercise

Use the operator that gives the remainder of 17 divided by 5.

print(17  5)
Exercise

Convert the text "42" into an integer.

age = ("42")

Next: Python Strings.

Sign in or create a free account to track progress, earn XP, badges and a certificate.