In this tutorial, you'll learn how to use numbers in Python programs. We'll focus on integers and floats, the two most common numeric types used by beginners.

Integers

An integer is a whole number (positive or negative), such as:

-3, -1, 0, 1, 2, 100

In Python, the type of an integer is int.

You can use basic mathematical operators to work with integers:

x = 20
y = 10

# Addition
total = x + y
print(total)  # 30

# Subtraction
difference = x - y
print(difference)  # 10

# Multiplication
product = x * y
print(product)  # 200

# Division
quotient = x / y
print(quotient)  # 2.0

Exponentiation

Use ** to calculate exponents:

x = 3
y = 3
power = x ** y
print(power)  # 27

Order of Operations

Use parentheses () to control the order of evaluation:

result = 20 / (10 + 10)
print(result)  # 1.0

Floats

A float is a number that has a decimal point:

0.5, 3.14, -2.75

The term "float" means that the decimal point can appear anywhere in the number.

You can use floats just like integers:

x = 0.5
y = 0.25

total = x + y
print(total)        # 0.75

difference = x - y
print(difference)   # 0.25

product = x * y
print(product)      # 0.125

quotient = x / y
print(quotient)     # 2.0

Division Always Returns a Float

Even when dividing two integers:

x = 20
y = 10
quotient = x / y
print(quotient)  # 2.0

Mixing Types

If you mix an integer and a float, the result will always be a float:

x = 1
y = 2.0
total = x + y
print(total)  # 3.0

Floating-Point Precision

Due to how computers store float values, sometimes results aren't exact:

x = 0.1
y = 0.2
total = x + y
print(total)  # 0.30000000000000004

This is normal and expected in most programming languages.

Underscores in Numbers

For readability, you can use underscores in large numbers:

# Without underscores
count = 10000000000

# With underscores
count = 10_000_000_000
print(count)  # 10000000000

Python ignores the underscores when storing and displaying the values. This also works with floats:

pi = 3.141_592_653
print(pi)  # 3.141592653

Underscores in numbers were introduced in Python 3.6.

Exercises

Exercise 1

Create two integer variables, add them, subtract them, multiply them, and divide them.

a = 15
b = 5
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)

Exercise 2

Calculate 4 to the power of 3 using the exponentiation operator.

print(4 ** 3)

Exercise 3

Use parentheses to calculate this expression: 100 - 5 * (2 + 3).

result = 100 - 5 * (2 + 3)
print(result)

Exercise 4

Create two float variables and perform all four arithmetic operations.

x = 1.5
y = 0.5
print(x + y)
print(x - y)
print(x * y)
print(x / y)

Exercise 5

Create a large number using underscores for readability.

population = 8_000_000_000
print(population)

Summary

  • Python supports integers (int) and floats (float).

  • Use +, -, *, /, and ** to perform arithmetic.

  • Division always returns a float.

  • Mixing int and float results in a float.

  • Be aware of floating-point precision issues.

  • Use underscores _ to format large numbers.

  • Practice regularly to understand how numeric operations behave in different scenarios.