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

Generate Parenthesis | Intuition + Code | Recursion Tree | Backtracking | Java

Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…

3 months ago

Square Root of Integer

Given an integer A. Compute and return the square root of A. If A is…

1 year ago

Build Array From Permutation

Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…

1 year ago

DSA: Heap

A heap is a specialized tree-based data structure that satisfies the heap property. It is…

2 years ago

DSA: Trie

What is a Trie in DSA? A trie, often known as a prefix tree, is…

2 years ago

Trees: Lowest Common Ancestor

What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…

2 years ago