Math operations help us do calculations like adding, subtracting, multiplying, and dividing numbers easily in programs.
Math-related operations in Python
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Python
result = a + b # addition result = a - b # subtraction result = a * b # multiplication result = a / b # division result = a % b # remainder (modulus) result = a ** b # power (exponentiation) result = a // b # floor division (whole number division)
Use + to add, - to subtract, * to multiply, and / to divide.
% gives the remainder after division, and ** raises a number to a power.
Examples
Python
sum = 5 + 3 print(sum)
Python
difference = 10 - 4 print(difference)
Python
product = 7 * 6 print(product)
Python
power = 2 ** 3 print(power)
Sample Program
This program shows all main math operations between two numbers, 15 and 4, and prints the results clearly.
Python
a = 15 b = 4 print(f"Addition: {a} + {b} = {a + b}") print(f"Subtraction: {a} - {b} = {a - b}") print(f"Multiplication: {a} * {b} = {a * b}") print(f"Division: {a} / {b} = {a / b}") print(f"Remainder: {a} % {b} = {a % b}") print(f"Power: {a} ** {b} = {a ** b}") print(f"Floor Division: {a} // {b} = {a // b}")
Important Notes
Division / always gives a float (decimal) result in Python.
Floor division // gives the whole number part of the division, ignoring decimals.
Use parentheses () to control the order of operations if needed.
Summary
Math operations let you do basic calculations in your code.
Use symbols like +, -, *, /, %, and ** for different math tasks.
Practice these to solve many real-life problems with programming.
Practice
1. Which operator in Python is used to find the remainder of a division?
easy
Solution
Step 1: Understand the modulus operator
The modulus operator%returns the remainder after division of one number by another.Step 2: Compare with other operators
The division operator/returns the quotient, exponent**raises to power, and multiplication*multiplies numbers.Final Answer:
The modulus operator % -> Option DQuick Check:
Remainder = % operator [OK]
Hint: Remainder uses % operator in math [OK]
Common Mistakes:
- Confusing / with %
- Using * for remainder
- Thinking ** gives remainder
2. Which of the following is the correct operator syntax to calculate 3 to the power of 4 in Python?
easy
Solution
Step 1: Identify the exponent operator in Python
Python uses**to calculate powers, so3 ** 4means 3 to the power 4.Step 2: Check other options
3 ^ 4is bitwise XOR,pow(3, 4)is a function but not syntax operator,3 ^^ 4is invalid syntax.Final Answer:
3 ** 4 -> Option CQuick Check:
Power uses ** operator [OK]
Hint: Use ** for powers, not ^ [OK]
Common Mistakes:
- Using ^ instead of **
- Trying ^^ which is invalid
- Confusing pow() function with operator
3. What is the output of this Python code?
result = 10 % 3 print(result)
medium
Solution
Step 1: Calculate 10 modulo 3
10 divided by 3 is 3 with remainder 1, so 10 % 3 equals 1.Step 2: Understand print output
The print statement outputs the value stored inresult, which is 1.Final Answer:
1 -> Option AQuick Check:
10 % 3 = 1 [OK]
Hint: Modulo gives remainder after division [OK]
Common Mistakes:
- Confusing modulo with division
- Expecting quotient instead of remainder
- Misreading print output
4. Find the error in this code snippet:
value = 5 ** print(value)
medium
Solution
Step 1: Check the exponent operator usage
The exponent operator**needs two numbers, but here only one number (5) is given before it.Step 2: Identify syntax error
Because the second operand is missing, Python will raise a syntax error before print runs.Final Answer:
Missing second operand for exponent operator -> Option BQuick Check:
** needs two numbers [OK]
Hint: Exponent needs two numbers, not one [OK]
Common Mistakes:
- Using ^ instead of **
- Thinking print() is wrong here
- Assuming variable name causes error
5. You want to calculate the area of a circle with radius 7 using Python. Which code correctly uses math operations to do this?
import math radius = 7 area = ? print(area)
hard
Solution
Step 1: Recall formula for circle area
The area of a circle is π times radius squared, or π * r².Step 2: Translate formula to Python code
Usemath.pifor π andradius ** 2for radius squared, somath.pi * (radius ** 2).Final Answer:
area = math.pi * (radius ** 2) -> Option AQuick Check:
Area = π * r² [OK]
Hint: Use ** 2 for square, multiply by math.pi [OK]
Common Mistakes:
- Using addition instead of multiplication
- Using exponent on pi instead of radius
- Forgetting to square radius
