Learn Without Walls
← Previous Lesson Lesson 2 of 4 Next Lesson →

Numbers: Integers and Floats

What You'll Learn

1. Integers vs. Floats

Python has two main types of numbers:

Integer (int): A whole number without a decimal point. Examples: 42, -7, 0, 1000000
Float (float): A number with a decimal point. Examples: 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))
<class 'int'> <class 'float'>
Note: 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:

OperatorNameExampleResult
+Addition5 + 38
-Subtraction10 - 46
*Multiplication6 * 742
/Division15 / 43.75
//Integer Division15 // 43
%Modulo (remainder)15 % 43
**Exponentiation2 ** 38
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)
8 6 42 3.75 8
Division always returns a float! Even 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 (//): Divides and rounds down to the nearest whole number (discards the remainder).
Modulo (%): Returns the remainder after division.
# 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)
4 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)
Hours: 2 Minutes: 15

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!
42 divided by 2 has remainder: 0

4. Order of Operations

Python follows the standard mathematical order of operations, sometimes remembered as PEMDAS:

  1. Parentheses: () - evaluated first
  2. Exponents: **
  3. Multiplication, Division, Integer Division, Modulo: *, /, //, % (left to right)
  4. 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)
14 20

Temperature Conversion

# Convert Fahrenheit to Celsius
fahrenheit = 98.6
celsius = (fahrenheit - 32) * 5 / 9
print("Body temp in Celsius:", celsius)
Body temp in Celsius: 37.0

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!)
7.0 5.0 3.5 2.0

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
110 85 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
15 4 3.14 20 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!

  1. Calculate the area of a rectangle with width 7.5 and height 12.3 using variables.
  2. Given 250 minutes, use // and % to find how many hours and leftover minutes that is.
  3. Use round() to round 3.14159 to 3 decimal places.

Key Takeaways

← Previous: Variables Next: Strings →