Categories: python

Python Mathematical Operators

Mathematical Operators:

Mathematical Operators are used in mathematics to perform operations such as addition, subtraction, multiplication, and division.

Python has seven arithmetic operators:

  • Addition
  • Subtraction
  • Multiplication
  • Division
  • Modulus
  • Exponentiation
  • Floor division
Addition:

The addition operator in Python is +. It is used to combine two values.

Example:

v1 = 2
v2 = 30
  
# using the addition operator
res = v1 + v2
print(res)

Output:

32
Subtraction:

The subtraction operator in Python is -. It’s used to deduct the second value from the first.

Example:

v1 = 20
v2 = 3

# using the subtraction operator
res = v1 - v2
print(res)

Output:

17
Multiplication:

The multiplication operator in Python is *. It is used to compute the product of two values.

Example:

v1 = 20
v2 = 3

# using the multiplication operator
res = v1 * v2
print(res)

Output:

60
Division:

The division operator in Python is /. When the first operand is divided by the second, it yields the quotient.

Example:

v1 = 20
v2 = 3

# using the division operator
res = v1 / v2
print(res)

Output:

6.666666666666667
Modulus:

The modulus operator in Python is %. When the first operand is divided by the second, it returns the remainder.

Example:

v1 = 20
v2 = 3

# using the division operator
res = v1 % v2
print(res)

Output:

2
Exponentiation:

** is the Python exponentiation operator. It is used to raise the first operand to the power of the second operand.

Example:

v1 = 20
v2 = 3

# using the division operator
res = v1 ** v2
print(res)

Output:

8000
Floor division:

//  is used in Python to perform floor division. It is used to find the quotient’s floor when the first operand is divided by the second.

Example:

v1 = 22
v2 = 3

# using the division operator
res = v1 // v2
print(res)

Output:

7

Note: also read about Python Syntax Rules & Hello World Program

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Recent Posts

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

4 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

4 months ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

10 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

10 months ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

10 months ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

11 months ago