Web Analytics

Numbers

Beginner ~25 min read

Python supports three numeric types: integers (whole numbers), floats (decimal numbers), and complex numbers. This lesson covers each type, arithmetic operations, and the powerful math module.

Integers (int)

Integers are whole numbers without decimal points. Unlike many languages, Python integers have unlimited precision - they can be as large as your memory allows!

Output
Click Run to execute your code
Number Bases: Python supports binary (0b), octal (0o), and hexadecimal (0x) notation. This is useful when working with low-level data, colors (hex), or file permissions (octal).

Floating Point Numbers (float)

Floats represent decimal numbers. They use the IEEE 754 double-precision format, giving about 15-17 digits of precision.

Output
Click Run to execute your code
Float Precision: 0.1 + 0.2 doesn't equal 0.3 exactly! This is a fundamental limitation of binary floating-point representation. For financial calculations, use the decimal module.

Complex Numbers (complex)

Complex numbers have a real and imaginary part. Python uses j for the imaginary unit (mathematicians use i, engineers use j).

Output
Click Run to execute your code
When to use complex: Complex numbers are essential in electrical engineering, signal processing, quantum computing, and certain mathematical algorithms.

Arithmetic Operations

Python provides all standard arithmetic operators:

Operator Name Example Result
+Addition5 + 38
-Subtraction5 - 32
*Multiplication5 * 315
/Division5 / 31.666...
//Floor Division5 // 31
%Modulo5 % 32
**Exponent5 ** 3125
Output
Click Run to execute your code
Division Note: In Python 3, / always returns a float. Use // for integer (floor) division. In Python 2, / performed integer division if both operands were integers.

The Math Module

Python's math module provides advanced mathematical functions:

Output
Click Run to execute your code

Common Mistakes

1. Integer division expecting float result

# Oops - using // when / was intended
result = 7 // 2    # Returns 3, not 3.5
result = 7 / 2     # Returns 3.5

2. Comparing floats for equality

# Don't do this
if 0.1 + 0.2 == 0.3:  # False!

# Do this instead
if abs((0.1 + 0.2) - 0.3) < 0.0001:  # True

3. Division by zero

# This raises ZeroDivisionError
result = 10 / 0

# Check first
if divisor != 0:
    result = 10 / divisor

Exercise: Simple Calculator

Task: Given two numbers, perform all arithmetic operations and display the results.

Requirements:

  • Calculate: addition, subtraction, multiplication
  • Calculate: division (both regular and integer)
  • Calculate: modulo and power (num1 squared)
Output
Click Run to execute your code
Show Solution
num1 = 25
num2 = 7

print(f"{num1} + {num2} = {num1 + num2}")
print(f"{num1} - {num2} = {num1 - num2}")
print(f"{num1} * {num2} = {num1 * num2}")
print(f"{num1} / {num2} = {num1 / num2:.3f}")
print(f"{num1} // {num2} = {num1 // num2}")
print(f"{num1} % {num2} = {num1 % num2}")
print(f"{num1} ** 2 = {num1 ** 2}")

Summary

  • int: Whole numbers with unlimited precision
  • float: Decimal numbers (IEEE 754 double-precision)
  • complex: Numbers with real and imaginary parts (3+4j)
  • Division: / returns float, // returns integer
  • Math module: Use import math for advanced functions
  • Precision: Use decimal module for financial calculations

What's Next?

In the next lesson, we'll explore strings - Python's powerful text data type, including indexing, slicing, and common string operations.