How To Do Math in Python 3 with Operators

Published on Aug. 22, 2023, 12:16 p.m.

Python 3 supports a wide range of arithmetic operators that you can use for math operations. Here are some common operators:

  • +: Addition
  • -: Subtraction
  • *: Multiplication
  • /: Division
  • %: Modulo (returns the remainder when one number is divided by another)
  • **: Exponentiation (raises one number to the power of another)
  • //: Floor Division (returns the result of division rounded down to the nearest integer)

Here are some examples:

x = 5
y = 2

# addition
print(x + y)  # output: 7

# subtraction
print(x - y)  # output: 3

# multiplication
print(x * y)  # output: 10

# division
print(x / y)  # output: 2.5

# modulo
print(x % y)  # output: 1

# exponentiation
print(x ** y)  # output: 25

# floor division
print(x // y)  # output: 2

In addition to these basic operators, Python also provides a number of mathematical functions in the math module that you can use to perform more complex mathematical operations, such as trigonometric functions, logarithmic functions, and more. To use these functions, you will need to import the math module at the beginning of your script.

Tags:

related content