Course 2.2Lesson 4 of 5
To find a tool, look inside the module. dir of a module lists its names, and the ones that start with an underscore are private. The note of a tool is read with its __doc__, or with help, which shows how to call it and what it gives. Check a name with in dir before you rely on it. The standard library has many modules, such as math, random, statistics, datetime, os, json, time, string and collections, and the choice of a tool follows from the job. Do not name your own file after a module, because Python will import your file instead. Read the documentation for the arguments and the answer, and test a tool on a small case before you trust it.
Try the code and the simulations beside the lesson.
After this lesson you can
dir,help and its note,Nobody knows every tool in every module. Experienced programmers look things up all the time. The skill is to find the tool, and to read how it works. Python helps you from inside the program.
You can use the same two calls for any module and any tool, even one that you do not know yet. help(len) and help(str.split) work too. When you meet a new tool, read its help before you use it, and then test it on a small case where you know the answer.
Python comes with hundreds of modules. This is called the standard library. You have met four. Here are some of the others, so that you know that they exist.
| Module | What it is for |
|---|---|
math | numbers: roots, logs, trigonometry |
random | chance |
statistics | describing data |
datetime | dates and times |
time | the clock, waiting, timing a program |
string | ready-made sets of letters and digits |
collections | special lists and dictionaries, such as a counter |
json | saving and reading data as text |
os | files and folders on the computer |
And beyond the standard library, other people share modules that you can install, such as matplotlib for graphs and numpy for fast number work. You will meet them in the next books.
When you have a job, ask three questions.
Do not go to the other extreme: a short loop that you understand is better than an import that you do not. But for the square root, the median, or the days between two dates, use the tool. It is correct for all the edge cases that you did not think of, such as leap years.
ModuleNotFoundError. Check the spelling and the capital letters.AttributeError. Check with dir.random.py, then import random finds your file, and not the real one. Strange errors follow. Choose names such as dice_game.py.from module import *.** Names clash, and you do not know where a name came from.from math import pow and then your own pow replace each other.Writing this count yourself would take a careful loop. help(math.comb) shows that the tool is already there, with one line of explanation.
Look at this note: math.gcd(*integers) is described as the greatest common divisor of the given integers. It tells you the name, that it takes whole numbers (the star means "any number of them"), and what it gives. The answer is a whole number. With this note, you know that math.gcd(12, 18, 30) is allowed, and gives 6.
Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
string, run dir and help on two of its tools, and write one sentence about each.dir(module) lists the names. help(tool) or tool.__doc__ explains one.in dir(module) before you rely on it.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
math?help(math.gcd) do?random.py?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.