Numbers

Python has integers, floats and complex numbers.

Syntaxn = 42 pi = 3.14159

Python has three numeric types:

  • int — whole numbers of any size: 42, -7.
  • float — numbers with a decimal point: 3.14, 9.99.
  • complex — numbers with an imaginary part, written with j: 2 + 3j.

Unlike many languages, Python integers never overflow — they can be as large as your memory allows.

Example

Example · python
count = 42
price = 9.99
big = 1_000_000
print(count, price, big)
print(2 ** 100)

# Output:
# 42 9.99 1000000
# 1267650600228229401496703205376

When to use it

  • An e-commerce site calculates the total price as a float after applying a percentage discount.
  • A scientific simulation uses large integers to count molecules without overflow errors.
  • A graphics program converts a float pixel coordinate to an integer index for array access.

More examples

Integer and float arithmetic

Multiplying an int by a float produces a float result, which type() confirms.

Example · python
items = 4
price = 2.50
total = items * price
print(type(total), total)

# Output:
# <class 'float'> 10.0

Integer division and remainder

Uses floor division // and modulo % to split minutes into hours and leftover minutes.

Example · python
minutes = 137
hours = minutes // 60
remaining = minutes % 60
print(f'{hours}h {remaining}m')

# Output:
# 2h 17m

Large int and scientific float

Demonstrates underscore separators in large ints and scientific notation floats.

Example · python
population = 8_000_000_000  # underscore for readability
light_speed = 3e8           # scientific notation
distance = 1.5e11
travel_time = distance / light_speed
print(f'~{travel_time:.0f} seconds')

# Output:
# ~500 seconds

Discussion

  • Be the first to comment on this lesson.