Numbers: Integers and Floats
What You'll Learn
- The difference between integers and floats
- All of Python's arithmetic operators
- Order of operations (precedence)
- Integer division and the modulo operator
- Common patterns for working with numbers
1. Integers vs. Floats
Python has two main types of numbers:
42, -7, 0, 1000000
3.14, -0.5, 2.0, 100.0
# Integers - whole numbers student_count = 30 temperature_celsius = -5 year = 2025 # Floats - decimal numbers price = 9.99 pi = 3.14159 gpa = 3.8 # Check the type print(type(student_count)) print(type(price))
2.0 is a float, even though its value is a whole number. The presence of the decimal point makes it a float.
2. Arithmetic Operators
Python supports all the standard math operations and a few extras:
| Operator | Name | Example | Result |
|---|---|---|---|
+ | Addition | 5 + 3 | 8 |
- | Subtraction | 10 - 4 | 6 |
* | Multiplication | 6 * 7 | 42 |
/ | Division | 15 / 4 | 3.75 |
// | Integer Division | 15 // 4 | 3 |
% | Modulo (remainder) | 15 % 4 | 3 |
** | Exponentiation | 2 ** 3 | 8 |
print(5 + 3) # Addition print(10 - 4) # Subtraction print(6 * 7) # Multiplication print(15 / 4) # Division (always returns float) print(2 ** 3) # Exponentiation (2 to the power of 3)
10 / 2 gives 5.0, not 5. This is an important detail in Python 3.
3. Integer Division and Modulo
Two operators deserve special attention because they are incredibly useful in programming.
# Integer division: how many whole times does 4 go into 17? print(17 // 4) # 4 (4 goes into 17 four times) # Modulo: what's left over? print(17 % 4) # 1 (17 = 4*4 + 1, remainder is 1)
Practical Use: Converting Minutes to Hours
total_minutes = 135 hours = total_minutes // 60 # How many whole hours? remaining = total_minutes % 60 # How many minutes left over? print("Hours:", hours) print("Minutes:", remaining)
Practical Use: Is a Number Even or Odd?
number = 42 remainder = number % 2 print(number, "divided by 2 has remainder:", remainder) # If remainder is 0, the number is even!
4. Order of Operations
Python follows the standard mathematical order of operations, sometimes remembered as PEMDAS:
- Parentheses:
()- evaluated first - Exponents:
** - Multiplication, Division, Integer Division, Modulo:
*,/,//,%(left to right) - Addition, Subtraction:
+,-(left to right)
# Without parentheses - follows PEMDAS result = 2 + 3 * 4 print(result) # 14, not 20! (multiplication first) # With parentheses - overrides default order result = (2 + 3) * 4 print(result) # 20 (parentheses first)
Temperature Conversion
# Convert Fahrenheit to Celsius fahrenheit = 98.6 celsius = (fahrenheit - 32) * 5 / 9 print("Body temp in Celsius:", celsius)
The parentheses ensure subtraction happens before multiplication.
5. Mixing Integers and Floats
When you combine an integer and a float in an operation, Python automatically converts the result to a float:
print(5 + 2.0) # 7.0 (int + float = float) print(10 * 0.5) # 5.0 (int * float = float) print(7 / 2) # 3.5 (division always returns float) print(6 / 3) # 2.0 (even when it divides evenly!)
Python also provides shorthand operators for updating variables:
score = 100 score += 10 # Same as: score = score + 10 print(score) # 110 score -= 25 # Same as: score = score - 25 print(score) # 85 score *= 2 # Same as: score = score * 2 print(score) # 170
6. Useful Number Functions
Python has several built-in functions for working with numbers:
# abs() - absolute value print(abs(-15)) # 15 # round() - round to nearest integer or decimal places print(round(3.7)) # 4 print(round(3.14159, 2)) # 3.14 # max() and min() print(max(10, 20, 5)) # 20 print(min(10, 20, 5)) # 5
Check Your Understanding
Question 1: What is the result of 17 // 5 and 17 % 5?
17 // 5 = 3 (5 goes into 17 three whole times)
17 % 5 = 2 (17 = 5*3 + 2, the remainder is 2)
Question 2: What does 2 + 3 ** 2 * 4 evaluate to?
Answer: 38
Step by step: 3 ** 2 = 9 (exponent first), then 9 * 4 = 36, then 2 + 36 = 38.
Try It Yourself!
- Calculate the area of a rectangle with width 7.5 and height 12.3 using variables.
- Given 250 minutes, use
//and%to find how many hours and leftover minutes that is. - Use
round()to round 3.14159 to 3 decimal places.
Key Takeaways
- Integers are whole numbers; floats have decimal points.
- Python supports 7 arithmetic operators:
+,-,*,/,//,%,** - Regular division (
/) always returns a float. - Integer division (
//) discards the decimal; modulo (%) gives the remainder. - Python follows PEMDAS order of operations. Use parentheses to control the order.
- Mixing ints and floats produces a float result.