Python Math Module

 


Python Math Module

Python has a built-in module that you can use for mathematical tasks. The math module has a set of methods and constants.

Math.ceil():- The math.ceil() method rounds a number UP to the nearest integer, if necessary, and returns the result.

Example

Round a number upward to its nearest integer:

 # Import math library

import math

# Round a number upward to its nearest integer
print(math.ceil(1.4))
print(math.ceil(5.3))
print(math.ceil(-5.3))
print(math.ceil(22.6))
print(math.ceil(10.0))

Output:

2

6

-5

23

10

Math.floor():- The math.floor() method rounds a number DOWN to the nearest integer, if necessary, and returns the result.

Example

Round numbers down to the nearest integer:

# Import math library
import math

# Round numbers down to the nearest integer
print
(math.floor(0.6))
print(math.floor(1.4))
print(math.floor(5.3))
print(math.floor(-5.3))
print(math.floor(22.6))
print(math.floor(10.0))

Output:

0

1

5

-6

22

10

Math.fabs():- The math.fabs() method returns the absolute value of a number, as a float. Absolute denotes a non-negative number. This removes the negative sign of the value if it has any. Unlike Python abs(), this method always converts the value to a float value.

#Import math Library
import math

#Print absolute values from numbers
print
(math.fabs(-66.43))
print(math.fabs(-7))

Output:

66.43

7.0

Math.factorial():- The math.factorial() method returns the factorial of a number.

Note: This method only accepts positive integers.

The factorial of a number is the sum of the multiplication, of all the whole numbers, from our specified number down to 1. For example, the factorial of 6 would be 6 x 5 x 4 x 3 x 2 x 1 = 720

Example

Find the factorial of a number:

#Import math Library
import math

#Return factorial of a number
print
(math.factorial(9))
print(math.factorial(6))
print(math.factorial(12))

print(math.factorial(5))

Output:

362880

720

479001600

120

Math.fsum():- The math.fsum() method returns the sum of all items in any iterable (tuples, arrays, lists, etc.).

Example

Return the sum of all items:

# Import math Library
import math

# Print the sum of all items
print
(math.fsum([12345]))
print(math.fsum([100400340500]))
print(math.fsum([1.70.31.54.5]))

Output:

15

1340

8

Example

Return the sum of all items:

# Import math Library
import math
a = [
12345]

B = (100400340500)
# Print the sum of all items
print
(math.fsum(a)
print(math.fsum(B)
Output:

15

1340

Math.sqrt():- The math.sqrt() method returns the square root of a number.

Note: The number must be greater than or equal to 0.

Example

Find the square root of different numbers:

# Import math Library
import math

# Return the square root of different numbers
print
 (math.sqrt(9))
print (math.sqrt(25))
print (math.sqrt(16))

Output:

3

5

4

Math.pow():- The math.pow() method returns the value of x raised to power y. If x is negative and y is not an integer, it returns a ValueError. This method converts both arguments into a float.

Tip: If we use math.pow(1.0,x) or math.pow(x,0.0), it will always returns 1.0.

Example

Find the value of 9 raised to the power of 3:

# Import math Library
import math

#Return the value of 9 raised to the power of 3
print
(math.pow(93))

print(math.pow(103))

Output:

729

1000

Math.fmod():- The math.fmod() method returns the remainder (modulo) of x/y.

Example

Return the remainder of x/y:

# Import math Library
import math

# Return the remainder of x/y
print
(math.fmod(204))
print(math.fmod(203))
print(math.fmod(156))
print(math.fmod(-103))
print(math.fmod(00))

Output:

729

0

2

3

-1

Math Methods

Method

Description

math.acos()

Returns the arc cosine of a number

math.acosh()

Returns the inverse hyperbolic cosine of a number

math.asin()

Returns the arc sine of a number

math.asinh()

Returns the inverse hyperbolic sine of a number

math.atan()

Returns the arc tangent of a number in radians

math.atan2()

Returns the arc tangent of y/x in radians

math.atanh()

Returns the inverse hyperbolic tangent of a number

math.ceil()

Rounds a number up to the nearest integer

math.comb()

Returns the number of ways to choose k items from n items without repetition and order

math.copysign()

Returns a float consisting of the value of the first parameter and the sign of the second parameter

math.cos()

Returns the cosine of a number

math.cosh()

Returns the hyperbolic cosine of a number

math.degrees()

Converts an angle from radians to degrees

math.dist()

Returns the Euclidean distance between two points (p and q), where p and q are the coordinates of that point

math.erf()

Returns the error function of a number

math.erfc()

Returns the complementary error function of a number

math.exp()

Returns E raised to the power of x

math.expm1()

Returns Ex - 1

math.fabs()

Returns the absolute value of a number

math.factorial()

Returns the factorial of a number

math.floor()

Rounds a number down to the nearest integer

math.fmod()

Returns the remainder of x/y

math.frexp()

Returns the mantissa and the exponent, of a specified number

math.fsum()

Returns the sum of all items in any iterable (tuples, arrays, lists, etc.)

math.gamma()

Returns the gamma function at x

math.gcd()

Returns the greatest common divisor of two integers

math.hypot()

Returns the Euclidean norm

math.isclose()

Checks whether two values are close to each other, or not

math.isfinite()

Checks whether a number is finite or not

math.isinf()

Checks whether a number is infinite or not

math.isnan()

Checks whether a value is NaN (not a number) or not

math.isqrt()

Rounds a square root number downwards to the nearest integer

math.ldexp()

Returns the inverse of math.frexp() which is x * (2**i) of the given numbers x and i

math.lgamma()

Returns the log gamma value of x

math.log()

Returns the natural logarithm of a number, or the logarithm of number to base

math.log10()

Returns the base-10 logarithm of x

math.log1p()

Returns the natural logarithm of 1+x

math.log2()

Returns the base-2 logarithm of x

math.perm()

Returns the number of ways to choose k items from n items with order and without repetition

math.pow()

Returns the value of x to the power of y

math.prod()

Returns the product of all the elements in an iterable

math.radians()

Converts a degree value into radians

math.remainder()

Returns the closest value that can make numerator completely divisible by the denominator

math.sin()

Returns the sine of a number

math.sinh()

Returns the hyperbolic sine of a number

math.sqrt()

Returns the square root of a number

math.tan()

Returns the tangent of a number

math.tanh()

Returns the hyperbolic tangent of a number

math.trunc()

Returns the truncated integer parts of a number

Math Constants

Constant

Description

math.e

Returns Euler's number (2.7182...)

math.inf

Returns a floating-point positive infinity

math.nan

Returns a floating-point NaN (Not a Number) value

math.pi

Returns PI (3.1415...)

math.tau

Returns tau (6.2831...)

 See the all math Modules Please visit the Python official website https://docs.python.org/3/library/math.html

<<Previous                                                  Next>>

कोई टिप्पणी नहीं

टिप्पणी: केवल इस ब्लॉग का सदस्य टिप्पणी भेज सकता है.

Send Multiple Emails From Excel | Send Bulk Mail from Excel Sheet with Attachment in One Click

Send Multiple Emails From Excel | Send Bulk Mail from Excel Sheet with Attachment in One Click Download VBA Code Notepad file - Click here D...

Blogger द्वारा संचालित.