Arithmetic Operators in Python [With Examples]

0
190
arithmetic operators in python

This post was last Updated on May 9, 2022 by Himanshu Tyagi to reflect the accuracy and up-to-date information on the page.

In this tutorial, you will learn about arithmetic operators in Python with examples. In Python, you can use arithmetic operators to perform Mathematical operations such as addition, multiplication, division, etc.

Python arithmetic operators are binary, which means that it requires two operands to operate.  The value on which the operators operate is known as operands. For example, in this case, x = 3 + 4 3 & 4 are the two operands, and addition (+) is the arithmetic operator.

Also ReadHow to Create Python Empty Set

Arithmetic Operators in Python [With Examples]

arithmetic operators in python

  • Addition (+)
  • Subtraction (-) 
  • Multiplication (*)
  • Division (/)
  • Floor Division (//)
  • Modulus (%)
  • Exponentiation (**)

Also ReadPython Matrix Using Numpy [With Examples]

1. Addition Operator

To find the sum of two numbers in Python, you can use the addition operator, represented by the + symbol.

Syntax: a + b

Example:

a = 10
b = 3
print("Sum of two numbers using addition operator is ", a + b)

Output:

Sum of two numbers using addition operator is 13

Also ReadHow To Create An Empty Dictionary in Python

2. Subtraction Operator

Use the subtraction operator in Python to find the difference between the two numbers. You can perform a subtraction operator by using the – symbol.

Syntax: a – b

Example:

a = 10
b = 3
print("Difference of two numbers using subtraction operator is ", a - b)

Output:

Difference of two numbers using subtraction operator is 7

Also ReadHow to Exit Python Program [4 Methods]

3. Multiplication Operator

To calculate the product of two numbers, you can use the multiplication operator, represented by the * symbol.

Syntax: a * b

Example:

a = 10
b = 3
print("Multiplication of two numbers is ", a * b)

Output:

Multiplication of two numbers is 30

Also ReadHow to Convert Python Tuples to Lists

4. Division Operator

You can use the division operator to find the quotient when the first operand is divided by the second. The / symbol represents it.

Syntax: a / b

Example:

a = 10
b = 3
print("Division of two is ", a / b)

Output:

Division of two is 3.3333333333333335

Also ReadHow to Handle String Index Out of Range Error In Python

5. Floor Division Operator

To find the floor of the quotient when the first operand is divided by the second, you can use the floor division operator, which is // in Python.

Syntax: x // y

Example:

a = 10
b = 3
print("Floor division of two numbers is ", a // b)

Output:

Floor division of two numbers is 3

Also ReadPython For Loop Index With Examples

6. Modulus Operator

The modulus operator (%)

Syntax: a % b

Example:

a = 10
b = 3
print("Remainder when a is divided by b is", a % b)

Output:

Remainder when a is divided by b is 1

Also ReadHow to Reverse an Array In Python [Flip Array]

7. Exponentiation Operator

Syntax: a ** b

Example:

a = 10
b = 3
print("a raised to the power b is", a ** b)

Output:

a raised to the power b is 1000

Also ReadHow to Check If Dict Has Key in Python [5 Methods]

Python program to do arithmetical operations

num1 = 34
num2 = 5
sum = num1 + num2
print("Addition of two numbers", sum)
subtraction = num1 - num2
print("Subtraction of two numbers", subtraction)
multiply = num1 * num2
print("Multiplication of two numbers", multiply)
division = num1 / num2
print("Division of two numbers", division)
floordivision = num1 // num2
print("Floor division of two numbers", floordivision)
exponentiation = num1 ** num2
print("Exponentiation of two numbers", exponentiation)
modulus = num1 % num2
print("Modulus of two numbers", modulus)

Output:

Addition of two numbers 39
Subtraction of two numbers 29
Multiplication of two numbers 170
Division of two numbers 6.8
Floor division of two numbers 6
Exponentiation of two numbers 45435424
Modulus of two numbers 4

Wrapping Up

In this tutorial, we explored arithmetic operators in Python with examples. Python’s arithmetic operators let you perform several Mathematical operations such as addition, multiplication, division, etc. Try to build a simple Python calculator to practice how arithmetic operators work in Python.

Next Read: