githubEdit

Numbers / Integers

Numbers are the foundation of every programming language. In this module, we will learn how to use numbers in Python.

Python has three main numeric types:

  • int for whole numbers (e.g. 2, -5)

  • float for decimal numbers (e.g. 3.14, -0.5)

  • complex for complex numbers (e.g. 2+3j)

bool is a subtype of int where False behaves like 0 and True behaves like 1.

Integers (int)

Integers are whole numbers of unlimited length.

positive_int = 2
negative_int = -2
zero = 0

Booleans (bool)

Booleans represent truth values: True and False.

is_raining = True
is_cloudy = False

Floats (float)

Floats are numbers with decimals.

Complex Numbers (complex)

Complex numbers have a real and imaginary part.

Common Operations

Useful Built-ins

Common Pitfalls

  • 0.1 + 0.2 is not exactly 0.3 because of floating-point precision.

  • Dividing two integers with / always returns a float.

References

Python Documentationarrow-up-right

W3Schoolsarrow-up-right

Nextarrow-up-right | Previous

Last updated