CodingNic

Python Foundations

Numbers and Math

Python Foundations 20 min read

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:

  • int for whole numbers
  • float for decimal numbers
python
age = 25
price = 19.99

Basic Math Operators

Python supports common mathematical operations.

python
print(10 + 5)
print(10 - 5)
print(10 * 5)
print(10 / 5)

More Operators

python
print(10 // 3)
print(10 % 3)
print(2 ** 3)

Operator Meaning

  • // floor division
  • % remainder (modulus)
  • ** power

Order of Operations

Python follows standard math rules.

python
print(2 + 3 * 4)
print((2 + 3) * 4)

Useful Number Functions

python
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.

python
age = int(input("Enter age: "))
print(age + 1)

Code Along

python
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

  1. What type stores decimal numbers?
  2. What does % return?
  3. What is the result of 2 ** 4?
  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.