Python: Packages
Python programs almost always use code written by others. Reinventing mathematical functions, date handling, or network requests makes no sense — all of this is already available. Python uses packages to share and reuse code.
A package is a directory of Python files organized by topic. The Python standard library contains dozens of such packages, covering everything from mathematics to filesystem access.
Importing a Package
To use code from a package, import it with the import keyword. Let's look at the math package:
import math
print(math.sqrt(16)) # => 4.0
print(math.pi) # => 3.141592653589793After import math, everything in the package is available through dot notation: math.sqrt, math.floor, math.pi.
Importing Specific Names
An alternative syntax lets you import only what you need:
from math import sqrt, pi
print(sqrt(25)) # => 5.0
print(pi) # => 3.141592653589793Now sqrt and pi are available directly, without the math. prefix. The import math form makes the origin of the function explicit — that helps when reading unfamiliar code. The from math import form is convenient when you need a few specific functions and writing the prefix every time is tedious.
Aliases
The as keyword lets you give an imported name an alias:
from math import sqrt as sq
print(sq(9)) # => 3.0An alias is useful when a name conflicts with an existing variable or is simply too long.
Standard Library
Python ships with a rich set of built-in packages. A few commonly used ones:
math— mathematical functions:sqrt,hypot,floor,ceil,pirandom— random numbers:randint,choice,shuffledatetime— working with dates and times
import random
print(random.randint(1, 6)) # => random number from 1 to 6
print(random.choice(['rock', 'paper', 'scissors'])) # => random elementThird-Party Packages
Beyond the standard library, thousands of packages are created by developers around the world. Install them with pip, Python's package manager:
pip install requestsAfter installation, imports work the same way as for built-in packages.
Instructions
Implement the function generate_pin(). It returns a random four-digit PIN code as a string.
Each digit of the PIN is an independent random integer from 0 to 9.
print(generate_pin()) # => e.g. "4823" or "0571"
print(generate_pin()) # => e.g. "1942" or "0037"Add a return type annotation.
Hint
Generate each of the four digits with random.randint(0, 9) and combine them into a string using an f-string.
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Your exercise will be checked with these tests:
import random
import solution
def test():
annotations = solution.generate_pin.__annotations__
assert 'return' in annotations, "You should add a return type annotation"
for seed in [1, 7, 42]:
random.seed(seed)
result = solution.generate_pin()
random.seed(seed)
d1 = random.randint(0, 9)
d2 = random.randint(0, 9)
d3 = random.randint(0, 9)
d4 = random.randint(0, 9)
assert result == f"{d1}{d2}{d3}{d4}"
assert len(result) == 4Teacher's solution will be available in:
20:00
