Exercise: Python

Questions for: Standard Libraries

What is the purpose of the math module?
A:
Working with regular expressions
B:
Performing mathematical operations
C:
Handling dates and times
D:
Parsing JSON data
Answer: B
import math

# Example usage of math module
result = math.sqrt(25)
print(f"Square root: {result}")
How can you use the random module in Python to generate a random integer between 1 and 10 (inclusive)?
A:
random.randint(1, 10)
B:
random.int(1, 10)
C:
rand.randint(1, 10)
D:
rand.random(1, 10)
Answer: A
import random

# Example usage of random module
random_number = random.randint(1, 10)
print(f"Random number: {random_number}")
What is the purpose of the json module?
A:
Working with binary data
B:
Parsing JSON data
C:
Handling dates and times
D:
Sorting elements in a list
Answer: B
import json

# Example usage of json module for parsing JSON data
json_data = '{"name": "John", "age": 30, "city": "New York"}'
parsed_data = json.loads(json_data)
print(parsed_data)
Which module in Python provides support for working with asynchronous I/O operations?
A:
asyncio
B:
asyncioio
C:
asyncioioops
D:
asyncops
Answer: A
import asyncio

# Example usage of asyncio for asynchronous I/O
async def main():
    print("Hello")
    await asyncio.sleep(1)
    print("World")

asyncio.run(main())
Which module in Python provides support for working with regular expressions in a more advanced and powerful manner than the re module?
A:
regexlib
B:
advancedre
C:
relib
D:
regex
Answer: D
import regex as re

# Example usage of regex module
pattern = re.compile(r'\b(\w+)\b')
text = "This is a sample text."
matches = pattern.findall(text)
print(matches)
Ad Slot (Above Pagination)
Quiz