By the end of this lesson, you will:
random and time packagesℹ️ What is a Package? A package is a folder that contains modules (files) and other folders with Python code for specific tasks. Think of it like a toolbox where each tool (function) is organized in compartments (modules)!
You've learned that functions help us perform specific tasks. But imagine writing every single function from scratch - that would take forever!
That's why programmers use packages - collections of pre-written code that we can use in our programs. Python has many ready-to-use packages!
Here's how packages are organized:
| Component | Description | Type |
|---|---|---|
| Function | A piece of code that performs one task | Code |
| Module | A file containing related functions | File |
| Package | A folder containing related modules | Folder |
📝 Remember the Flow! Function -> Module -> Package
Small pieces of code combine to form files, and files combine to form folders!
Before using any function from a package, you must import it first. There are three main ways to import:
importimport ... asfrom ... importLet's learn each method using the randint() function from the random package!
Use the import keyword followed by the package name. Then call functions using package.function():
import random
random.randint(1,10)
Expected output:
4
Sometimes package names are long. You can give them a shorter nickname using as:
import random as rd
rd.randint(1,10)
Expected output:
1
If you only need one function, import just that function. Then you can use it directly without the package name:
from random import randint
randint(1,10)
Expected output:
9
💡 Which Import Method Should I Use?
- Use
import packagewhen you need many functions from the package- Use
import package as nicknamefor packages with long names- Use
from package import functionwhen you only need one or two functions
Python has many built-in packages. Let's explore two very useful ones: random and time.
The random package helps us work with random numbers and choices:
random() - Generate Random DecimalsReturns a random decimal number between 0 and 1:
random.random()
Expected output:
0.8722454090457804
randint() - Generate Random IntegersReturns a random whole number within your specified range:
random.randint(1,20)
Expected output:
19
shuffle() - Mix Up a ListTakes a list and randomly rearranges its items:
numbers = list(range(10))
random.shuffle(numbers)
print(numbers)
Expected output:
[4, 0, 2, 9, 8, 5, 7, 1, 3, 6]
📝 Learn More! To discover more functions in the
randompackage, visit: https://www.w3schools.com/python/module_random.asp
The time package helps us work with time and dates:
localtime() - Get Current TimeReturns the current time with detailed information:
time.localtime()
Expected output:
time.struct_time(tm_year=2020, tm_mon=11, tm_mday=18, tm_hour=7, tm_min=7, tm_sec=11, tm_wday=2, tm_yday=323, tm_isdst=0)
sleep() - Pause Your ProgramMakes your program wait for a specified number of seconds:
for i in range(5): for i in range(5):
print(i+1) print(i+1)
time.sleep(1) time.sleep(1)
Expected output:
1 1
2 2
3 3
4 4
5 5
📝 Learn More! To discover more functions in the
timepackage, visit: https://www.programiz.com/python-programming/time
In this lesson, you learned:
import, import...as, and from...importrandom package helps with random numberstime package helps with time-related tasksCode with AI: Try using AI to work with Python packages!
Prompts to Try:
Try these challenges to test your understanding:
random package and create a number guessing gametime package to create a simple countdown timermath or datetime) and explore three of its functions