🌟 Python Part 3: Numbers and Arithmetic
1️⃣ Python Number Types
Python supports various types of numbers used in everyday and advanced programming:
| Type | Example | Description |
int | 42 | Whole numbers without a decimal point |
float | 3.14 | Numbers with decimals (floating point) |
complex | 2 + 3j | Complex numbers for advanced scientific use |
bool | True, False | Boolean values that behave like 1 and 0 |
Decimal | Decimal("0.1") | High-precision decimal (for financial applications) |
Fraction | Fraction(1, 3) | Rational number (e.g., 1 divided by 3) |
from decimal import Decimal
from fractions import Fraction
a = 10 # int
b = 3.14 # float
c = 2 + 3j # complex
print(type(a)) # <class 'int'>
print(type(b)) # <class 'float'>
print(type(c)) # <class 'complex'>
Output:
<class 'int'>
<class 'float'>
<class 'complex'>
2️⃣ Basic Arithmetic Operators
| Operator | Name | Example | Result | Description |
+ | Addition | 3 + 2 | 5 | Adds two numbers |
- | Subtraction | 5 - 3 | 2 | Subtracts right from left |
* | Multiplication | 4 * 2 | 8 | Multiplies two numbers |
/ | Division | 10 / 4 | 2.5 | Regular division (returns float) |
// | Floor Division | 10 // 4 | 2 | Division discarding decimal |
% | Modulus (Remainder) | 10 % 4 | 2 | Returns remainder |
** | Exponentiation | 2 ** 3 | 8 | Raises to the power |
# Addition
apples = 3 + 5
print("Total apples:", apples) # 8
# Subtraction
balance = 100 - 40
print("Remaining balance:", balance) # 60
# Multiplication
total_cost = 5 * 200
print("Total cost for 5 items:", total_cost) # 1000
# Division
avg = 500 / 4
print("Average per person:", avg) # 125.0
# Floor Division
pages_per_day = 50 // 7
print("Pages per day (ignoring fractions):", pages_per_day) # 7
# Modulus
leftover = 10 % 3
print("Leftover chocolates:", leftover) # 1
# Exponentiation
area = 4 ** 2
print("Area of square:", area) # 16
3️⃣ Order of Operations (PEMDAS)
Python respects the mathematical precedence:
| Letter | Meaning | Example |
| P | Parentheses | (2 + 3) * 4 = 20 |
| E | Exponents | 2 ** 3 = 8 |
| MD | Multiplication/Division | 4 * 2 / 2 = 4 |
| AS | Addition/Subtraction | 10 - 2 + 1 = 9 |
# Without parentheses
result1 = 2 + 3 * 4
print("2 + 3 * 4 =", result1) # 14
# With parentheses
result2 = (2 + 3) * 4
print("(2 + 3) * 4 =", result2) # 20
# Exponent before multiplication
result3 = 2 * 3 ** 2
print("2 * 3 ** 2 =", result3) # 18
# Left-to-right for same precedence
result4 = 10 - 4 + 2
print("10 - 4 + 2 =", result4) # 8
4️⃣ Type Conversion with Numbers
| Function | Example | Result | Description |
int() | int(3.9) | 3 | Converts float to integer (truncates) |
float() | float(5) | 5.0 | Converts integer to float |
round() | round(3.14159, 2) | 3.14 | Rounds to specified decimal places |
# int()
value1 = int(3.9)
print("int(3.9) =", value1) # 3
# float()
value2 = float(5)
print("float(5) =", value2) # 5.0
# round()
value3 = round(3.14159, 2)
print("round(3.14159, 2) =", value3) # 3.14
5️⃣ Practical Examples
💰 Calculate Total Bill with Tax
price = 1000
tax_rate = 0.15
total = price + (price * tax_rate)
print("Total bill with tax:", total) # 1150.0
🍿 Apply Discount
price = 200
discount = 0.2
final_price = price - (price * discount)
print("Price after discount:", final_price) # 160.0
🎂 Calculate Age
birth_year = 1995
current_year = 2025
age = current_year - birth_year
print("You are", age, "years old.") # 30
⚖️ Calculate BMI
weight_kg = 68
height_m = 1.75
bmi = weight_kg / (height_m ** 2)
print("Your BMI is:", round(bmi, 2)) # 22.2
6️⃣ Built-in Math Functions
| Function | Example | Result | Description |
abs() | abs(-5) | 5 | Absolute (non-negative) value |
pow() | pow(2, 3) | 8 | Power (equivalent to 2 ** 3) |
max() | max(1, 5, 3) | 5 | Returns largest number |
min() | min(1, 5, 3) | 1 | Returns smallest number |
round() | round(3.14159, 2) | 3.14 | Rounds to decimal places |
# abs()
temperature_change = -7
print("Absolute temperature change:", abs(temperature_change)) # 7
# pow()
print("2 raised to the power 3:", pow(2, 3)) # 8
# max()
scores = [85, 92, 78, 90]
print("Highest score:", max(scores)) # 92
# min()
print("Lowest score:", min(scores)) # 78
# round()
pi = 3.14159
print("Rounded PI:", round(pi, 2)) # 3.14
7️⃣ Importing the math module
Use the math module for more advanced mathematical tools.
import math
# math.sqrt()
print("Square root of 16:", math.sqrt(16)) # 4.0
# math.ceil()
print("Ceiling of 4.2:", math.ceil(4.2)) # 5
# math.floor()
print("Floor of 4.8:", math.floor(4.8)) # 4
# math.pi
print("Value of Pi:", math.pi) # 3.141592653589793
Stay tuned for Python Part 2: Strings and User Input 📈