Learning Target: I can use the io module to perform file operations in memory.
The io module enables users to store and access data from memory, eliminating the need to work with physical files.
import io
# Create an in-memory text stream
text_stream = io.StringIO("Hello, world!")
# Read from the text stream
print(text_stream.read())
Try creating an in-memory binary stream and write some data to it.
Try it here:
Learning Target: I can use the os module to perform operating system tasks.
The os module allows users to perform various operating system-based actions, such as renaming files and executing shell commands.
import os
# List files in the current directory
print(os.listdir("."))
# Rename a file
os.rename("old_file.txt", "new_file.txt")
Try creating a new directory and listing its contents.
Try it here:
Learning Target: I can use the os.path module to work with file paths.
The os.path module provides methods to check for the existence of a file or directory in a specific location.
import os.path
# Check if a file exists
print(os.path.exists("example.txt"))
# Join paths
print(os.path.join("folder", "file.txt"))
Try checking if a directory exists and create it if it doesn't.
Try it here:
Learning Target: I can use the sys module to handle system-specific functionalities.
The sys module provides access to system-specific functionalities, such as managing files and handling command-line arguments.
import sys
# Print command-line arguments
print(sys.argv)
# Exit the program
sys.exit()
Try writing a script that takes a command-line argument and prints it.
Try it here:
Learning Target: I can use the math module to perform mathematical operations.
The math module provides various math functions such as fabs, ceil, floor, trunc, fmod, frexp, and nan.
import math
# Calculate the square root
print(math.sqrt(16))
# Calculate the ceiling of a number
print(math.ceil(4.2))
Try using the math module to calculate the floor and trunc values of a number.
Try it here:
Learning Target: I can use the isnan, sqrt, isqrt, and pi functions from the math module.
The math module helps solve complex math problems related to statistics, geometry, trigonometry, and others.
import math
# Check if a value is NaN
print(math.isnan(float('nan')))
# Calculate the square root
print(math.sqrt(25))
# Calculate the integer square root
print(math.isqrt(25))
# Calculate the value of pi
print(math.pi)
Try using the math module to calculate the power of a number using math.pow.
Try it here:
Learning Target: I can use the datetime module to work with dates and times.
The datetime module provides various methods to work with dates and times, such as now, strftime, and weekday.
import datetime
# Get the current date and time
now = datetime.datetime.now()
print(now)
# Format the date and time
print(now.strftime("%Y-%m-%d %H:%M:%S"))
Try using the datetime module to get the name of the current weekday.
Try it here:
Learning Target: I can use the random module to generate random numbers.
The random module offers multiple methods to generate random numbers or values from a given list of values.
import random
# Generate a random integer
print(random.randint(1, 10))
# Generate a random number between 0 and 1
print(random.random())
Try using the random module to shuffle a list of numbers.
Try it here:
Learning Target: I can use the random module to work with lists.
The random module can also be used with words and phrases, offering functions such as shuffle, choice, and sample.
import random
# Shuffle a list
my_list = [1, 2, 3, 4, 5]
random.shuffle(my_list)
print(my_list)
# Choose a random element from a list
print(random.choice(my_list))
Try using the random module to get a sample of elements from a list.
Try it here: