Chapter 08 of 11

Numbers in Python

Numbers are everywhere in programming. We use them to calculate prices, ages, scores, distances, percentages, and much more.

Python makes working with numbers quite easy and supports different types of numeric values.

Types of Numbers in Python

Python mainly provides three numeric data types:

Type

Example

Description

int

25

Whole numbers

float

25.5

Decimal numbers

complex

2 + 3j

Complex numbers

Let's look at each one.

1. Integers (int)

An integer is a whole number without a decimal point.

age = 20
score = 95
temperature = -5

Integers can be positive, negative, or zero.

print(10)
print(-10)
print(0)

Python integers can also represent very large whole numbers without a fixed 32-bit or 64-bit limit like many other languages.

2. Floating-Point Numbers (float)

A float is a number that contains a decimal point.

price = 99.99
height = 5.8
temperature = 36.5

You can perform calculations with floats just like integers:

price = 100.50
quantity = 2

total = price * quantity

print(total)

Output:

201.0

3. Complex Numbers (complex)

Python also supports complex numbers, which contain a real and imaginary part.

number = 2 + 3j

print(number)

Output:

(2+3j)

The j represents the imaginary part.

Complex numbers are mostly useful in scientific and mathematical applications, so you won't need them often when learning basic Python.

Arithmetic with Numbers

Python supports all the common mathematical operations.

a = 10
b = 3

print(a + b)   # Addition
print(a - b)   # Subtraction
print(a * b)   # Multiplication
print(a / b)   # Division

Output:

13
7
30
3.3333333333333335

There are also some useful operators specifically for numbers:

print(10 // 3)  # Floor division
print(10 % 3)   # Remainder
print(10 ** 3)  # Exponentiation

Output:

3
1
1000

Floor Division (//)

// performs division and returns the floor of the result.

print(10 // 3)

Output:

3

Modulus (%)

% gives the remainder after division.

print(10 % 3)

Output:

1

This is particularly useful for checking whether a number is even or odd.

number = 8

print(number % 2)

Output:

0

A remainder of 0 means the number is divisible by 2.

Exponentiation (**)

** is used for powers.

print(2 ** 3)

Output:

8

This means:

2 × 2 × 2 = 8

Working with Large Numbers

Python allows you to write large numbers using underscores to make them easier to read.

population = 1_400_000_000

print(population)

Output:

1400000000

The underscores are ignored by Python.

Converting Numbers

You can convert values between numeric types.

Integer to Float

number = 10
decimal_number = float(number)

print(decimal_number)

Output:

10.0

Float to Integer

number = 10.8
whole_number = int(number)

print(whole_number)

Output:

10

Notice that int() does not round the number. It removes the fractional part.

Checking the Type

You can use type() to check what type of number you're working with.

age = 20
price = 99.99

print(type(age))
print(type(price))

Output:

<class 'int'>
<class 'float'>

A Practical Example

Let's calculate the total price of some products:

price = 250
quantity = 3

total = price * quantity

print("Total:", total)

Output:

Total: 750

You can also calculate a discounted price:

price = 1000
discount = 10

discount_amount = price * discount / 100
final_price = price - discount_amount

print("Final Price:", final_price)

Output:

Final Price: 900.0

So, Python gives you everything you need for working with numbers—from simple calculations to more advanced mathematical operations.