Why Math is Important in Programming
Every great program has one thing in common — it relies heavily on logic and mathematical operations. Whether it's game development, finance, or machine learning, math is at the core of coding. Luckily, Python gives us a rich set of arithmetic operators to work with numbers effortlessly.
Python supports the following arithmetic operators:
- + Addition
- - Subtraction
- * Multiplication
- / Division
- // Floor Division
- % Modulus
- ** Exponentiation
Let’s go through each of them with simple, real-world examples.
Floor Division Operator (//)
Imagine you have 11 balls and you want to distribute them equally among 4 friends. If we do 11 ÷ 4, the result is 2.75. But can you give 0.75 of a ball to a friend? Of course not!
So what you really want is only the whole number part of the division — the quotient without the decimal. This is done using the floor division operator (//).
Example:
balls = 11
friends = 4
ball_distributed = balls // friends
print("Balls given to each friend:",ball_distributed)
This gives:
Balls given to each friend: 2
Floor division simply discards the decimal part and returns the integer.
Modulus Operator (%)
After distributing the balls equally, you may want to know how many balls are left. This is where the modulus operator % comes in.
It returns the remainder when one number is divided by another.
Let’s extend the program:
balls = 11
friends = 4
ball_distributed = balls // friends
ball_remaining = balls % friends
print("Balls given to each friend:",ball_distributed)
print("Balls remaining with us:", ball_remaining)
Output:
Balls given to each friend: 2
Balls remaining with us: 3
Remember:
- // → Gives quotient
- % → Gives remainder
Exponentiation Operator (**)
Want to calculate powers like 4³ (4 raised to 3)? Python makes it easy with the exponentiation operator **.
Example
x = 4 ** 3
print("4 raised to power 3 is:", x)
Output:
4 raised to power 3 is: 64
Simple and powerful!
Sample Code
Code Explanation
- a + b → Adds two numbers
- a - b → Subtracts b from a
- a * b → Multiplies a and b
- a / b → Performs normal division (returns float)
- a // b → Floor division (removes decimal)
- a % b → Returns the remainder
- a ** b → Raises a to the power of b
This code showcases how versatile Python's arithmetic operators are and how they can be applied to almost any numeric operation.
Output
Addition: 19
Subtraction: 11
Multiplication: 60
Division: 3.75
Floor Division: 3
Modulus: 3
Exponentiation: 50625
Watch It on YouTube
Want to see these operations live in action with examples? Click here to watch the full video on YouTube →
Final Thoughts
Python provides all the basic and advanced arithmetic operators you need to build powerful logic in your programs. You now understand how to use floor division, modulus, and exponentiation — tools you’ll use repeatedly in real-world applications.
Keep practicing. These operators form the building blocks for loops, conditions, and data processing!