Python - Arithmetic Operators

Python

Share
python logo

In this tutorial we will learn about Arithmetic operators in Python.

Following are the list of Arithmetic operators in Python

Addition

We use the + sign to perform addition in Python.

This operator adds the value of the left side variable with the value of the right side variable.

In the following Python program we are adding the value of two variables.

# variables
x = 10
y = 20

# operation
result = x + y

# output
print("Result:", result)

The above code will give the following result.

Result: 30

Subtraction

We use the - sign to perform subtraction in Python.

This operator subtracts the right side variable value from the left side variable value.

In the following Python program we are subtracting the value of y from x.

# variables
x = 10
y = 20

# operation
result = x - y

# output
print("Result:", result)

The above code will give the following result.

Result: -10

Multiplication

We use the * sign to perform multiplication in Python.

This operator multiplies the value of the left side variable with the value of the right side variable.

In the following Python program we are multiplying the value of x and y.

# variables
x = 10
y = 20

# operation
result = x * y

# output
print("Result:", result)

The above code will give the following result.

Result: 200

Division

We use the / sign to perform division in Python.

This operator divides the left side variable value by the right side variable value.

In the following Python program we are dividing the value of x by y.

# variables
x = 10
y = 20

# operation
result = x / y

# output
print("Result:", result)

The above code will give the following result.

Result: 0.5

Modulus

We use the % sign to perform modulus in Python.

This operator divides the value of the left side variable by the value of the right side variable and gives us the remainder value.

In the following Python program we are finding the remainder on dividing the value of x by y.

# variables
x = 101
y = 20

# operation
result = x % y

# output
print("Result:", result)

The above code will give the following result.

Result: 1

Exponent

We use the ** sign to perform exponential operation in Python.

This operator raises the left side variable value to the power of right side variable value.

In the following Python program we are raising x to the power of y i.e. xy.

# variables
x = 2
y = 5

# operation
result = x ** y

# output
print("Result:", result)

The above code will give the following result.

Result: 32

Floor Division

We use the // sign to perform floor division in Python.

Points to note!

  • When two positive numbers are involved then it returns the quotient after removing the decimal parts.
  • When one of the operand is a negative number then it floors the result towards negative infinity.

In the following Python program we are performing floor division. We have 10 and 3 and we will get 3 as the result of 10//3.

We get 3.33333... on dividing 10 by 3.
Floor(3.333...) = 3

# variables
x = 10
y = 3

# operation
result = x // y

# output
print("Result:", result)

The above code will give the following result.

Result: 3

In the following Python program we are performing floor division using two negative numbers. We have -10 and -3 and we will get 3 as the result of -10//-3.

We get 3.33333... on dividing -10 by -3.
Floor(3.333...) = 3

# variables
x = -10
y = -3

# operation
result = x // y

# output
print("Result:", result)

The above code will give the following result.

Result: 3

In the following Python program we are performing floor division using a positive and a negative number. We have 10 and -4 and we will get -3 as the result of 10//-4.

We get -2.5 on dividing 10 by -4.
Floor(-2.5) = -3

# variables
x = 10
y = -4

# operation
result = x // y

# output
print("Result:", result)

The above code will give the following result.

Result: -3
Share