Numbers and Math
Numbers and Math
Numbers are used in almost every Python program. You will use them for calculations, scores, prices, measurements, statistics, and much more. Python makes working with numbers simple and powerful.
Number Types
Python mainly uses two number types:
intfor whole numbersfloatfor decimal numbers
age = 25
price = 19.99
Basic Math Operators
Python supports common mathematical operations.
print(10 + 5)
print(10 - 5)
print(10 * 5)
print(10 / 5)
More Operators
print(10 // 3)
print(10 % 3)
print(2 ** 3)
Operator Meaning
//floor division%remainder (modulus)**power
Order of Operations
Python follows standard math rules.
print(2 + 3 * 4)
print((2 + 3) * 4)
Useful Number Functions
print(abs(-7))
print(round(3.75))
print(max(4, 8, 2))
print(min(4, 8, 2))
Working With User Input
Input is stored as text, so convert when needed.
age = int(input("Enter age: "))
print(age + 1)
Code Along
num1 = 12
num2 = 5
print(num1 + num2)
print(num1 - num2)
print(num1 * num2)
print(num1 / num2)
Mini Challenge
Create a program that:
- Stores two numbers
- Prints their sum
- Prints their difference
- Prints their product
- Prints their remainder
Real World Use Case
Shopping systems use numbers to calculate totals, taxes, discounts, and change. Banking apps use numbers for balances, transfers, and reports.
Quiz
- What type stores decimal numbers?
- What does
%return? - What is the result of
2 ** 4? - Why do we convert input with
int()?
Assignment
Build a simple calculator that asks the user for two numbers and displays results for all major operators.
Summary
You learned number types, math operators, useful number functions, operator precedence, and how to use numbers in real Python programs.