Learning

Python Standard Library

Python Standard Library

math Module

python
print(math.pi) # 3.141592653589793 print(math.e) # 2.718281828459045 print(math.sqrt(16)) # 4.0 print(math.ceil(4.1)) # 5 print(math.floor(4.9)) # 4 print(math.factorial(5)) # 120 print(math.gcd(12, 18)) # 6 print(math.log2(8)) # 3.0 print(math.inf) # Infinity

random Module

python
random.seed(42) # Reproducible results print(random.random()) # 0.0 to 1.0 print(random.randint(1, 10)) # 1 to 10 (inclusive) print(random.uniform(1.5, 5.5)) # 1.5 to 5.5 # Choose from sequence print(random.choice(['a', 'b', 'c'])) print(random.choices([1,2,3], weights=[10,1,1], k=4)) # Weighted with replacement print(random.sample(range(100), 5)) # 5 unique random items # Shuffle in-place cards = ['A', 'K', 'Q', 'J'] random.shuffle(cards)

datetime Module

python
# Current time now = datetime.now() today = date.today() # Create specific date dob = date(1995, 5, 15) # Date arithmetic future = now + timedelta(days=30) past = now - timedelta(hours=2) # Difference (returns timedelta) diff = now - dob print(diff.days) # Days between dates # Formatting (datetime → string) print(now.strftime('%Y-%m-%d %H:%M:%S')) # '2024-01-15 14:30:00' # Parsing (string → datetime) parsed = datetime.strptime('2024/01/15', '%Y/%m/%d')

os Module

python
# Paths print(os.getcwd()) # Current directory os.chdir('/tmp') # Change directory print(os.path.exists('file.txt')) # True/False print(os.path.isfile('file.txt')) # Is it a file? print(os.path.isdir('folder')) # Is it a directory? # Path manipulation (prefer os.path over string concat) full_path = os.path.join('folder', 'subfolder', 'file.txt') print(os.path.basename(full_path)) # 'file.txt' print(os.path.dirname(full_path)) # 'folder/subfolder' # Environment variables print(os.environ.get('PATH')) os.environ['MY_VAR'] = 'value' # List directory print(os.listdir('.'))

sys Module

python
print(sys.argv) # Command line arguments print(sys.version) # Python version print(sys.platform) # 'linux', 'win32', 'darwin' print(sys.getsizeof([1,2,3])) # Memory size in bytes sys.exit(0) # Exit script
Key Rules
  • •Use random.seed() for reproducible results during testing — omit it for true randomness in production
  • •Use os.path.join() for cross-platform paths — never hardcode '/' or '\\' for paths
  • •datetime.strptime() parses strings to dates; strftime() formats dates to strings — don't mix them up
  • •sys.argv[0] is the script name, sys.argv[1:] are the actual arguments passed by the user
  • •Use math.inf and -math.inf instead of float('inf') for cleaner code
Your Task

Create a function `generate_password(length=12)` that generates a secure random password containing uppercase, lowercase, digits, and symbols. Use `random.SystemRandom()` for cryptographic security (don't use plain `random`). Create a function `days_until(target_date_str)` that takes a 'YYYY-MM-DD' string and returns the number of days from today.

EditorPython · JSX
PreviewUpdates on Run Tests
Loading preview…
Tests
Should import string module
Should define generate_password function
Should use random.SystemRandom()
Should use string constants (ascii_letters, digits)
Should define days_until function
Should use strptime for parsing