Python can perform mathematical calculations just like a calculator. Here are the basic operations:
result = 10 + 5
print(result) # Output: 15
x = 3.5
y = 2.5
print(x + y) # Output: 6.0
result = 10 - 5
print(result) # Output: 5
x = 20
y = 8
print(x - y) # Output: 12
result = 10 * 5
print(result) # Output: 50
x = 3
y = 4
print(x * y) # Output: 12
result = 10 / 5
print(result) # Output: 2.0
x = 15
y = 3
print(x / y) # Output: 5.0
Divides and rounds down to the nearest integer:
result = 10 // 3
print(result) # Output: 3
x = 17
y = 5
print(x // y) # Output: 3
Returns the remainder after division:
result = 10 % 3
print(result) # Output: 1
x = 17
y = 5
print(x % y) # Output: 2
Raises a number to a power:
result = 2 ** 3
print(result) # Output: 8
x = 5
y = 2
print(x ** y) # Output: 25
Python follows the standard order of operations:
# Without parentheses
result = 2 + 3 * 4
print(result) # Output: 14 (not 20)
# With parentheses
result = (2 + 3) * 4
print(result) # Output: 20
# Complex expression
result = 10 + 5 * 2 - 3 / 1
print(result) # Output: 17.0
length = 10
width = 5
area = length * width
print("Area:", area) # Output: Area: 50
price = 25.99
quantity = 3
total = price * quantity
print("Total:", total) # Output: Total: 77.97
Try calculating the area of a rectangle, the total cost of items, or any other math problem using the code editor!
Open Code Editor