Numerical Operations in Python

Numerical Operations in Python

This article explains numerical operations in Python.

You can learn about numerical types, an overview of built-in functions and modules, and an overview of mathematical functions in the math module.

YouTube Video

Numerical Operations in Python

Python is equipped with very powerful capabilities for numerical operations. Below is an explanation of basic numerical operations in Python.

Numeric Types

Python mainly has the following numerical types.

 1# Integer (int)
 2x = 10
 3y = -5
 4print("x =", x, "type:", type(x))  # <class 'int'>
 5print("y =", y, "type:", type(y))  # <class 'int'>
 6print("x + y =", x + y)            # 5
 7
 8# Float
 9a = 3.14
10b = -0.001
11print("\na =", a, "type:", type(a))  # <class 'float'>
12print("b =", b, "type:", type(b))    # <class 'float'>
13print("a * 2 =", a * 2)              # 6.28
14
15# Complex
16c = 2 + 3j
17d = 1 - 1j
18print("\nc =", c, "type:", type(c))   # <class 'complex'>
19print("d =", d, "type:", type(d))     # <class 'complex'>
20print("c + d =", c + d)               # (3+2j)
21print("c * d =", c * d)               # (5+1j)
  • int (integer type) represents integers such as 10 or -5.
  • float (floating-point type) represents numbers with decimal points, such as 3.14 or -0.001.
  • complex (complex number type) represents complex numbers, such as 2 + 3j. Here, j is the imaginary unit.

Built-in Functions and Modules

Python also provides many built-in functions and modules useful for numerical operations.

  • The abs(x) function returns the absolute value.
1result = abs(-10)  # result is 10
2print(result)

This code is an example of using the abs() function to get the absolute value of a number.

  • The round(x, n) function rounds the value to n decimal places.
1result = round(3.14159, 2)  # result is 3.14
2print(result)

This code is an example of using the round() function to round a number to a specified number of decimal places.

  • The math module provides more advanced mathematical functions.
1import math
2
3# result is 4.0 (square root)
4result = math.sqrt(16)
5print(result)
6
7# result is 1.0 (sine function)
8result = math.sin(math.pi / 2)
9print(result)

This code is an example of using the math module to perform advanced mathematical calculations such as square roots and trigonometric functions.

  • The decimal module supports high-precision decimal calculations.
1from decimal import Decimal
2
3# result is Decimal('0.3')
4result = Decimal('0.1') + Decimal('0.2')
5print(result)
6
7# The result is 0.30000000000000004 due to floating-point inaccuracies
8print(0.1 + 0.2)

This code is an example of avoiding floating-point errors and performing high-precision decimal calculations using the decimal module.

  • The fractions module handles rational numbers (fractions).
1from fractions import Fraction
2result = Fraction(1, 3) + Fraction(1, 6)  # result is Fraction(1, 2)
3print(result)

With these features, Python makes it easy to perform various numerical calculations. Due to the precision limitations of floating-point calculations, the decimal module is recommended for computations requiring higher precision.

Mathematical Functions in Python

In Python, there is a handy module called math for using mathematical functions. This module provides many functions necessary for performing mathematical calculations. Let's take a look at some representative functions of the math module.

Basic Mathematical Functions

  • math.sqrt(x): Returns the square root of x. x must be a non-negative number.
  • math.pow(x, y): Returns x raised to the power of y.
  • math.exp(x): Returns the exponential of x ((e^x)).
  • math.log(x[, base]): Calculates the natural logarithm or the logarithm to the specified base. If base is omitted, the natural logarithm is calculated.

Trigonometric Functions

  • math.sin(x): Returns the sine of x (in radians).
  • math.cos(x): Returns the cosine of x (in radians).
  • math.tan(x): Returns the tangent of x (in radians).

Inverse trigonometric functions

  • math.asin(x): Returns the arcsine (inverse sine) of x.
  • math.acos(x): Returns the arccosine (inverse cosine) of x.
  • math.atan(x): Returns the arctangent (inverse tangent) of x.

Hyperbolic Functions

  • math.sinh(x): Returns the hyperbolic sine of x.
  • math.cosh(x): Returns the hyperbolic cosine of x.
  • math.tanh(x): Returns the hyperbolic tangent of x.

Constants

  • math.pi: A constant that represents the ratio of a circle's circumference to its diameter ((\pi)).
  • math.e: A constant that represents the base of the natural logarithm ((e)).

Examples of Calculation

Here are some examples.

 1import math
 2
 3# Square root calculation
 4print(math.sqrt(16))  # Output: 4.0
 5
 6# Power calculation
 7print(math.pow(2, 3))  # Output: 8.0
 8
 9# Natural logarithm calculation
10print(math.log(math.e))  # Output: 1.0
11
12# Trigonometric functions
13degree = 45
14radian = math.radians(degree)  # Convert to radians
15print(math.sin(radian))  # Output: 0.7071067811865475 (approximately 1/√2)
16
17# Constants
18print(math.pi)  # Output: 3.141592653589793
19print(math.e)   # Output: 2.718281828459045
  • This code uses the math module to perform calculations such as square roots, exponentiation, natural logarithms, and trigonometric functions, and also displays constants like pi and Euler's number.

Summary

In this example, we used the math module, but Python also provides extensive support for advanced mathematical calculations and random number generation. By using libraries such as NumPy or SciPy, you can leverage more powerful and diverse mathematical functionalities.

You can follow along with the above article using Visual Studio Code on our YouTube channel. Please also check out the YouTube channel.

YouTube Video