Search⌘ K
AI Features

Data Types: Numeric and Boolean

Explore Python's numeric and boolean data types by understanding how integers and floats are stored and manipulated. Learn about floating-point precision challenges and how to use booleans for logical operations. This lesson equips you to handle Python's core data types accurately for better program decisions.

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 fundamental Python data types, learning 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). As one of the most powerful Python data types, integers here 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, meaning they can represent both positive and negative whole numbers natively.

  • Line 4: We utilize Python's arbitrary precision feature. Unlike many other languages that crash if a number gets too big (overflow), Python automatically expands the memory for this integer to hold the massive result of 21002^{100}.

  • Line 6: When printing, Python displays the actual value stored in memory, confirming that the underscores were purely for our convenience in the code.

  • Line 7: We output the count variable to verify that Python correctly stored the negative value. This demonstrates that the integer type natively handles signed numbers, preserving the negative state as a fundamental property of the data rather than just a display format.

  • Line 8: The output proves that Python successfully calculated and stored a number far exceeding the typical limits of standard computer integers.

Floating-point numbers (float)

Any number containing a decimal point is a floating-point number. Even if the decimal part is zero, e.g., 1.0, Python treats it as a float, not an int. This distinction matters because floats are stored differently in memory to handle fractional precision.

We can also define floats using scientific notation with e or E. This is useful for representing very small or very large numbers concisely.

Python 3.14.0
# Defining floats
pi_approx = 3.14159
exact_float = 10.0 # Looks like an int, but the decimal makes it a float
scientific = 2.5e-3 # 2.5 times 10 to the power of -3
print(pi_approx)
print(exact_float)
print(scientific)
  • Line 2: We define a standard float using a decimal point.

  • Line 3: By adding .0, we explicitly tell Python to treat this 10 as a float.

  • Line 4: 2.5e-3 is shorthand for 2.5×1032.5 \times 10^{-3}, which evaluates to 0.0025.

  • Lines 6–7: We print the standard floats to verify their values.

  • Line 8: Python prints scientific notation in standard decimal form when reasonable. For extremely tiny or huge numbers (like 1e-10), it remains in scientific notation to ensure readability.

The floating-point precision trap

Floating-point numbers in computers are approximations. While we typically count in base-10 (decimal), computers store numbers in base-2 (binary). Some simple decimals, like 0.1, cannot be represented perfectly in binary fractions, just as 1/3 cannot be represented perfectly in decimal (0.3333...). This leads to tiny precision errors that can cause logical bugs if we expect exact equality. For example, look at the code below:

Python 3.14.0
# The precision problem
result = 0.1 + 0.2
print(result)
  • Line 2: We attempt a simple addition that reveals the binary approximation model. Because computers represent numbers in base-2 (binary) rather than base-10, they cannot perfectly represent certain fractions like 0.1 or 0.2.

  • Line 4: The output of 0.30000000000000004 is the result of these tiny "rounding errors" accumulating during the calculation. This confirms that while floating-point math is highly efficient, it is not always perfectly precise for decimal-sensitive logic.

Such approximations can fail our logic, if ignored.

Managing precision with round()

To handle these approximations, we use the round(number, ndigits) function. This rounds the float to the specified number of decimal places.

Python 3.14.0
raw_sum = 0.1 + 0.2
rounded_sum = round(raw_sum, 1) # The precision problem
print(rounded_sum)
  • Line 2: We round the result to 1 decimal place, which converts 0.300...04 to 0.3.

From approximation to certainty: Booleans (bool)

We have seen that floating-point numbers can sometimes be "fuzzy" or approximate. However, much of programming requires absolute precision. A feature is either on or off; a user is either logged in or logged out; a password is correct or incorrect. To handle these binary states, Python uses a specific data type that allows for no gray areas.

As one of the core Python data types, the bool type has exactly two possible values: True and False. While floats measure quantity (how much?), booleans measure truth (is it so?).

These are not strings; they are special keywords built into the language. A critical detail for beginners is capitalization. Python is case-sensitive, so True (capital T) is a valid boolean value, while true (lowercase t) is treated as an unknown variable name, which will cause an error.

We can assign booleans to variables just like numbers or strings, and we can inspect their type to confirm they are distinct from text.

Python 3.14.0
# Defining boolean variables
is_active = True
is_loading = False
print(is_active)
print(is_loading)
print(type(is_active))
  • Lines 2–3: We establish binary states using the unique True and False keywords. By omitting quotation marks, we ensure Python treats these as logical values rather than literal text strings.

  • Lines 5–6: When we output these variables, Python displays the raw state of the logic. This confirms that the program now holds a definitive "Yes" or "No" signal that can be used for future decision-making.

  • Line 7: Using type() reveals that these objects belong to the bool class. This confirms they have their own specific behaviors and rules that are entirely separate from text or numeric Python data types.

We have now established the foundations of data and logic in Python. We explored several fundamental Python data types, observing how the language distinguishes between exact integers and approximate floating-point numbers, learning to manage precision errors when math gets messy. We also mastered the rules of logic, creating boolean values through comparisons and combining them to express complex conditions. These skills, accurate calculation and truth evaluation, are the prerequisites for our next major leap: teaching our programs to make decisions based on this data.