Skip to content

Python math operators

  • by

Python math operators are symbols or characters that allow you to perform mathematical operations on numbers or variables. These operators enable you to perform tasks such as addition, subtraction, multiplication, division, and more. Here are some commonly used Python math operators:

OperatorDescriptionExample
+Additiona + b
-Subtractiona - b
*Multiplicationa * b
/Divisiona / b
//Floor Divisiona // b
%Moduloa % b
**Exponentiationa ** b
=Assignmenta = b
+=Incrementa += b
-=Decrementa -= b

These operators can be used with variables or literal values to perform various mathematical operations in Python.

Python math operators example

Here are some examples of how to use Python math operators:

# Addition
a = 5
b = 3
result = a + b
print(result)  # Output: 8

# Subtraction
a = 10
b = 7
result = a - b
print(result)  # Output: 3

# Multiplication
a = 4
b = 6
result = a * b
print(result)  # Output: 24

# Division
a = 10
b = 2
result = a / b
print(result)  # Output: 5.0 (division always returns a float)

# Floor Division
a = 10
b = 3
result = a // b
print(result)  # Output: 3 (rounded down to the nearest whole number)

# Modulo
a = 10
b = 3
result = a % b
print(result)  # Output: 1 (remainder of the division)

# Exponentiation
a = 2
b = 3
result = a ** b
print(result)  # Output: 8 (2 raised to the power of 3)

# Assignment
a = 5
b = a
print(b)  # Output: 5

# Increment
a = 5
a += 2
print(a)  # Output: 7

# Decrement
a = 5
a -= 2
print(a)  # Output: 3

Output:

Python math operators

Syntax

Addition: +

result = operand1 + operand2

Subtraction: -

result = operand1 - operand2

Multiplication: *

result = operand1 * operand2

Division: /

result = operand1 / operand2

Floor Division: //

result = operand1 // operand2

Modulo: %

result = operand1 % operand2

Exponentiation: **

result = operand1 ** operand2

Assignment: =

variable = value

Increment: +=

variable += value

Decrement: -=

variable -= value

In the above syntax, operand1 and operand2 represent the values or variables on which the operation is performed. The result of the operation is stored in the result variable (for arithmetic operations) or assigned to a variable (for assignment and increment/decrement).

Note: the actual variable names, values, and operators used may vary depending on your specific use case.

Do comment if you have any doubts or suggestions on this Python math topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading