So far we have learned about Python’s built-in functions and various data type methods. But these form a small fraction of all the functions that the Python programming language comes with. Python’s other functions (and even other data types) are separated into various modules, which is another name we give to Python code files. Unlike the functions and data types we’ve seen so far, these modules are not automatically loaded when run the Python interpreter, as they contain more specialized functions and data types. So in this section, we’re going to learn how to load one of these modules and use their definitions.
import statementTo load a Python module, we use a piece of code called an import statement, which has the following syntax:
For example, here is how we could load the math module in the Python console:
Like the other statements we’ve seen so far, import statements do not produce a value, but they do have an important effect. An import statement introduces a new variable (the name of the module being imported) that can be used to refer to all definitions from that module.
For example, the math module defines a function log2 which computes the base-2 logarithm of a number. To access this function, we use dot notation: This notation is the same as accessing data type methods, but log2 is not a method. It’s a top-level function, just one that happens to be defined in the math module.
What other functions are contained in the math module? We’ll make use of a few other later in this course, but if you’re curious you can call the special built-in function dir on the the module (or any other module) to see a list of functions and other variables defined in the module:
>>> dir(math)
['__doc__', '__loader__', '__name__', '__package__', '__spec__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'comb', 'copysign', 'cos', 'cosh', 'degrees', 'dist', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'gcd', 'hypot', 'inf', 'isclose', 'isfinite', 'isinf', 'isnan', 'isqrt', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'log2', 'modf', 'nan', 'perm', 'pi', 'pow', 'prod', 'radians', 'remainder', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'tau', 'trunc']Ignoring the first few with the double underscore, we see some familiar looking names, like ceil, floor, pi, and sin. We’ve linked to the documentation for the math module in the References section below.
datetime modulePython comes with far more modules than we’ll have time to learn about in this course. However, just to illustrate the breadth of these modules, we’ll briefly introduce one more that will be useful occasionally throughout the course.
The datetime module provides not just functions but new data types for representing time-based data. The first data type we’ll study here is date, which is a data type that represents a specific date.
>>> import datetime
>>> canada_day = datetime.date(1867, 7, 1) # Create a new date
>>> type(canada_day)
<class 'datetime.date'>
>>> term_start = datetime.date(2020, 9, 10)
>>> datetime.date.weekday(term_start) # Return the day of the week of the date
3 # 0 = Monday, 1 = Tuesday, etc.Note the double use of dot notation in that last expression. datetime.date is the data type being accessed, and .weekday accesses a method of that data type.
We can compare dates for equality using == and chronological order (e.g., < for comparing one date comes before another). We can also subtract dates, which is pretty cool:
The difference between two dates is an instance of the datetime.timedelta data type, which is used to represent an interval of time. What the above expression tells us is that 55,954 days have passed between the first day of the fall semester and the day of Canada’s confederation. Fun fact: Canada’s confederation first consisted of only four provinces: Ontario, Quebec, Nova Scotia, and New Brunswick.
Up to this point, we’ve covered several different data types, functions, methods, and now modules in Python. It might be starting to feel a bit daunting, and we wanted to take a moment to pause and look at the bigger picture. Our goal in showing you these elements of Python is not to overwhelm you, but instead to give you a taste of the language’s powerful computational capabilities. But this course is not about memorizing different functions, data types, and modules in Python! All throughout this course, you’ll have access to references and documentation that describe the functionality of these different elements, and will have lots of opportunities to practice using them. For now, all we want you to know is simply that these capabilities exist, how to experiment with them in the Python console, and how to look up information about them.