Mathematical functions in python with examples w3schools
Mathematical functions in python with examples w3schools
I'll make sure to respond in English only for this request.
W3Schools provides a comprehensive tutorial on mathematical functions in Python, which are essential for performing various calculations and operations. Here's an overview of the mathematical functions in Python along with examples:
1. Trigonometric Functions:
math.sin(x)
: Returns the sine of x (measured in radians). math.cos(x)
: Returns the cosine of x (measured in radians). math.tan(x)
: Returns the tangent of x (measured in radians).
Example:
import math
print(math.sin(math.pi / 2)) # Output: 1.0
print(math.cos(0)) # Output: 1.0
print(math.tan(math.pi / 4)) # Output: 1.0
2. Exponential and Logarithmic Functions:
math.exp(x)
: Returns the value of e^x. math.log(x)
: Returns the natural logarithm of x.
Example:
import math
print(math.exp(1)) # Output: 2.718281828459045
print(math.log(10)) # Output: 2.302585092994046
3. Hyperbolic Functions:
math.sinh(x)
: Returns the hyperbolic sine of x. math.cosh(x)
: Returns the hyperbolic cosine of x.
Example:
import math
print(math.sinh(1)) # Output: 0.4621171572605613
print(math.cosh(0)) # Output: 1.0
4. Power and Root Functions:
math.pow(x, y)
: Returns x raised to the power of y. math.sqrt(x)
: Returns the square root of x.
Example:
import math
print(math.pow(2, 3)) # Output: 8.0
print(math.sqrt(9)) # Output: 3.0
5. Statistical Functions:
math.ceil(x)
: Rounds x upwards to the nearest integer. math.floor(x)
: Rounds x downwards to the nearest integer.
Example:
import math
print(math.ceil(2.8)) # Output: 3.0
print(math.floor(3.5)) # Output: 3.0
These are some of the essential mathematical functions in Python, which can be used to perform various calculations and operations. W3Schools provides more detailed tutorials and examples for each function, making it a valuable resource for learning Python programming.
Hope this helps!
What is a math function in Python with an example?
I'd be happy to explain!
In Python, you can use built-in mathematical functions to perform various operations, such as calculating the absolute value, square root, or trigonometric values.
Here's an example of how to use some of these functions:
import math
Absolute Value
print(abs(-5)) # Output: 5
Square Root
print(math.sqrt(64)) # Output: 8.0
Trigonometric Functions
print(math.sin(math.pi / 2)) # Output: 1.0
print(math.cos(math.pi / 4)) # Output: 0.7071067811865475
print(math.tan(math.pi / 3)) # Output: 1.7320508075688772
Exponential and Logarithmic Functions
print(math.exp(10)) # Output: 22026.465794366614
print(math.log(1000)) # Output: 6.9087639696914395
Factorial Function (Note: This is only available in Python 3.8+)
print(math.perm(4, 2)) # Output: 6
In this example:
We useabs
to calculate the absolute value of a number. We use math.sqrt
to calculate the square root of a number. We use trigonometric functions like math.sin
, math.cos
, and math.tan
to calculate values based on angles (in radians). We use exponential functions like math.exp
to calculate exponential values, and logarithmic functions like math.log
to calculate logarithmic values.
These are just a few examples of the many mathematical operations available in Python. You can find more detailed information about these functions, along with others, on the official Python documentation page for the math
module.
It's worth noting that some of these functions might not be exact due to floating-point precision limitations (like math.sqrt(2)
).