How To Do Math in Python 3 with Operators? - GeeksforGeeks

18 Apr, 2022
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.
Divide left operand with right operand using ‘//’ operator
and provide the only quotient as an output
Divide left operand with right operand using ‘%’ operator
and provide the only remainder as an output
Example 1: Unary Operators.
Python3
a = 2.202b = -2.202 # This inverts the sign# for both integer as# well as float typec = -aprint("Minus operator value 1:", c)c = -bprint("Minus operator value 2:", c) # This does not inverts the sign# for both integer as well# as float typec = +aprint("Plus operator value 1:", c)c = +bprint("Plus operator value 2:", c) a = 2b = -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 = 4b = -5 # This + operator performs# addition of two operands# or numbersd = a + bprint("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 = 4b = -5 # This - operator performs# subtraction of two operands# or numbersd = a - bprint("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 = 4b = -5c = 5.02 # This * operator performs# Multiplication of two# operands or numbersd = a * bprint("Multiplication value 1:", d) d = a * cprint("Multiplication value 2:", d)
Multiplication value 1: -20
Multiplication value 2: 20.08
Example 5: Division Operator.
Python3
a = 20b = -5c = 5.02 # This '/' operator performs# Division of two operands# or numbersd = a / bprint("Division value 1:", d) d = a / cprint("Division value 2:", d)
Output:
Division value 1: -4.0
Division value 2: 3.9840637450199208
Example 6: Floor Division Operator.
Python3
a = 20b = -5c = 5.02 # This // operator performs# Floor Division of two# operands or numbersd = a // bprint("Floor Division value 1:", d) d = a // cprint("Floor Division value 2:", d)
Output:
Floor Division value 1: -4
Floor Division value 2: 3.0
Example 7: Exponential Operator.
Python3
a = 5b = 3c = -3 # This ** operator performs# Exponential operation of two# operands or numbersd = a ** bprint("Exponent value 1:", d) d = a ** cprint("Exponent value 2:", d)
Output:
Exponent value 1: 125
Exponent value 2: 0.008
Example 8: Modulo Operator.
Python3
a = 12 b = 5c = 3 # This % operator performs Modulus# of two operands or numbers and# return the remainderd = a % b  print("Modulus value 1:", d) d = c % bprint("Modulus value 2:", d)
Modulus value 1: 2
Modulus value 2: 3

varshagumber28
ruhelaa48
sumitgumber28
Python-Operators
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Check if element exists in list in Python
How To Convert Python Dictionary To JSON?
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Python | Get unique values from a list
Python | os.path.join() method
Create a directory in Python
Defaultdict in Python
Python | Pandas dataframe.groupby()