On Day 6, we learned how to unlock Python's vast ecosystem of pre-built tools by using modules. Instead of writing everything from scratch, we can import powerful code written by others to add new features to our programs instantly.
- Cheat Sheet:
day_6_cheatsheet.md
- The
importStatement: How to bring a module's functionality into our program. - The
randomModule: For generating random numbers (randint), making random choices from lists (choice), and shuffling sequences (shuffle). - The
mathModule: For accessing mathematical constants (pi) and functions (sqrt). - The
datetimeandtimeModules: For getting the current date/time and pausing program execution (sleep). - Different Import Styles: Using
from...importandimport...asfor cleaner code.
import random
import time
# A list of possible responses
responses = ["Yes, definitely.", "My reply is no.", "Ask again later."]
print("Thinking...")
time.sleep(2) # Create a dramatic pause
# Use a function from the 'random' module to pick a response
fortune = random.choice(responses)
print(f"The Magic 8-Ball says: '{fortune}'")