×

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

How to Convert Int to String in Python?

Every value we use or store in a variable in Python will have a specific data type. It describes the value's nature; based on that, Python will automatically assign a...

4 minutes read.

Python List copy() method

Python List copy () method The list.copy () method returns a shallow copy of the list. Syntax list.copy() Parameter NA Return This function returns the copy for the given list. Example 1 # Python program explaining # the list.copy() method # passing the...

1 minute read.

Python Set symmetric_difference() method

Python Set symmetric_difference() method The set.symmetric_difference() method returns a new set, which is the symmetric difference of two sets. The returned set contains only the unique items and, hence, deleting the common elements of...

2 minutes read.

Working with files in Python

Python is an object-oriented high-level programming language. Python has dynamic semantics and has high-level built-in data structures which support dynamic typing and dynamic binding. Python provides rapid development. It has an...

5 minutes read.

Pillow Python introduction and setup

Introduction Advanced image processing implies handling the picture carefully with the assistance of a PC. Utilizing picture handling we can perform activities like upgrading the picture, obscuring the picture, separating text...

4 minutes read.

Tensorflow Angular in Python

TensorFlow is an open-source, Python-compatible toolkit for numerical computation that accelerates and simplifies the creation of neural networks and machine learning algorithms. They make the interesting notion that models can be...

6 minutes read.

Python String decode() method

Python String decode() method The string.decode() method decodes the string using the codec registered for encoding. Default encoding is the current default string encoding. It may raise errors to set a different error handling...

1 minute read.

What is Python 2

Python is a widely used high-level language. The initial work on developing python was begun in the late 1980s. In 1989, Guido Van Rossum started to work on it. Initially,...

3 minutes read.

List Comprehension in Python

Sometimes we need new lists that are completely based on previously existing lists, so to perform this operation successfully, some of the programming languages come with a syntactic construct known...

5 minutes read.

Exit program in python

Exit program in python The quit(), exit(), and sys.exit() functions have almost the same functionality as they raise the SystemExit exception by which the Python interpreter exit and no stack traceback...

2 minutes read.

Python String istittle() method

Python String istittle() method The string.istittle() method returns a boolean value true if the string is a titlecased string and there is at least one character, for example uppercase characters may...

2 minutes read.

List Index out of Range Python for Loop

The List is generally used in Python to store multiple items or elements inside one single variable. The List is ordered in nature, the List can contain the elements inside...

3 minutes read.

GUI Calendar 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 program to count the number of a substring in a string

Python program to count the number of a substring in a string A part of the string is called a substring. This article explains the Python program to find how many...

1 minute read.

How to handle exceptions in python

Prerequisite: Exceptions and errors in Python What are Exceptions? Exceptions are run-time errors that unexpectedly occur and crash a program. When occurred, it alters the flow of the program. These errors are...

9 minutes read.

Python whois

What is whois? Whois is a protocol used to identify the owner of the registered domain name. It is a querying database that is used to record the registered users. WHOIS is...

4 minutes read.

Python Control Flow Statements

This article aims to introduce you to what control flow statements are in general and Control Flow Statements in Python programming Language, the Importance of control flow statements and look...

3 minutes read.

Unit Testing in Python

The process of testing whether a particular unit is working properly or not is called “UNIT TESTING”. A unit test will check small components in your application. The first and...

7 minutes read.

Filter List in Python

Python: Python is one of the most used programming languages, as it is used widely in software and data analysis, web development, etc. It is said to be a user-friendly programming...

3 minutes read.

Python String

Python String Python string is considered as the most popular data type present in Python language. In Python, string data type is the collection of characters, letters, white spaces etc. written...

31 minutes read.