Number-theoretic Functions
What You'll Learn: In this tutorial, you'll discover various math functions available in Python's math module. These functions help you perform a wide range of mathematical tasks.
Number-theoretic Functions
math.ceil()- Rounds a number up to the nearest integer.Example:
pythonimport math x = math.ceil(2.3) print(x) # Output: 3
math.comb(n, k)- Returns the number of ways to choosekitems fromnitems without repetition and order.Example:
pythonimport math x = math.comb(5, 2) print(x) # Output: 10
math.copysign(x, y)- Returnsxwith the sign ofy.Example:
pythonimport math x = math.copysign(3, -1) print(x) # Output: -3.0
math.fabs(x)- Returns the absolute value ofx.Example:
pythonimport math x = math.fabs(-5.5) print(x) # Output: 5.5
math.factorial(x)- Returns the factorial ofx.Example:
pythonimport math x = math.factorial(4) print(x) # Output: 24
math.floor(x)- Rounds a number down to the nearest integer.Example:
pythonimport math x = math.floor(2.9) print(x) # Output: 2
math.fmod(x, y)- Returns the remainder ofxdivided byy.Example:
pythonimport math x = math.fmod(7, 3) print(x) # Output: 1.0
math.frexp(x)- Returns the mantissa and exponent ofx.Example:
pythonimport math x = math.frexp(8) print(x) # Output: (0.5, 4)
math.fsum(iterable)- Returns an accurate floating-point sum of values in an iterable.Example:
pythonimport math x = math.fsum([0.1, 0.1, 0.1]) print(x) # Output: 0.30000000000000004
math.gcd(a, b)- Returns the greatest common divisor ofaandb.Example:
pythonimport math x = math.gcd(8, 12) print(x) # Output: 4
math.isclose(a, b)- Returns True ifais close tob.Example:
pythonimport math x = math.isclose(1.001, 1.002, abs_tol=0.01) print(x) # Output: True
math.isfinite(x)- Returns True ifxis a finite number.Example:
pythonimport math x = math.isfinite(10) print(x) # Output: True
math.isinf(x)- Returns True ifxis an infinity.Example:
pythonimport math x = math.isinf(float('inf')) print(x) # Output: True
math.isnan(x)- Returns True ifxis a NaN (not a number).Example:
pythonimport math x = math.isnan(float('nan')) print(x) # Output: True
math.isqrt(n)- Returns the integer square root ofn.Example:
pythonimport math x = math.isqrt(16) print(x) # Output: 4
math.lcm(a, b)- Returns the least common multiple ofaandb.Example:
pythonimport math x = math.lcm(6, 8) print(x) # Output: 24
math.ldexp(x, i)- Returnsx * (2**i).Example:
pythonimport math x = math.ldexp(2.5, 3) print(x) # Output: 20.0
math.modf(x)- Returns the fractional and integer parts ofx.Example:
pythonimport math x = math.modf(3.14) print(x) # Output: (0.14, 3.0)
math.nextafter(x, y)- Returns the next floating-point value afterxtowardsy.Example:
pythonimport math x = math.nextafter(1, 2) print(x) # Output: 1.0000000000000002
math.perm(n, k)- Returns the number of ways to choose and arrangekitems fromnitems without repetition.Example:
pythonimport math x = math.perm(5, 2) print(x) # Output: 20
math.prod(iterable)- Returns the product of all elements in the iterable.Example:
pythonimport math x = math.prod([1, 2, 3, 4]) print(x) # Output: 24
math.remainder(x, y)- Returns the IEEE 754-style remainder ofxdivided byy.Example:
pythonimport math x = math.remainder(7, 3) print(x) # Output: 1.0
math.trunc(x)- Returns the truncated integer part ofx.Example:
pythonimport math x = math.trunc(3.14) print(x) # Output: 3
math.ulp(x)- Returns the value of the least significant bit ofx.Example:
pythonimport math x = math.ulp(1.0) print(x) # Output: 2.220446049250313e-16
Try It Yourself: Fun Exercises
- Calculate Areas:
- Use the
math.piconstant andmath.pow()function to calculate the area of a circle given its radius.
- Use the
- Explore Trigonometric Functions:
- Experiment with
math.sin(),math.cos(), andmath.tan()to see how they work with different angles.
- Experiment with
- Work with Factorials:
- Use
math.factorial()to calculate the factorial of different numbers and understand how quickly the result grows.
- Use
Summary:
In this Python tutorial, we explored various number-theoretic functions available in the math module. These functions are useful for performing a wide range of mathematical operations in your programs. Keep experimenting and have fun with Python!