Numbers: From Basics to Real Usage

Introduction

In this chapter, you will learn how numbers work in Python, including whole numbers, decimals, and common numeric operations. Numbers are the foundation of calculations, scoring systems, prices, and data analysis. Once you understand this chapter, your code can handle real-world math with much more confidence.

Prerequisites

  • Python 3.10+ installed
  • Basic understanding of variables and operators
  • Ability to run .py files in terminal or IDE

What Are Numbers in Python

In Python, numbers are values you can calculate with.

The most common numeric types for beginners are:

  • int: whole numbers like 10, -3, 0
  • float: decimal numbers like 3.14, -0.5

Quick example:

python
# Integer value
student_count = 30
 
# Floating-point value
average_score = 88.5
 
# Print values
print(student_count)
print(average_score)

1) Integer (int)

Use int when you need whole numbers.

Common cases:

  • Number of users
  • Number of items in cart
  • Retry count
python
# Store whole numbers
apples = 12
days_left = 7
 
# Integer math
total = apples + days_left
print(total)  # 19

2) Floating-Point (float)

Use float for decimal values.

Common cases:

  • Prices
  • Temperature
  • Measurement values
python
# Store decimal numbers
price = 19.99
discount = 2.5
 
# Float math
final_price = price - discount
print(final_price)  # 17.49

Tip

When to Choose int vs float

If decimals matter, use float.
If values are always whole numbers, use int.

3) Type Conversion: int() and float()

User input is text by default, so conversion is often required.

python
# Text input from user
user_age_text = input("Enter your age: ")
 
# Convert text to int
user_age = int(user_age_text)
 
# Use numeric value
print(user_age + 1)

Convert decimal text:

python
# Text input for a decimal number
weight_text = input("Enter your weight: ")
 
# Convert to float
weight = float(weight_text)
 
# Print as number
print(weight)

Warning

int("3.5") will fail with ValueError.
If the value may contain decimals, convert with float() first.

4) Useful Number Operations

You already know basic operators. Here are beginner-friendly patterns used often.

Absolute Value

abs() returns distance from zero.

python
# Convert negative score delta to positive distance
delta = -12
print(abs(delta))  # 12

Rounding

round() helps control decimal display.

python
# Rounded to two decimal places
pi_value = 3.1415926
print(round(pi_value, 2))  # 3.14

Min and Max

Find smallest and largest values quickly.

python
# Find boundaries in scores
scores = [78, 95, 88, 66]
print(min(scores))  # 66
print(max(scores))  # 95

5) Real Mini Example: Shopping Total

This example combines float, conversion, and arithmetic.

python
# Ask for unit price
unit_price = float(input("Enter unit price: "))
 
# Ask for quantity
quantity = int(input("Enter quantity: "))
 
# Ask for discount rate
discount_rate = float(input("Enter discount rate (for 10%, type 0.10): "))
 
# Compute subtotal
subtotal = unit_price * quantity
 
# Compute discount amount
discount_amount = subtotal * discount_rate
 
# Compute final payment
final_total = subtotal - discount_amount
 
# Print result with two decimal places
print(f"Final total: ${final_total:.2f}")

This is already close to real business logic in many beginner projects.

Common Beginner Mistakes

Mistake 1: Forgetting Conversion After input()

input() gives text, so "2" + "3" becomes "23" instead of numeric addition.

Mistake 2: Expecting Perfect Decimal Precision

Some floating-point calculations may look slightly off (for example, 0.1 + 0.2). This is normal in many programming languages.

Mistake 3: Mixing Types Without Intention

Mixing strings and numbers carelessly causes errors or unexpected results. Be explicit with int() and float().

Practice Challenge

Build a tiny score calculator:

  1. Let user input three exam scores (float)
  2. Calculate average score
  3. Print:
    • average (2 decimals)
    • highest score
    • lowest score

If you can finish this, you are using numbers like a real developer.

FAQ

Should I always use float to be safe?

Not always. Use int when the value is naturally whole. It keeps intent clear and avoids unnecessary decimal handling.

Why does 0.1 + 0.2 not always show exactly 0.3?

Because floating-point numbers are stored in binary approximation. This is expected behavior, not a Python bug.

How can I show only two decimal places?

Use formatted strings, for example: f"{value:.2f}".

Is int(3.9) the same as rounding?

No. int(3.9) truncates to 3, while rounding uses round(3.9) and gives 4.