0
0
Pythonprogramming~5 mins

Math-related operations in Python

Choose your learning style9 modes available
Introduction

Math operations help us do calculations like adding, subtracting, multiplying, and dividing numbers easily in programs.

Calculating the total price of items in a shopping cart.
Finding the average score of students in a class.
Converting temperatures between Celsius and Fahrenheit.
Calculating the area or perimeter of shapes.
Counting how many times something happens.
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
Adds 5 and 3, then prints 8.
Python
sum = 5 + 3
print(sum)
Subtracts 4 from 10, then prints 6.
Python
difference = 10 - 4
print(difference)
Multiplies 7 by 6, then prints 42.
Python
product = 7 * 6
print(product)
Raises 2 to the power of 3, then prints 8.
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}")
OutputSuccess
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.