Doing Math with Numbers
Python has two different ways to store numbers. They might sound technical, but you already use them in real life every day!
1. Integers (Whole Numbers)
An integer (or int) is just a standard whole number. It can be positive or negative, but it never has a decimal point. You use integers when counting things.
# Think of this like counting apples or followers
apples_count = 5
temperature = -12
score = 100
2. Floats (Decimal Numbers)
A float is any number that has a decimal point. Whenever you are dealing with money, weight, or exact measurements, you will use floats.
# Think of this like a price tag or weight
coffee_price = 49.99
weight = 2.5
[!NOTE] Even if a decimal ends in zero (like
10.0), Python still considers it a float!
Why two different types?
Computers handle whole numbers much faster than decimals. But don't worry - Python is smart enough to handle conversions behind the scenes for you most of the time!
3. Basic Math
Python can do all standard math operations effortlessly. You don't need quotation marks for numbers.
# Addition and Subtraction
total = 10 + 5 # 15
change = 20 - 7 # 13
# Multiplication and Division
area = 6 * 4 # 24
half = 10 / 2 # 5.0 (Division always gives a float!)
If you wrap numbers in quotation marks ("5"), Python treats them as Strings (Text) rather than numbers:
print(5 + 5) # Output: 10 (Math Addition)
print("5" + "5") # Output: 55 (Text Concatenation)
Quick Summary
- Integers (
int): Whole positive or negative numbers with no decimal points (e.g.,42,-10). - Floats (
float): Numbers that contain decimal fractions (e.g.,3.14,-0.5). - Division Rule (
/): Regular division in Python 3 always returns afloat(e.g.,10 / 2yields5.0). - Type Conversion: Use
int()to truncate to whole numbers andfloat()to convert numbers/strings to decimals.
What's Next?
Sometimes you do not need numbers or words; you just need a simple "Yes" or "No" flag. Let's look at Booleans in the next lesson!