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

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

1 day ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

3 weeks ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

3 weeks ago

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

4 weeks ago

Autocomplete System Implementation

Build an autocomplete system that, given a query string s and a set of possible…

4 weeks ago

Job Scheduler Implementation

Design a job scheduler that accepts a function f and an integer n. The scheduler…

4 weeks ago