search

How to perform mathematical operations in Python?

Python provides several operators and built-in functions to perform mathematical operations. 

Here are some common mathematical operations in Python:

Arithmetic operators:

  • Addition: + operator adds two numbers. Example: a + b
  • Subtraction: - operator subtracts one number from another. Example: a - b
  • Multiplication: * operator multiplies two numbers. Example: a * b
  • Division: / operator divides one number by another. Example: a / b
  • Floor Division: // operator performs integer division, discarding the decimal part. Example: a // b
  • Modulo: % operator returns the remainder of the division. Example: a % b
  • Exponentiation: ** operator raises a number to the power of another. Example: a ** b

Assignment operators: Arithmetic operators can be combined with the assignment operator (=) to update the value of a variable. Example: a += b is equivalent to a = a + b.

Comparison operators: Comparison operators are used to compare two values and return a Boolean result (True or False):

  • Equal to: == operator checks if two values are equal. Example: a == b
  • Not equal to: != operator checks if two values are not equal. Example: a != b
  • Greater than: > operator checks if the left value is greater than the right value. Example: a > b
  • Less than: < operator checks if the left value is less than the right value. Example: a < b
  • Greater than or equal to: >= operator checks if the left value is greater than or equal to the right value. Example: a >= b
  • Less than or equal to: <= operator checks if the left value is less than or equal to the right value. Example: a <= b

Math functions: Python provides several built-in math functions in the math module. To use these functions, you need to import the math module first. Example: import math

Here are some commonly used math functions:

  • math.sqrt(x): Returns the square root of x.
  • math.floor(x): Returns the largest integer less than or equal to x.
  • math.ceil(x): Returns the smallest integer greater than or equal to x.
  • math.abs(x): Returns the absolute value of x.
  • math.sin(x), math.cos(x), math.tan(x): Returns the trigonometric sine, cosine, and tangent of x, respectively.
  • math.exp(x): Returns the exponential value of x.
  • math.log(x): Returns the natural logarithm (base e) of x.

Here's an example demonstrating the use of arithmetic operators and math functions:

import math

a = 5
b = 3

# Arithmetic operations
sum = a + b
difference = a - b
product = a * b
quotient = a / b
floor_division = a // b
remainder = a % b
power = a ** b

# Math functions
square_root = math.sqrt(a)
rounded_value = math.floor(3.7)
absolute_value = math.abs(-4)
sine_value = math.sin(math.pi / 2)

print(sum, difference, product, quotient, floor_division, remainder, power)
print(square_root, rounded_value, absolute_value, sine_value)

This will output:

8 2 15 1.6666666666666667 1 2 125
2.23606797749979 3 4 1.0

By using these operators and functions, you can perform various mathematical calculations in Python.

Related Questions You Might Be Interested