Search⌘ K
AI Features

Types: Numeric and Boolean

Discover how Python handles numeric types, including integers with arbitrary precision and floating-point numbers with potential precision issues. Learn to use the round() function to manage floating-point approximations and understand the Boolean type for binary logic. This lesson equips you with foundational skills to accurately manage numbers and logic essential for decision-making in Python programming.

Whether we are calculating the trajectory of a rocket or simply counting the items in a shopping cart, numbers are the bedrock of programming. Python makes working with numbers remarkably intuitive because it handles the messy details of memory and storage for us. We simply type a number, and Python decides how to store it. In this lesson, we will explore how Python represents whole numbers and decimals, and uncover an unexpected detail in how computers handle math.

Numeric types

Integers (int)

An integer is a whole number without a fractional part. In many other programming languages, integers have a fixed size (e.g., they can only hold numbers up to a certain limit, like 2 billion). In Python, integers have arbitrary precision. This means they can be as large as our computer's memory allows, making Python excellent for heavy mathematical computation.

When writing large numbers, it can be difficult to count the zeros. Python allows us to use underscores (_) as visual separators. These underscores are ignored by the interpreter but make code much easier for humans to read.

Python 3.14.0
# Defining integers
population = 7_900_000_000 # 7.9 billion
count = -5
large_math = 2 ** 100 # 2 to the power of 100
print(population)
print(count)
print(large_math)
  • Line 2: We improve code readability by using underscores as visual separators for large numbers. The Python interpreter ignores them completely, treating this exactly like 7900000000.

  • Line 3: Python integers are signed, ...