Python Modules with the dir() Function

Let's get to know the dir() function and how it helps us explore Python modules. This function provides a sorted list of all names available in a module when the module has been imported entirely using the import instruction (not from module import...). By using dir(module), you can discover the names of functions, classes, and other entities within the module. Here's an example using the math module:

import math

for name in dir(math):
    print(name, end="\t")
OUTPUT:

Running this code will show the names of entities within the math module, including familiar ones like sin(), cos(), and sqrt(). You might also notice names starting with "__" - we'll learn more about them when we explore writing our own modules. While using dir() directly in code might not be very common, it's handy when you want to check a module's contents before writing your code. Additionally, you can execute the dir() function directly in the Python console IDLE to explore modules without creating a separate script.

Selected Functions From The Math Module

Let's take a quick look at some essential functions provided by the math module. Although we've chosen only a few, remember that there are many more valuable functions available. You can explore them further on your own time.

Trigonometry Functions

Methods Description Example Output
sin(x) Calculate the sine of the angle 'x' in radians from math import sin, pi print(round(sin(pi/6), 2)) 0.50
cos(x) Calculate the cosine of the angle 'x' in radians from math import cos, pi print(round(cos(pi/2), 2)) 0.00
tan(x) Determines the tangent of the angle 'x' in radians from math import tan, pi print(round(tan(pi/4), 2)) 1.00

Note: Be cautious with tan(), as it may not accept all arguments.

Inverse Trigonometry Functions

Methods Description
asin(x) Calculates the arcsine of 'x' (returns the angle in radians)
acos(x) Computes the arccosine of 'x' (returns the angle in radians)
atan(x) Determines the arctangent of 'x' (returns the angle in radians)

Circular Functions

Methods Description
sinh(x) Calculate the hyperbolic sine
cosh(x) Calculate the hyperbolic cosine
tanh(x) Calculate the hyperbolic tangent
asinh(x) Calculate the hyperbolic arcsine
acosh(x) Calculate the hyperbolic arcosine
atanh(x) Calculate the hyperbolic arctangent

Simplifying Angle measurements

Methods Description
pi A constant with an approximate value of π (pi)
radians(x) A function that converts the angle 'x' from degrees to radians.
degrees(x) Performs the opposite conversion, from radians to degrees.

Functions Related to Exponentiation and Logarithm

Methods Description
e A constant with a value that is an approximation of Euler's number
exp(x) Finding the value of ex
log(x) The natural logarithm of x
log(x, b) The natural logarithm of x to base b
log10(x) The decimal logarithm of x (more precise than log(x,10))
log2(x) The decimal logarithm of x (more precise than log(x,2))

General-Purpose Mathematical Operations

Methods Description
ceil(x) The ceiling of x (the smallest integer greater than or equal to x)
floor(x) The floor of x (the largest integer less than or equal to x)
trunc(x) The value of x truncated to an integer (be careful - it's not an equivalent either of ceil or floor)
factorial(x) Returns x! (x has to be an integral and not a negative)
hypot(x, y) Returns the length of the hypotenuse of a right-angle triangle with the leg lengths equal to x and y (the same as sqrt(pow(x, 2) + pow(y, 2)) but more precise)

Examples

1. Write a python code that convert 90o to radians.

$$ \begin{aligned} 1^\circ &= \frac{\pi}{180}\text{ radians} \\ \\ 90^\circ \text{ radians}&= 90^\circ \times \frac{\pi}{180} \\ \\ &= \frac{\pi}{2} \\ \\ 90^\circ \text{ radians}&\approx 1.5707 \end{aligned} $$
from math import pi, radians
  print(radians(90))
OUTPUT:

2. Write a python code that convert $\frac{\pi}{4}$ radians to degrees.

$$ \begin{aligned} 1 \text{ radians} &= \frac{180}{\pi}^\circ \\ \\ \frac{\pi}{4} \text{ radians}&= \frac{\pi}{180} \times \frac{180}{\pi} \\ \\ &= 45^\circ \end{aligned} $$
from math import pi, degrees
  print(degrees(pi/4))
OUTPUT:

3. What is the floor and ceiling of 1.4 and 2.6?

$$ \text{Let x=1.4 and y=2.6} $$ floor and ceiling
from math import ceil, floor, trunc

x = 1.4
y = 2.6

print(floor(x), floor(y))
print(floor(-x), floor(-y))
print(ceil(x), ceil(y))
print(ceil(-x), ceil(-y))
print(trunc(x), trunc(y))
print(trunc(-x), trunc(-y))
OUTPUT:

Conclusion

In this exploration of selected Python modules, we learned about the dir() function, which helps us explore the contents of Python modules. It provides a sorted list of names available in a module when the module has been imported entirely using the import instruction.

Using the math module as an example, we saw how the dir(math) function displays a list of functions, classes, and other entities within the module. This can be useful to check a module's contents before writing code.

We then delved into some essential functions from the math module, categorized into various mathematical operations. Some of these functions include trigonometry functions like sin(x), cos(x), and tan(x), inverse trigonometry functions like asin(x), acos(x), and atan(x), as well as circular functions like sinh(x), cosh(x), and tanh(x). Additionally, the module provides functions for simplifying angle measurements with pi, radians(x), and degrees(x), as well as functions related to exponentiation and logarithm such as exp(x), log(x), log(x, b), log10(x), and log2(x). We also explored general-purpose mathematical operations like ceil(x), floor(x), trunc(x), factorial(x), and hypot(x, y).

To reinforce our understanding, we practiced some examples using these functions. We converted angles between degrees and radians, and we found the floor and ceiling values for given numbers.

Overall, the math module in Python provides a wide range of mathematical functions that can be extremely valuable when working on various computational tasks, scientific calculations, and more. By understanding and utilizing these functions, Python programmers can efficiently perform complex mathematical operations and solve a diverse set of problems.

End Of Article

End Of Article