Lesson 36/48 ยท ๐ Real World Python
๐ Real World PythonLesson 36/48
Phase 7 ยท Real World Python18 min
Modules & Imports
Use Python's built-in toolkit, math, random, datetime and more
Python comes with a huge standard library, thousands of ready-made tools you can use by importing them. You don't need to write everything from scratch.
import and from...importpython
# Import the whole module
import math
print(math.sqrt(16)) # 4.0
print(math.pi) # 3.141592653589793
# Import just what you need
from math import sqrt, floor
print(sqrt(25)) # 5.0
print(floor(3.9)) # 3Use
Use
import module when you'll use many things from it (keeps code clear about where things come from).Use
from module import name when you only need one or two things and want shorter code.The math module, useful functions
math module highlightspython
import math
print(math.sqrt(144)) # 12.0, square root
print(math.ceil(4.1)) # 5, round up
print(math.floor(4.9)) # 4, round down
print(math.abs(-7)) # AttributeError! Use abs() instead
print(abs(-7)) # 7, abs is a built-in, not in math
print(math.pow(2, 10)) # 1024.0
print(math.log(1000, 10)) # 3.0, log base 10The random module, generating random data
random modulepython
import random
print(random.randint(1, 6)) # random int 1โ6 (dice roll!)
print(random.random()) # float between 0.0 and 1.0
print(random.choice(["a","b","c"])) # random pick from list
items = [1, 2, 3, 4, 5]
random.shuffle(items) # shuffles the list in place
print(items)๐คQuick Check
What does import random; random.randint(1, 10) return?
The datetime module, working with dates and times
datetime modulepython
from datetime import datetime, date
today = date.today()
print(today) # 2026-03-09
print(today.year) # 2026
print(today.strftime("%d %B %Y")) # 09 March 2026
now = datetime.now()
print(now.strftime("%H:%M")) # 14:30 (current time)Alias with `as`, if a module name is long or clashes with your code, rename it on import:
import datetime as dt then use dt.date.today()๐คQuick Check
Which imports only the floor function from the math module?
๐In the Real World...
boto3 for AWS, azure-mgmt for Azure, kubernetes client for K8s, paramiko for SSH, requests for HTTP APIs, every cloud SDK is a Python module you import. Learning modules unlocks the entire ecosystem.
๐ฏ
Phase Complete!
Real World Python
You've worked through error handling, modules, and real-world Python tools. Name 3 Python modules you'd use in a DevOps script and what you'd use each one for.
0/500
Practice Exercises
0/3 solvedExercise 1 of 3easy
โฑ 00:00Hypotenuse Calculator
Use
Formula: c = โ(aยฒ + bยฒ)
Given: a = 3, b = 4Expected output:
math.sqrt to calculate the hypotenuse of a right triangle.Formula: c = โ(aยฒ + bยฒ)
Given: a = 3, b = 4Expected output:
Hypotenuse: 5.0solution.py
1 / 3
Exercise 2 of 3easy
โฑ 00:00Dice Simulator
Simulate rolling two dice and print the result.
Use
Expected output format:
(The actual numbers will vary, just make sure the format is correct and total = die1 + die2)
Use
random.randint(1, 6) for each die.Expected output format:
Die 1: X, Die 2: Y, Total: Z(The actual numbers will vary, just make sure the format is correct and total = die1 + die2)
solution.py
2 / 3
Exercise 3 of 3medium
โฑ 00:00Days Until New Year
Calculate how many days remain until January 1st of next year.
Use
Print:
Use
date.today() and date(year, month, day) from the datetime module.Print:
Days until New Year: Xsolution.py
3 / 3
Solve all 3 exercises to unlock completion