A module is a toolbox of code that you can use in your own program. Someone has already written, tested and improved a function for the square root, for rolling a die, for the median of a list and for the number of days between two dates. In this course you import the math, random, statistics and datetime modules, use their tools to solve small everyday problems, and learn the ways of importing. A lesson on finding and choosing tools shows how to look inside a module with dir and help, how to read an import error, and which mistakes make an import fail. A dice laboratory is the project at the end: you roll a die hundreds of times and see how fair it is. The practice runs your code and tells you which results are wrong.
5 lessons, about 2 h 25 min5 simulations14 problems10 practice questionsPython in every lesson
A module is a toolbox of ready-made code. import math brings the toolbox in, and math.sqrt uses a tool with a dot. The math module has sqrt, pi, ceil, floor, factorial, gcd, hypot, trigonometric functions that work in radians, and isclose for comparing decimal numbers. from math import sqrt brings in one tool and import math as m gives the toolbox a shorter name. A tool may refuse a value with a ValueError. A module name that does not exist gives a ModuleNotFoundError, and a tool name that does not exist gives an AttributeError.
The random module adds chance. randint gives a whole number with both ends included, choice picks one item of a list, shuffle mixes a list itself, random gives a decimal number from 0 up to but not including 1, uniform gives a decimal number between two limits, and sample picks several different items. A seed makes the sequence repeat, which is useful when you test a program. Without a seed, each run is different. The numbers are only pseudo-random, which is enough for games and tests, but not for secrets.
The statistics module describes a list of numbers. The mean is the sum divided by the count, the median is the middle value of the sorted data, or the mean of the two middle values when the count is even, and the mode is the most common value, while multimode gives all the values that tie. pstdev shows how spread out the data is. The datetime module makes a date with date of year, month and day. Subtracting two dates gives a gap, and days is the number of days. strftime writes a date as text, such as the day name, and a timedelta adds days to a date.
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.
In this project we build a dice laboratory. The program rolls a die 600 times with a seed so that the result is the same each run, counts each face with a dictionary, compares each count with the expected 100, finds the mean, the mode and the biggest gap from the expected count, and draws a bar chart in text. We plan what a fair die should show, build the program in steps with the random and statistics modules, test it with different seeds and different numbers of rolls, and extend it with two dice and the sum of two dice.
Log in to join the discussion.
Loading…