×

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

Exrex Python

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...

4 minutes read.

SKLearn Clustering

These are ml methods thatare responsible for detecting patterns and the similarities within the data.The clustering methods are unsupervised.Here the data is clustered to form groups with the help of...

3 minutes read.

Palindrome program in Python

Palindrome program in python A number or string is said to be a palindrome if we invert the number or string and the string or number remains the same as the...

3 minutes read.

Syntax of Map function in Python

In this tutorial, we will understand what the map function in Python is meant. Further, we will see the syntax to be used for the same in python language. We...

3 minutes read.

What is a Script in Python

Have you ever heard that Python is a scripting language? Or when people address a Python program file as a script? Besides programming, script generally means "a written story for...

3 minutes read.

Python Strong Number

Strong Number- Logic Divide each of the number's digits into separate units to determine if it is a strong number or not.The factorial of each digit must then be determined. The...

5 minutes read.

Python locals() function

Python locals() function The locals() function in Python updates and returns a dictionary representing the current local symbol table. Syntax locals() Parameter NA Return This function returns the local symbol table as a dictionary or returns free...

1 minute read.

How to create a file in Python?

How to create a file in python? Files provide us a convenient way of storing our information. Once we have our data, the next task is to store it so that...

3 minutes read.

Python System Command

To execute a program in Python, we need to execute some shell commands to run our program on the computer. Python will provide some shell commands in our background to...

3 minutes read.

Python Support

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.

Why learn Python?

All things considered, learning python would be extraordinary, it'll acquaint you with the universe of dynamic programming dialects, if you're somebody who has had a semester of involvement with C. Python...

4 minutes read.

Python len() function

Python len() function The len() function in Python  returns the number of items in an object. Syntax len(s) Parameter s: This parameter represents a sequence (such as a string, bytes, tuple, list, or range) or...

1 minute read.

Python String lstrip() method

Python String lstrip() method The string. lstrip () method in Python returns a copy of the string with leading characters removed (based on the string argument passed). Syntax string.lstrip([chars]) Parameter chars(optional): This parameter represents a...

1 minute read.

Python String strip() method

Python String strip() method The string. strip() method in Python removes any leading (spaces at the beginning) and trailing (spaces at the end) characters (space is the default leading character to...

1 minute read.

Algorithm for Factorial of a number in Python

What is a Factorial Number? In mathematics, the factorial of a positive integer n, denoted by n!. It is the product of all positive integers less than or equal to n. For...

3 minutes read.

Convert List to Array in Python

What is List in Python? In Python, a list is a built-in data structure that stores a collection of items. The items in a list can be of any data type,...

3 minutes read.

What is Collaborative Filtering in ML, Python

Introduction Contents recommendation is a useful tactic for almost any specific technology looking to increase interest, but it frequently calls for a lot of user data and perhaps laborious content tagging...

3 minutes read.

Python Logistic Regression with Sklearn & Scikit

In this article, we'll walk through a tutorial for utilising the Python Sklearn (formerly known as Scikit Learn) package to implement logistic regression. To assist you in remembering the concept,...

18 minutes read.

Spell Corrector GUI using Tkinter in Python

GUI: A graphical interface (GUI) is a user interface that lets users interact with electronic devices like computers and smartphones by using menus, icons, and other visual cues (graphics). In contrast...

3 minutes read.

Python Bitwise Operators

When used with variables and objects in an expression, operators are tokens that carry out calculations. The variables and items to which the computation is applied are known as operands....

5 minutes read.