Practice Top 30 Python Programming Questions on Modules and Packages
Q1. Write a program to calculate the square root of 16 using the math module.
- Input: None
- Expected Output: 4.0
Q2. Write a program to generate a random integer between 1 and 10 using the random module.
- Input: None
- Expected Output: Random number between 1 and 10
Q3. Write a program to list all files in the current directory using the os module.
- Input: None
- Expected Output: A list of filenames (e.g., ['file1.txt', 'file2.py'])
Q4. Write a program to display the current working directory using the os module.
- Input: None
- Expected Output: Current working directory path (e.g., /home/user)
Q5. Write a program to display today’s date using the datetime module.
- Input: None
- Expected Output: Current date (e.g., 2024-12-04)
Q6. Write a module math_utils.py with a function add(a, b) that returns the sum of two numbers. Import and use it in a script to calculate 5 + 7.
- Input: None
- Expected Output: 12
Q7. Create a module math_ops.py with functions add(a, b) and multiply(a, b). Use both functions in a script to calculate 2 + 3 and 2 3.
- Input: None
- Expected Output:
Addition: 5
Multiplication: 6
Q8. Write a module greet.py with a function hello(name). Import it with an alias and call hello("Alice").
- Input: None
- Expected Output: "Hello, Alice!"
Q9. Write a program to modify a custom module config.py and reload it using importlib.reload.
- Input: Modify the greeting variable in config.py
- Expected Output: Updated value of greeting
Q10. Write a module constants.py with a constant PI = 3.14. Import it and calculate the circumference of a circle with radius 5.
- Input: None
- Expected Output: 31.400000000000002
Q11. Create a package mypackage with an __init__.py file and a module module1.py containing a function say_hello(). Import it and call say_hello().
- Input: None
- Expected Output: "Hello from module1!"
Q12. Create a package mypackage with submodules module1 and module2. Call functions from both submodules in a script.
- Input: None
- Expected Output: Outputs from functions in module1 and module2.
Q13. Write a program to import and call specific functions from a package mypackage.
- Input: None
- Expected Output: Outputs from the imported functions.
Q14. Write a program to dynamically import a module math and call the sqrt() function.
- Input: None
- Expected Output: Output of math.sqrt(16)
Q15. Create a package mypackage with two modules module1 and module2. Use relative imports to access functions between them.
- Input: None
- Expected Output: Outputs from functions using relative imports.
Q16. Write a program to use the Template class from the string module to substitute values into a template.
- Input: {'name': 'Alice'}
- Expected Output: "Hello, Alice!"
Q17. Write a program to count the occurrences of elements in a list using collections.Counter.
- Input: [1, 2, 2, 3, 3, 3]
- Expected Output: Counter({3: 3, 2: 2, 1: 1})
Q18. Write a program to generate all permutations of [1, 2, 3] using itertools.
- Input: None
- Expected Output: [(1, 2, 3), (1, 3, 2), ...]
Q19. Write a program to find all occurrences of Python in the string "I love Python. Python is fun!".
- Input: None
- Expected Output: ['Python', 'Python']
Q20. Write a program to measure the time it takes to execute a loop with time module.
- Input: None
- Expected Output: Execution time in seconds.
Q21. Write a program to make a GET request to https://api.github.com using the requests library.
- Input: None
- Expected Output: Response data (e.g., status code 200)
Q22. Write a program to parse the string {"name": "Alice", "age": 25} into a Python dictionary.
- Input: None
- Expected Output: {'name': 'Alice', 'age': 25}
Q23. Write a program to serialize a dictionary into a JSON string and deserialize it back.
- Input: {'key': 'value'}
- Expected Output: Serialized: {"key": "value"}, Deserialized: {'key': 'value'}
Q24. Write a program to create a file example.txt in the current directory using pathlib.
- Input: None
- Expected Output: File example.txt created.
Q25. Write a program to fetch the value of the environment variable HOME.
- Input: None
- Expected Output: Path to home directory.
Q26. Write a program to use lru_cache from the functools module to cache the results of a function that calculates the factorial of a number.
- Input: 5, then 5 again
- Expected Output: First call: 120, Second call: Cached result: 120
Q27. Create a package utilities with two modules:
- string_utils.py: A function reverse_string(s) to reverse a string.
- math_utils.py: A function square(n) to calculate the square of a number.
Import and use these in a script.
- Input: reverse_string("hello"), square(4)
- Expected Output: "olleh", 16
Q28. Write a program to use pkg_resources to check the version of the installed numpy package.
- Input: None
- Expected Output: "numpy version: "
Q29. Create a package shapes with the structure:
shapes/
__init__.py
circle.py
rectangle.py
- circle.py: A function area(radius) to calculate the area of a circle.
- rectangle.py: A function area(length, width) to calculate the area of a rectangle.
Import and use these functions in a script.
- Input: circle.area(5), rectangle.area(4, 3)
- Expected Output: 78.5, 12
Q30. Create a package mytools and write a setup.py script to install it using pip. Include a module text_tools.py with a function word_count(text) to count the words in a string. Demonstrate its usage after installation.
- Input: "Hello world!"
- Expected Output: 2
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!