×

Python Arithmetic Operators

An arithmetic operator is a mathematical operator that is used to operate on two operands. Based on the operator used, action is performed on the operands, and output is delivered.

Following are the seven Arithmetic operators used in Python: -

  1. Addition operator
  2. Subtraction operator
  3. Multiplication operator
  4. Division operator
  5. Modulus operator
  6. Exponentiation operator
  7. Floor division operator

 Now, let’s dive deeper and understand each of the above-mentioned operators:

1. Addition operator

In python, ‘+’ is used as the addition operator. It takes two operands. Operands can be both numbers positive as well as negative., It is also capable of adding two strings. The addition of string using ‘+’ is called string concatenation.

For Example-

  • z = “ABC” + “def” ( string concatenation is performed )

The solution here will be ABCdef.

  • x= - 5, y = - 7;

z = x + y; (addition of negative numbers )

Here, z = (-5) + (-7);

So, the answer would be -12.

Example 1: Implementation showing addition operator on two positive numbers.

Arithmetic Operators in Python

Output:

Arithmetic Operators in Python

Example 2:  Implementation showing addition operator on two strings.

Arithmetic Operators in Python

it simply adds both the operands and gives out the result.

Output:

Arithmetic Operators in Python

Example 3: Implementation showing addition operator on two negative numbers.

Arithmetic Operators in Python

Output:

Arithmetic Operators in Python

2. Subtraction operator

 ‘-’  is used as the subtraction operator in python. It also takes two operands which should be a number positive or negative. It is not capable of supporting string operations.

For Example-

  • x = -9, y=4

z = x - y ;

Here, z = (-9)- 4;

So, the answer would be -13

Example 1: Implementation showing the usage of subtraction operator

Arithmetic Operators in Python

Output:

Arithmetic Operators in Python

Example 2: Implementation showing the usage of subtraction operator on two strings.

Arithmetic Operators in Python

Output:

Arithmetic Operators in Python

The above code throws an error as the subtraction operator is not capable of performing string operations.

3. Multiplication operator

In python, ‘*’ is used as a multiplication operator.   It finds the product of two desired numbers.

For Example-

x = 6, y = 5; 

z = x * y

Here, 5 * 6;

So, the answer would be 30.

Example 1: Implementation showing the usage of multiplication operator on two strings

Arithmetic Operators in Python

Output:         

Arithmetic Operators in Python

4. Division operator

In python, ‘/ ’ is used as a division operator. It gives out the quotient on dividing value 1 with value 2 or vice versa.

For Example-

x = 34, y = 5;

z = x / y;

Here, 34 / 5;

So, the answer would be 6.

Example 1: Implementation showing the usage of division operator on two positive integers.

Arithmetic Operators in Python

Output:

Arithmetic Operators in Python

5. Modulus operator

 In python, ‘ % ’ is used as a modulus operator. It gives out the remainder when value 1 divides the value 2.

For Example-

x = 24, y = 5;

z = x % y;

Here, 20 % 5

So, the answer would be 4.

Example 1: Implementation showing the usage of modulus operator on two positive integers.

Arithmetic Operators in Python

Output:

Arithmetic Operators in Python

6. Exponentiation operator

  ‘**’ is the exponentiation operator used in Python. This operator is used to raise the first operand to the power of the second.
For Example-

x = 3, y=2;

z = x ** y;

Here, z = 3**2 (that is 3^2)

So, the answer would be 9.

Example 1: Implementation showing the usage of an exponential operator on two positive integers.

Arithmetic Operators in Python

Output:

Arithmetic Operators in Python

7. Floor division operator

 In Python, ‘ // ’ is used to perform the floor division. It is used to calculate the floor of the quotient when the first operand is divided by the second.
For Example-

x = 5, y = 2;

z = x // y;

Here, 5//2

the answer would be 2.

Example 1: Implementation showing the usage of an exponential operator on two positive integers.

Arithmetic Operators in Python

Output:

Arithmetic Operators in Python

Summary:

We have seen all the seven arithmetic operators supported in python and we have also seen its implementations on python IDE.

These operators are not much different from mathematical operators used outside the programming world.


Related Topics

Python Switch Case

What is a Switch Case Statement? In the programming languages, a switch statement is a logical loop or syntax that tests the variable's value and compares it with multiple cases. Once...

3 minutes read.

Python String rsplit() method

Python String rsplit() method The string.rsplit() method in Python splits a string into a list, starting from the right. If the "max" parameter is not specified, this method will return the...

1 minute read.

How to Import Files in Python

Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Reverse a sentence In Python

Introduction The built-in reverse() function is not supported by the Python string library. As we know, a Python string is defined as a set of Unicode characters and Python provides various...

4 minutes read.

Python pycountry

What is Pycountry? Python programming language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations...

4 minutes read.

Python Letter to Number

Python Letter to Number In this tutorial, we will convert the given letters into numbers using a Python. We will convert the given letter into the letter value as defined in...

3 minutes read.

How to set up a proxy using selenium in python

What is Proxy? A proxy acts as a middleman between user requests and server responses. The main purposes of proxies are to protect user privacy and encapsulate data between various interactive...

3 minutes read.

Python String startswith() method

Python String startswith() method The string.startswith() method in Python returns a boolean value ‘True’ if the given string starts with the prefix, otherwise it returns False. Syntax startswith(prefix[, start[, end]]) Parameter prefix: This parameter signifies the value to check. start(optional):...

1 minute read.

Python String rjust() method

Python String rjust() method The string.rjust() method in Python returns a right-justified string of a given minimum width where the padding is done using the specified fillchar (default is a space). It returns...

2 minutes read.

Android apps for coding in python

Nowadays, it is the generation of smartphones. The world can be seen and acknowledged with a single click on your mobile phone. Everyone uses mobile phones to do any work,...

9 minutes read.

Closest Pair of Points in Python

We are given an array of n points in the plane, and our task is to find the pair of points in the array that are the closest to each...

3 minutes read.

Calculator Program in Python

Simple Calculator Program in Python This example helps to learn how to create a simple calculator program to perform operations like addition, subtraction, multiplication, and division. Source Code The following is the source...

2 minutes read.

How to change the names of Columns in Python

Introduction To play with huge amounts of data, in Python we require a tool. The tool which is available in Python is Pandas. Pandas is an open-source library. It is used...

3 minutes read.

Image to NumPy Arrays in Python

Image is a simple process of working model confined to machine learning, Python works with the image in the data format of width and height i.e. Images are excelled into...

3 minutes read.

CatPlot in Python

Python Seaborn Library Seaborn is a superb Python tool for displaying graphical statistics graphing. Seaborn provides different color schemes and attractive default styles to facilitate the creation of various statistics charts...

8 minutes read.

Python String rindex() method

Python String rindex() method The string. rindex() method in Python returns the highest index of the substring inside the string (if found). If the substring is not found, it raises an...

2 minutes read.

How to Reverse a number in Python?

In Python, conditional statements can be combined with the list() method, slice() method, recursion() method, and other predefined techniques to generate reverse logical functions. The reverse() method is the most...

7 minutes read.

How to create a DataFrames in Python

How to create a DataFrames in Python Data Frame is a data structure in which data is stored in tabular form. They can also be referred as two-dimensional collection of data. Various...

5 minutes read.

Accuracy_score Function in Sklearn

A crucial stage in data science is measuring our model's performance using the appropriate metric. In this article, we will examine two methods for calculating the accuracy of your predictions:...

13 minutes read.

Python lock

Before understanding the concept of Python-lock we need to understand what kind if condition we require to implement the locks in Python. Let us start with multithreading and its implementation...

5 minutes read.