Skip to content

Arithmetic Operations

PotatoScript edited this page Feb 27, 2025 · 2 revisions

➕ Arithmetic Operations in Python 🔢

This section will cover:
✅ Basic arithmetic operations 🏗️
✅ Operator precedence (order of calculations) 🏆
✅ Using the math module for advanced calculations 📊
✅ Common mistakes and how to avoid them 🚨


1️⃣ Basic Arithmetic Operators 🏗️

Python supports basic mathematical operations, just like a calculator. Here are the most common ones:

Operator Description Example Output
+ Addition 5 + 3 8
- Subtraction 10 - 4 6
* Multiplication 6 * 3 18
/ Division 9 / 2 4.5
// Floor Division 9 // 2 4
% Modulus (Remainder) 10 % 3 1
** Exponentiation (Power) 2 ** 3 8

2️⃣ Examples of Basic Arithmetic 🧮

🔹 Addition & Subtraction:

a = 10
b = 5
print(a + b)  # 15
print(a - b)  # 5

🔹 Multiplication & Division:

print(6 * 3)  # 18
print(9 / 2)  # 4.5

🔹 Floor Division (//) – Removes the decimal part:

print(9 // 2)  # 4
print(15 // 4) # 3

🔹 Modulus (%) – Finds the remainder:

print(10 % 3)  # 1
print(20 % 7)  # 6

🔹 Exponentiation (**) – Power calculation:

print(2 ** 3)  # 8  (2 raised to the power of 3)
print(5 ** 2)  # 25 (5 squared)

3️⃣ Operator Precedence (Order of Operations) 🏆

Just like in math, some operations are performed first. Python follows PEMDAS (Parentheses, Exponents, Multiplication/Division, Addition/Subtraction).

🔹 Example without parentheses:

result = 5 + 2 * 3
print(result)  # 11 (Multiplication happens first)

🔹 Example with parentheses:

result = (5 + 2) * 3
print(result)  # 21 (Parentheses first)

Remember: Always use parentheses () to make your calculations clear!


4️⃣ Advanced Math with the math Module 📊

Python has a built-in math module for advanced calculations. Let's explore! 🚀

🔹 Importing the math module:

import math
Function Description Example Output
math.sqrt(x) Square root math.sqrt(16) 4.0
math.ceil(x) Rounds up math.ceil(4.2) 5
math.floor(x) Rounds down math.floor(4.8) 4
math.pow(x, y) Power calculation math.pow(2, 3) 8.0
math.pi Pi value (π) math.pi 3.1415926535

🔹 Example usage:

import math

print(math.sqrt(25))  # 5.0
print(math.ceil(4.2))  # 5
print(math.floor(4.8))  # 4
print(math.pow(2, 3))  # 8.0
print(math.pi)  # 3.1415926535

5️⃣ Common Mistakes & How to Avoid Them 🚨

Dividing by Zero (ZeroDivisionError)

print(10 / 0)  # ❌ ERROR! Cannot divide by zero!

Solution: Always check before dividing!

num = 0
if num != 0:
    print(10 / num)
else:
    print("Cannot divide by zero! 🚨")

Forgetting to convert strings to numbers

num = input("Enter a number: ")  # num is a string!
print(num + 5)  # ❌ ERROR! Can't add string to number!

Solution: Convert input to int or float

num = int(input("Enter a number: "))
print(num + 5)  # ✅ Works correctly!

✅ Summary

✔️ Python supports addition (+), subtraction (-), multiplication (*), division (/), floor division (//), modulus (%), and exponentiation ()**.
✔️ Operator precedence follows PEMDAS rules.
✔️ The math module provides advanced functions like math.sqrt(), math.pow(), and math.pi.
✔️ Common mistakes include dividing by zero and forgetting to convert input to numbers.


  • to convert str into int int(str)

  • print(10 / 3) #output 3.333333

  • print(10 // 3) #output 3 get int

  • print(10 % 3) #output 1 remainder

  • print(10 ** 3) #10 power of 3 output 1000

  • print(round(2.9)) #output 3

  • print(abs(-2.9)) #output 2.9 absolute always return positive value

  • refer to https://docs.python.org/3/library/math.html or type python 3 math module

import math
print(math.ceil(2.9)) #output 3
print(math.floor(2.9)) #output 2

Clone this wiki locally