Open In App

How To Do Math in Python 3 with Operators?

Last Updated : 18 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Python3 provides us data types like integer and float along with various operators to perform mathematical calculations for graph plotting, machine learning algorithms, Statistical, data analytical purposes. An operator is a symbol that performs specific operations, which is very useful if one is frequently dealing with numbers. Operator precedence specifies the order in which operators are evaluated when two or more operators with different precedence are adjacent in an expression.  Below is a quick reference table for Unary Operators and Arithmetic Operator.

Operators in Python

Type  Operator Name Example Description
 Urinary Operator      – Minus    – x Negates the numeric argument
      + Plus   + x numeric argument unchanged.
      ~ Invert – (x + 1) bit-wise inversion of x
Arithmetic Operator     + Addition    x + y Add two operands using ‘+’ operator in between
       – Subtraction    x – y subtract two operands using the ‘-‘ operator in between
       * Multiplication    x * y Multiply two operands using ‘*’ operator in between
      / Division    x / y Divide left operand with right operand using ‘/’ operator
      // Floor Division   x // y

Divide left operand with right operand using ‘//’ operator

and provide the only quotient as an output

     ** Exponentiation   x ** y Exponentiation (power) x to the power y
      % Modulo   x % y

Divide left operand with right operand using ‘%’ operator

and provide the only remainder as an output

Operator Precedence

Operator Meaning Associativity
      ** Exponent Right-to-left
     ~x  Bitwise NOT (Invert) Left-right
  +x, -x Unary plus, Unary minus Left-right
*, /, //, % Multiplication, Division, Floor division, Modulus Left-right
     +, –  Addition, Subtraction Left-right

Example 1: Unary Operators.

Python3




a = 2.202
b = -2.202
 
# This inverts the sign
# for both integer as
# well as float type
c = -a
print("Minus operator value 1:", c)
c = -b
print("Minus operator value 2:", c)
 
# This does not inverts the sign
# for both integer as well
# as float type
c = +a
print("Plus operator value 1:", c)
c = +b
print("Plus operator value 2:", c)
 
a = 2
b = -2
 
# This inverts the sign only
# for integer type as perform
# operation as per this '-(x + 1)'.
c = ~a  # -(2 + 1)
print("Invert operator value 1:", c)
c = ~b     # -(-2 + 1)
print("Invert operator value 2:", c)


Output: 

Minus operator value 1: -2.202
Minus operator value 2: 2.202
Plus operator value 1: 2.202
Plus operator value 2: -2.202
Invert operator value 1: -3
Invert operator value 2: 1

Example 2: Addition Operator.

Python3




a = 4
b = -5
 
# This + operator performs
# addition of two operands
# or numbers
d = a + b
print("Addition value 1:", d)
 
a = [1, 2, 3, 4, 5]
b = [6, 7, 8, 9, 10]
d = []
for j in range(len(a)):
    d.append(a[j] + b[j])
 
print("Addition value 2:", d)


Output: 

Addition value 1: -1
Addition value 2: [7, 9, 11, 13, 15]

Example 3: Subtraction Operator.

Python3




a = 4
b = -5
 
# This - operator performs
# subtraction of two operands
# or numbers
d = a - b
print("Subtraction value 1:",d)
 
a = [ 1 ,4,5]
b = [1, 2, 3]
print("Subtraction values:")
for i in range(len(a)) :
  print(a[i] - b[i])


Output: 

Subtraction value 1: 9
Subtraction values:
0
2
2

Example 4: Multiplication Operator.

Python3




a = 4
b = -5
c = 5.02
 
# This * operator performs
# Multiplication of two
# operands or numbers
d = a * b
print("Multiplication value 1:", d)
 
d = a * c
print("Multiplication value 2:", d)


Output

Multiplication value 1: -20
Multiplication value 2: 20.08

Example 5: Division Operator.

Python3




a = 20
b = -5
c = 5.02
 
# This '/' operator performs
# Division of two operands
# or numbers
d = a / b
print("Division value 1:", d)
 
d = a / c
print("Division value 2:", d)


Output:

Division value 1: -4.0
Division value 2: 3.9840637450199208

Example 6: Floor Division Operator.

Python3




a = 20
b = -5
c = 5.02
 
# This // operator performs
# Floor Division of two
# operands or numbers
d = a // b
print("Floor Division value 1:", d)
 
d = a // c
print("Floor Division value 2:", d)


Output:

Floor Division value 1: -4
Floor Division value 2: 3.0

Example 7: Exponential Operator.

Python3




a = 5
b = 3
c = -3
 
# This ** operator performs
# Exponential operation of two
# operands or numbers
d = a ** b
print("Exponent value 1:", d)
 
d = a ** c
print("Exponent value 2:", d)


Output:

Exponent value 1: 125
Exponent value 2: 0.008

Example 8: Modulo Operator.

Python3




a = 12 
b = 5
c = 3
 
# This % operator performs Modulus
# of two operands or numbers and
# return the remainder
d = a % b  
print("Modulus value 1:", d)
 
d = c % b
print("Modulus value 2:", d)


Output

Modulus value 1: 2
Modulus value 2: 3


Similar Reads

Python Value Error :Math Domain Error in Python
Errors are the problems in a program due to which the program will stop the execution. One of the errors is 'ValueError: math domain error' in Python. In this article, you will learn why this error occurs and how to fix it with examples. What is 'ValueError: math domain error' in Python?In mathematics, we have certain operations that we consider un
4 min read
Python math.sqrt() function | Find Square Root in Python
sqrt() function returns square root of any number. It is an inbuilt function in Python programming language. In this article, we will learn more about the Python Program to Find the Square Root. sqrt() Function We can calculate square root in Python using the sqrt() function from the math module. In this example, we are calculating the square root
3 min read
Division Operators in Python
Division Operators allow you to divide two numbers and return a quotient, i.e., the first number or number at the left is divided by the second number or number at the right and returns the quotient. Division Operators in PythonThere are two types of division operators: Float divisionInteger division( Floor division)When an integer is divided, the
5 min read
Python | Operators | Question 1
What is the output of the following code : C/C++ Code print 9//2 (A) 4.5 (B) 4.0 (C) 4 (D) Error Answer: (C)Explanation: The ‘//’ operator in Python returns the integer part of the floating number. Quiz of this QuestionPlease comment below if you find anything wrong in the above post
1 min read
Python | Operators | Question 2
Which function overloads the >> operator? (A) more() (B) gt() (C) ge() (D) None of the above Answer: (D) Explanation: rshift() overloads the >> operatorQuiz of this QuestionPlease comment below if you find anything wrong in the above post
1 min read
Python | Operators | Question 3
Which operator is overloaded by the or() function? (A) || (B) | (C) // (D) / Answer: (B) Explanation: or() function overloads the bitwise OR operatorQuiz of this QuestionPlease comment below if you find anything wrong in the above post
1 min read
Python | Operators | Question 4
What is the output of the following program : C/C++ Code i = 0 while i (A) 0 2 1 3 2 4 (B) 0 1 2 3 4 5 (C) Error (D) 1 0 2 4 3 5 Answer: (C)Explanation: There is no operator ++ in Python Quiz of this QuestionPlease comment below if you find anything wrong in the above post
1 min read
Python | Solve given list containing numbers and arithmetic operators
Given a list containing numbers and arithmetic operators, the task is to solve the list. Example: Input: lst = [2, '+', 22, '+', 55, '+', 4] Output: 83 Input: lst = [12, '+', 8, '-', 5] Output: 15 Below are some ways to achieve the above tasks. Method #1: Using Iteration We can use iteration as the simplest approach to solve the list with importing
3 min read
Python | Splitting operators in String
Sometimes we have a source string to have certain mathematical statement for computations and we need to split both the numbers and operators as a list of individual elements. Let's discuss certain ways in which this problem can be performed. Method #1 : Using re.split() This task can be solved using the split functionality provided by Python regex
7 min read
Merging and Updating Dictionary Operators in Python 3.9
Python 3.9 is still in development and scheduled to be released in October this year. On Feb 26, alpha 4 versions have been released by the development team. One of the latest features in Python 3.9 is the merge and update operators. There are various ways in which Dictionaries can be merged by the use of various functions and constructors in Pytho
3 min read
Article Tags :
Practice Tags :