
Python
Python has two main types of numbers: integers (whole numbers) and floats (decimal numbers). Integers are numbers without decimal points like 5, -3, or 1000, while floats have decimal points like 3.14, -2.5, or 0.001. Python automatically figures out which type you're using based on whether your number has a decimal point.
An integer is a whole number with no decimal part, while a float is a number with a decimal point. Python treats them slightly differently in storage and operations, but you usually don't need to think about this distinction. When you perform certain operations (like division), Python automatically converts integers to floats when needed.
# Integers - whole numbers
count = 42
temperature = -5
students = 0
year = 2024
# Floats - decimal numbers
price = 19.99
height = 5.6
pi = 3.14159
accuracy = 0.95
# Check the type
print(type(42)) # <class 'int'>
print(type(3.14)) # <class 'float'>Python can perform basic arithmetic operations on numbers: addition (+), subtraction (-), multiplication (*), and division (/). These operations work with both integers and floats, and Python automatically handles the conversions needed to give you the correct result.
# Basic arithmetic operations
a = 10
b = 3
# Addition
sum_result = a + b # 13
# Subtraction
difference = a - b # 7
# Multiplication
product = a * b # 30
# Division (always returns float)
quotient = a / b # 3.333...
print(f"Sum: {sum_result}")
print(f"Difference: {difference}")
print(f"Product: {product}")
print(f"Quotient: {quotient}")Beyond basic arithmetic, Python provides additional operations: power (exponent), floor division (dividing and rounding down), and modulo (finding the remainder). These operations are commonly used in real programs for calculations, rounding, and checking divisibility.
# Power (exponentiation)
power = 2 ** 3 # 2 to the power of 3 = 8
square = 5 ** 2 # 5 squared = 25
# Floor division (divides and rounds down)
floor_div = 17 // 5 # 17 divided by 5 = 3 (not 3.4)
floor_div2 = 10 // 3 # 10 divided by 3 = 3
# Modulo (remainder)
remainder = 17 % 5 # Remainder of 17 รท 5 = 2
remainder2 = 10 % 3 # Remainder of 10 รท 3 = 1
print(f"2 ** 3 = {power}")
print(f"17 // 5 = {floor_div}")
print(f"17 % 5 = {remainder}")Python follows the standard order of operations (PEMDAS): Parentheses, Exponents, Multiplication/Division, Addition/Subtraction. This means some operations happen before others, so understanding the order prevents mistakes. You can always use parentheses to make the order clear and explicit.
# Without parentheses
result1 = 2 + 3 * 4 # 2 + (3 * 4) = 14, not 20!
result2 = 10 / 2 - 1 # (10 / 2) - 1 = 4, not 10 / 1 = 10
# With parentheses (explicit)
result3 = (2 + 3) * 4 # 5 * 4 = 20
result4 = 10 / (2 - 1) # 10 / 1 = 10.0
print(f"2 + 3 * 4 = {result1}") # 14
print(f"(2 + 3) * 4 = {result3}") # 20current_year = 2024
birth_year = 1998
age_years = current_year - birth_year
age_days = age_years * 365
print(f"You are approximately {age_days} days old!")price_per_item = 12.99
quantity = 5
total = price_per_item * quantity
tax_rate = 0.08
tax = total * tax_rate
final_price = total + tax
print(f"Subtotal: ${total:.2f}")
print(f"Tax: ${tax:.2f}")
print(f"Total: ${final_price:.2f}")number = 17
remainder = number % 2
if remainder == 0:
print(f"{number} is even")
else:
print(f"{number} is odd")| Concept | Remember |
|---|---|
| Integer | Whole number: 5, -3, 1000 |
| Float | Decimal number: 3.14, -2.5 |
| Addition | + adds numbers |
| Subtraction | - subtracts numbers |
| Multiplication | * multiplies numbers |
| Division | / always returns float |
| Floor Division | // divides and rounds down |
| Modulo | % gives remainder |
| Power | ** raises to power |
| Order | PEMDAS: Parentheses, Exponents, Multiplication, Division, Addition, Subtraction |
You've learned about number types. Now let's explore all the operations you can perform with numbers in more detail!
Ready to practice? Try challenges or take the quiz
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep โข Build Real โข Verify Skills โข Launch Forward