Skip to main content

Command Palette

Search for a command to run...

🌟 Python Part 3: Numbers and Arithmetic

Published
•5 min read•View as Markdown

1️⃣ Python Number Types

Python supports various types of numbers used in everyday and advanced programming:

TypeExampleDescription
int42Whole numbers without a decimal point
float3.14Numbers with decimals (floating point)
complex2 + 3jComplex numbers for advanced scientific use
boolTrue, FalseBoolean values that behave like 1 and 0
DecimalDecimal("0.1")High-precision decimal (for financial applications)
FractionFraction(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

OperatorNameExampleResultDescription
+Addition3 + 25Adds two numbers
-Subtraction5 - 32Subtracts right from left
*Multiplication4 * 28Multiplies two numbers
/Division10 / 42.5Regular division (returns float)
//Floor Division10 // 42Division discarding decimal
%Modulus (Remainder)10 % 42Returns remainder
**Exponentiation2 ** 38Raises 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:

LetterMeaningExample
PParentheses(2 + 3) * 4 = 20
EExponents2 ** 3 = 8
MDMultiplication/Division4 * 2 / 2 = 4
ASAddition/Subtraction10 - 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

FunctionExampleResultDescription
int()int(3.9)3Converts float to integer (truncates)
float()float(5)5.0Converts integer to float
round()round(3.14159, 2)3.14Rounds 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

FunctionExampleResultDescription
abs()abs(-5)5Absolute (non-negative) value
pow()pow(2, 3)8Power (equivalent to 2 ** 3)
max()max(1, 5, 3)5Returns largest number
min()min(1, 5, 3)1Returns smallest number
round()round(3.14159, 2)3.14Rounds 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 📈

More from this blog

Data Science

39 posts