</>
Skip to content
Python lessons (8/45)

Python — Numbers

Three numeric types

age = 25            # int   — whole numbers
price = 9.99        # float — decimals
c = 2 + 3j          # complex — engineering/math (rare in apps)

print(type(age))    # <class 'int'>

The operators

a, b = 10, 3

a + b     # 13
a - b     # 7
a * b     # 30
a / b     # 3.333…  ← division ALWAYS returns float
a // b    # 3       floor division — drops the decimal
a % b     # 1       remainder
a ** b    # 1000    power (10³)

/ vs // is the classic surprise: 10 / 5 gives 2.0, not 2. Use // when you truly want integer division.

Order of operations

Math rules apply; parentheses win:

2 + 3 * 4       # 14
(2 + 3) * 4     # 20
-3 ** 2         # -9  (power before minus!)
(-3) ** 2       # 9

Underscores for readability

Big numbers may include visual separators — Python ignores them:

population = 1_400_000_000
print(population)      # 1400000000

Float precision quirk

Floats are binary under the hood:

0.1 + 0.2          # 0.30000000000000004 😱
round(0.1 + 0.2, 2)   # 0.3  ✓ round for display/comparison

Same IEEE-float reality as every language. For money, compute in cents (int) or use the decimal module.

Useful built-ins

abs(-7)              # 7
round(3.14159, 2)    # 3.14
max(3, 7, 5)         # 7
min(3, 7, 5)         # 3
sum([1, 2, 3])       # 6
pow(2, 8)            # 256

The math module

import math

math.sqrt(81)        # 9.0
math.floor(4.9)      # 4   always down
math.ceil(4.1)       # 5   always up
math.pi              # 3.14159…
math.inf             # infinity

Converting between types

int(3.99)      # 3     truncates toward zero!
int("42")      # 42
float("3.5")   # 3.5
str(42)        # "42"

Gotchas: int("3.5") raises ValueError (parse with int(float("3.5"))); 0.1 + 0.2 != 0.3; / never gives an int.

Mini Practice

  1. Predict then verify all seven operators on 17 and 5.
  2. Show 10 / 5 vs 10 // 5 type difference.
  3. Fix 0.1 + 0.2 == 0.3 using round().
  4. Convert seconds to h:m:s with // and %.
  5. Import math; compute hypotenuse via sqrt(a2 + b2).

Next: casting →

Related Topics

Frequently Asked Questions about Numbers

What is Numbers in Python?

Numbers is a fundamental concept in Python. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Numbers?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Numbers.

Why is Numbers important in Python?

Numbers is essential for Python development. Understanding this concept will help you write better code and solve real-world problems more effectively.