Numbers
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!
Click Run to execute your code
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.
Click Run to execute your code
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).
Click Run to execute your code
Arithmetic Operations
Python provides all standard arithmetic operators:
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 5 - 3 | 2 |
* | Multiplication | 5 * 3 | 15 |
/ | Division | 5 / 3 | 1.666... |
// | Floor Division | 5 // 3 | 1 |
% | Modulo | 5 % 3 | 2 |
** | Exponent | 5 ** 3 | 125 |
Click Run to execute your code
/ 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:
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)
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 mathfor advanced functions - Precision: Use
decimalmodule 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.
Enjoying these tutorials?