5 min read
Data Types I - Numbers
Learn to work with whole numbers and decimal numbers
What You'll Learn
- Two types of numbers in Python
- Doing math with numbers
- Converting between number types
Two Types of Numbers
Python has two main number types:
1. Integers (Whole Numbers)
Numbers without decimals:
code.py
age = 25
students = 30
year = 2024
score = -102. Floats (Decimal Numbers)
Numbers with decimal points:
code.py
price = 19.99
temperature = 98.6
height = 5.8
weight = 150.5Basic Math Operations
Addition (+)
code.py
total = 10 + 5
print(total)
# Shows: 15
price = 100.50 + 25.25
print(price)
# Shows: 125.75Subtraction (-)
code.py
difference = 20 - 8
print(difference)
# Shows: 12
balance = 500.00 - 150.50
print(balance)
# Shows: 349.5Multiplication (*)
code.py
total = 5 * 4
print(total)
# Shows: 20
area = 10.5 * 2.0
print(area)
# Shows: 21.0Division (/)
code.py
result = 20 / 4
print(result)
# Shows: 5.0
half = 10 / 2
print(half)
# Shows: 5.0Note: Division always gives a float (decimal)!
Power (**)
Multiply a number by itself:
code.py
square = 5 ** 2
print(square)
# Shows: 25 (5 × 5)
cube = 3 ** 3
print(cube)
# Shows: 27 (3 × 3 × 3)Remainder (%)
Get the leftover after division:
code.py
remainder = 10 % 3
print(remainder)
# Shows: 1 (10 ÷ 3 = 3 with 1 left over)
even_check = 8 % 2
print(even_check)
# Shows: 0 (no remainder means even number)Working with Numbers
Simple Calculator
code.py
num1 = 10
num2 = 3
print("Add:", num1 + num2) # 13
print("Subtract:", num1 - num2) # 7
print("Multiply:", num1 * num2) # 30
print("Divide:", num1 / num2) # 3.333...Real-World Example: Shopping
code.py
price_per_item = 25.50
quantity = 3
tax_rate = 0.08
# Calculate total
subtotal = price_per_item * quantity
tax = subtotal * tax_rate
total = subtotal + tax
print("Subtotal:", subtotal)
print("Tax:", tax)
print("Total:", total)Mixing Integers and Floats
You can mix them, Python handles it:
code.py
result = 10 + 5.5
print(result)
# Shows: 15.5
total = 100 * 1.5
print(total)
# Shows: 150.0Converting Number Types
Float to Integer
Removes the decimal part:
code.py
price = 19.99
whole_price = int(price)
print(whole_price)
# Shows: 19 (decimal removed)Integer to Float
Adds a decimal point:
code.py
age = 25
age_float = float(age)
print(age_float)
# Shows: 25.0Rounding Numbers
Make decimals shorter:
code.py
price = 19.99876
rounded = round(price, 2)
print(rounded)
# Shows: 20.0 (rounded to 2 decimals)
number = 15.7
rounded_whole = round(number)
print(rounded_whole)
# Shows: 16Practice Examples
Example 1: Calculate age
code.py
current_year = 2024
birth_year = 1995
age = current_year - birth_year
print("Age:", age)
# Shows: Age: 29Example 2: Split bill
code.py
total_bill = 150.00
people = 3
per_person = total_bill / people
print("Each person pays:", per_person)
# Shows: Each person pays: 50.0Example 3: Calculate area
code.py
length = 10.5
width = 8.0
area = length * width
print("Area:", area)
# Shows: Area: 84.0Order of Operations
Python follows math rules (PEMDAS):
code.py
result = 2 + 3 * 4
print(result)
# Shows: 14 (multiply first, then add)
result = (2 + 3) * 4
print(result)
# Shows: 20 (parentheses first)Use parentheses () to control order!
Common Mistakes
Mistake 1: Dividing by zero
code.py
result = 10 / 0 # Error!Mistake 2: Forgetting decimals
code.py
half = 1 / 2
print(half)
# Shows: 0.5 (not 0)Quick Reference
code.py
+ # Add
- # Subtract
* # Multiply
/ # Divide
** # Power
% # RemainderSummary
- Integers = whole numbers (1, 2, 3)
- Floats = decimal numbers (1.5, 2.99)
- Use math operators for calculations
- Division always gives float
- Use round() to shorten decimals
What's Next?
Now let's learn about working with text (strings)!