Python Mathematics Course
- Built-in Math Functions
Python provides several built-in math functions that cover a range of basic operations.
The abs() Function
- Description: Returns the absolute value of a number.
- Syntax: abs(x)
- Examples:
print(abs(-5)) # Output: 5 print(abs(3.14)) # Output: 3.14
The pow() Function
- Description: Raises a number to a specified power. It can also take a third argument for modulus.
- Syntax: pow(base, exp, mod=None)
- Examples:
print(pow(2, 3)) # Output: 8 print(pow(2, 3, 5)) # Output: 3 (8 mod 5)
The round() Function
- Description: Rounds a number to a specified number of decimal places.
- Syntax: round(number, ndigits=None)
- Examples:
print(round(3.14159, 2)) # Output: 3.14 print(round(2.71828)) # Output: 3
The divmod() Function
- Description: Returns a tuple containing the quotient and the remainder of the division.
- Syntax: divmod(a, b)
- Examples:
print(divmod(9, 4)) # Output: (2, 1) print(divmod(10, 3)) # Output: (3, 1)
- The math Module
The math module provides additional mathematical functions. To use it, you need to import it.
Importing the math Module
- Syntax: import math
Basic Functions
The math.sqrt() Function
- Description: Returns the square root of a number.
- Syntax: math.sqrt(x)
- Examples:
import math print(math.sqrt(16)) # Output: 4.0 print(math.sqrt(2)) # Output: 1.4142135623730951
The math.pow() Function
- Description: Raises a number to a power.
- Syntax: math.pow(x, y)
- Examples:
import math print(math.pow(2, 3)) # Output: 8.0 print(math.pow(4, 0.5)) # Output: 2.0
The math.log() Function
- Description: Returns the logarithm of a number with a specified base (default is base e).
- Syntax: math.log(x, base)
- Examples:
import math print(math.log(10)) # Output: 2.302585092994046 (base e) print(math.log(100, 10)) # Output: 2.0 (base 10)
The math.exp() Function
- Description: Returns e raised to the specified power.
- Syntax: math.exp(x)
- Examples:
import math print(math.exp(1)) # Output: 2.718281828459045 print(math.exp(0)) # Output: 1.0
Trigonometric Functions
The math.sin() Function
- Description: Returns the sine of an angle in radians.
- Syntax: math.sin(x)
- Examples:
import math print(math.sin(math.pi / 2)) # Output: 1.0 print(math.sin(math.pi)) # Output: 1.2246467991473532e-16
The math.cos() Function
- Description: Returns the cosine of an angle in radians.
- Syntax: math.cos(x)
- Examples:
import math print(math.cos(0)) # Output: 1.0 print(math.cos(math.pi)) # Output: -1.0
The math.tan() Function
- Description: Returns the tangent of an angle in radians.
- Syntax: math.tan(x)
- Examples:
import math print(math.tan(0)) # Output: 0.0 print(math.tan(math.pi / 4)) # Output: 0.9999999999999999
Additional Functions
The math.factorial() Function
- Description: Returns the factorial of a non-negative integer.
- Syntax: math.factorial(x)
- Examples:
import math print(math.factorial(5)) # Output: 120
The math.gcd() Function
- Description: Returns the greatest common divisor of two integers.
- Syntax: math.gcd(a, b)
- Examples:
import math print(math.gcd(48, 18)) # Output: 6
The math.degrees() and math.radians() Functions
- Description: Converts between radians and degrees.
- Syntax:
- math.degrees(x)
- math.radians(x)
- Examples:
import math print(math.degrees(math.pi)) # Output: 180.0 print(math.radians(180)) # Output: 3.141592653589793
Conclusion
This course has introduced you to the main mathematical functions available in Python through its built-in functions and the math module. You have learned to use functions for basic calculations, working with logarithms, square roots, trigonometric functions, and more. Feel free to experiment with these functions and combine them to solve more complex mathematical problems.