top of page

Python Practice Set (Part 8): Exploring Modules and Standard Libraries

Updated: Sep 3

ree

Unlocking Python's Superpowers: A Guide to Modules and Libraries


Welcome to Part 8 of our Python journey! So far, we've built everything from scratch. But one of Python's greatest strengths is its "batteries-included" philosophy—it comes with a vast standard library of pre-written code, organized into files called modules. By learning how to import these modules, you can instantly gain new powers, from generating random numbers to performing complex math and working with dates. Let's explore how to use them!


1. The Random Number Guesser (The random Module)


Write a function guess_the_number that simulates a simple guessing game. The function should:

  1. Use the random module to generate a random integer between 1 and 10 (inclusive).

  2. Print the randomly generated number.

  3. For this exercise, we'll just show the concept, so no user input is needed.


2. High-Precision Circle Area (The math Module)


In a previous exercise, we used 3.14159 for π. The math module provides a much more accurate value. Write a function calculate_precise_area that takes a radius as input. Inside the function, import the math module and use math.pito calculate the circle's area. Return the result.


3. What Day is Today? (The datetime Module)


Write a function get_formatted_date that returns the current date as a formatted string. The function should:

  1. Import the date object from the datetime module.

  2. Get today's date using date.today().

  3. Format the date into the YYYY-MM-DD format (e.g., "2025-09-02") and return it as a string.


4. System Statistics: CPU Count (The os Module)


The os module lets your Python script interact with the operating system. Write a function get_cpu_count that imports the os module and uses it to find out how many CPUs are on the machine. The function should print a message like "This machine has 8 CPUs."

Hint: Look for a function within the os module that sounds like it would count CPUs.


5. Calculating the Average (The statistics Module)


You have a list of grades: grades = [88, 92, 100, 75, 85, 92]. Write a function calculate_average that takes a list of numbers, imports the statistics module, and uses the statistics.mean() function to calculate and return the average (mean) of the list. Print the result.



Comments


bottom of page