×

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 Project Ideas for Beginners

Python project ideas for beginners Advanced Python is an amazing platform to grow and achieve success as a developer in the future. Python is a programming language that can be very...

7 minutes read.

Python Online Compiler

Compose, Run and Offer Python code internet involving OneCompiler's Python online compiler free of charge. It's one of the hearty, highlight-rich web-based compilers for python language, supporting both the adaptations, which...

3 minutes read.

Python oct() function

Python oct() function The oct() function in Python converts an integer number to an octal string prefixed with “0o”. Syntax oct(x) Parameter x: This parameter represents an Integer Number Return This function returns an octal string. Example 1 #...

1 minute read.

PyPi TensorFlow

It is a free open-source library for high-performative numerical computations. The architecture of TensorFlow allows us for easy deployment and computing across a varied number of platforms and from desktops...

3 minutes read.

Python Call Function

In this article, you will learn about calling a function in Python. But before learning this, we should know about functions in Python.So, let’s get some overview of functions in...

6 minutes read.

How to set font for Text in Python

As a tuple with the font family as the first component, a size in point as the second, and possibly a string with one or more of the style modifiers...

5 minutes read.

Python Set isdisjoint() method

Python Set isdisjoint() method The set.isdisjoint() method in Python returns a boolean value True if two sets are disjoint sets ( i.e. none of the elements are present in both sets), otherwise it returns...

1 minute read.

Operator Overloading in Python

In any programming language, + is an arithmetic operator; it adds two numbers to give the sum. Have you ever tried to concatenate two strings? Concatenating two strings is adding...

5 minutes read.

Python List remove() method

Python List remove() method The list.remove () method in Python removes the item at the specified position in the given list. Syntax list.remove(x) Parameter x: This parameter represents the element you want to remove and accepts any type...

1 minute read.

Python String isspace() method

Python String isspace() method The string.isspace() method returns a Boolean value true if there are only whitespace characters in the given string. This function is used to check if the given...

2 minutes read.

Array to String in Python

What is an Array: The idea behind an array is to group several items of the same type, making it easier to locate each element by adding an extra offset to...

4 minutes read.

How to create a dictionary in Python?

How to create a dictionary in python Dictionary is a data structure in Python that represents our data in the form of keys and values. Each value in a dictionary can be...

5 minutes read.

Python Boolean

In this article, you will learn the boolean variables in python, bool() function in python, and bool operators with examples, Boolean Objects in Python. There are the only two possible values...

6 minutes read.

Python Program to Find the gcd of Two Numbers

Introduction Greatest Common Divisor is the full form of gcd. The greatest common divisor, or GCD, of two numbers, is a value that can exactly divide the two digits and is...

5 minutes read.

Python program to count and display vowels in a string

Python program to count and display vowels in a string This python program counts the vowels in a string and displays them on the screen. We can do this in several...

2 minutes read.

Joint Plot in Python

Introduction to Joint plots: The joint plot is the finest approach to evaluate both the specific distribution of each variable and also the connection between the two variables. Three different plots make...

4 minutes read.

Python List Methods

Python List Methods Python has a set of built-in methods that you can use on lists or arrays. Following are all of the methods of list objects: Methods Explanation append The list.append() method...

3 minutes read.

Python Operators

Operators in Python programming An operator is a symbol that operates on one or more operands. An operand is a value or a variable on which we perform the operation....

5 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 Set intersection_update() method

Python Set intersection_update() method The set.intersection_update() method in Python removes the items that is not present in both sets. It is different from the set.intersection() method, because the intersection()method returns a new set, with only  the common elements...

2 minutes read.