🎓 A1Learner
🔍
0 of 116 lessons completed

Python Numbers

Python numbers come in three types. The types are int, float and complex.

Whole numbers

An int counts whole things. It can be zero or below zero.

apples = 12
below_zero = -5
print(apples)
print(below_zero)

Reading big numbers

Underscores are only for your eyes. Python ignores them in the value.

stars = 1_000_000
print(stars)
print(type(stars))

Numbers with a point

A float holds a number with a point. Prices are a good everyday picture.

price = 9.99
print(price)
print(type(price))

The e shortcut

The e means times ten to a power. So 3e-2 is a very small float.

tiny = 3e-2
big = 1.5e3
print(tiny)
print(big)

A first look at complex

A complex number has two parts. The letter j marks the second part, called imag. You read the parts with .real and .imag.

z = 3 + 5j
print(z.real)
print(z.imag)

Changing a number's type

The casting lesson introduced int() and float(). They work on numbers too.

whole = int(7.9)
point = float(4)
print(whole)
print(point)

Example

width = 6
length = 4.5
area = width * length
print("Area:", area)
print(type(width))
print(type(length))
print(type(area))

Key points

  • Int: a whole number with no point, like 4 or -7
  • Float: a number with a point, like 4.5 or 0.25
  • Complex: a number with a real part and an imag part
  • Use underscores in long whole numbers to read them easily, like 1_000_000
  • Use e notation for very small or very large floats, like 3e-2
  • Use int() and float() to convert a number to a new type

In this example, width is the int 6. length is the float 4.5. area holds width times length. Python makes the answer a float, so it prints as 27.0. The last three lines print the type of each value. You can see the two starting types and the float answer.

Note

In Python, an int can grow as big as you need.

It does not overflow like whole numbers in many other languages.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

What is the type of 2.5?