Python Modules and Libraries Quiz
1. Given the following code snippet:
import math print(math.pi)
What will this code output?
2. Which Python library would you use if you need to work with dates and times? Write an example code snippet that uses this library to get the current date and time.
3. The following code snippet:
import os os.system('mkdir new_directory')
is used to do what in Python?
4. Consider the following code snippet:
import random print(random.randint(1, 100))
What is the range of potential outputs from this script?
5. What is the difference between `import module` and `from module import function` in Python? Provide a brief explanation and an example code snippet for each.
6. What does the following code snippet do?
import numpy as np a = np.array([1, 2, 3]) b = np.array([4, 5, 6]) print(np.dot(a, b))
7. How would you use the `requests` library to send a GET request to `https://www.example.com`? Write a code snippet that sends this request and prints the HTTP status code of the response.
8. Given the following code snippet:
import pandas as pd data = pd.read_csv('data.csv') print(data.head())
What will this script do when run?
9. Write a code snippet that uses the `json` library to parse the following JSON string into a Python dictionary: `'{ “name”:”John”, “age”:30, “city”:”New York”}’`.
10. How would you use the `matplotlib.pyplot` library to plot the graph of a function, say `y = x^2`, for `x` ranging from -10 to 10? Write a code snippet that accomplishes this.