Lesson 1: io ๐Ÿ“‚

Learning Target: I can use the io module to perform file operations in memory.

Explanation

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())

Your Turn!

Try creating an in-memory binary stream and write some data to it.

Try it here:

Lesson 2: os ๐Ÿ–ฅ๏ธ

Learning Target: I can use the os module to perform operating system tasks.

Explanation

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")

Your Turn!

Try creating a new directory and listing its contents.

Try it here:

Lesson 3: os.path ๐Ÿ“‚

Learning Target: I can use the os.path module to work with file paths.

Explanation

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"))

Your Turn!

Try checking if a directory exists and create it if it doesn't.

Try it here:

Lesson 4: sys ๐Ÿงพ

Learning Target: I can use the sys module to handle system-specific functionalities.

Explanation

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()

Your Turn!

Try writing a script that takes a command-line argument and prints it.

Try it here:

Lesson 5: Math ๐Ÿ“

Learning Target: I can use the math module to perform mathematical operations.

Explanation

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))

Your Turn!

Try using the math module to calculate the floor and trunc values of a number.

Try it here:

Lesson 6: isnan, sqrt, isqrt, and pi ๐Ÿ“

Learning Target: I can use the isnan, sqrt, isqrt, and pi functions from the math module.

Explanation

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)

Your Turn!

Try using the math module to calculate the power of a number using math.pow.

Try it here:

Lesson 7: datetime โฐ

Learning Target: I can use the datetime module to work with dates and times.

Explanation

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"))

Your Turn!

Try using the datetime module to get the name of the current weekday.

Try it here:

Lesson 8: Random with Numbers ๐ŸŽฒ

Learning Target: I can use the random module to generate random numbers.

Explanation

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())

Your Turn!

Try using the random module to shuffle a list of numbers.

Try it here:

Lesson 9: Random with Lists ๐Ÿ“‹

Learning Target: I can use the random module to work with lists.

Explanation

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))

Your Turn!

Try using the random module to get a sample of elements from a list.

Try it here: