Loading topic
math, random, and datetime. This chapter explains how Python finds and loads a module, how the different import forms change the names in your code, and which mistakes to avoid. Later ch6 min readimport, and that you can run a script or use the interactive interpreter. It also assumes you know the difference between int and float. The math module is part of the standard library, so there is nothing to install.5 min readint and float, and have used import math before. It covers four ways to turn a number into a nearby whole number, or a number with fewer decimals. They give different answers for negative numbers, for ha5 min readmath module's constants and functions. It covers why float arithmetic gives results like 0.30000000000000004, how to compare floats with math.isclose, and how to add them accurately with math.fsum.5 min readint and float. You should also have read the earlier chapters on importing from the standard library and on floating-point behavior in math.4 min readrandom module generates pseudo-random numbers: values that look unpredictable but come from a deterministic algorithm. This chapter covers the four core functions for producing numbers: random(), randint(), randrange(), and uniform(). It also covers where each one is easy to misuse.4 min readrandom.random() and random.randint(). The functions here take a whole sequence and make a random decision about its items. The details that cause bugs are replacement, ordering, mutation, and4 min readrandom.randint, random.choice and random.shuffle, and that you can write functions with default arguments. It covers how to make random code repeatable, why separate random.Random objects are often better than the module-level functions, and the cases where 5 min readrandom module is built for speed and repeatability, not secrecy. This chapter explains why that makes it unsafe for passwords, session tokens, and similar values. It also shows how to use the secrets module instead.6 min readdate and datetime objects and know that timedelta represents a duration, as covered in the previous chapter. You should also be comfortable with functions, try/except, and sorting lists. Here you will learn which arithmetic operations are allowed on date4 min readdate, datetime, and timedelta objects and do arithmetic with them, as covered in the previous chapters. It also assumes you know the difference between naive and aware datetimes. Here you will turn those objects into text and text back into objects.5 min readdate, datetime and timedelta objects, do arithmetic with them, and format and parse them with strftime, strptime, isoformat and fromisoformat. It builds on those skills to handle moments in time that must mean the same thing on every machine.5 min readmath, random, and datetime in small programs. It assumes you know the individual pieces from earlier chapters: math.isclose and math.fsum, random.Random instances, timedelta arithmetic, and the difference between naive and aware datetimes.5 min read