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.
Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.
It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.
In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.
It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.
By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.
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
