CS101 - Notes on modules

Topics

Python includes some built-in functions such as len(), type(), int(), float(), dir(), help(), print(), input(). Here is the complete list of built-in functions. The real power of modern languages is the libraries of existing functionality that allow us to do complex things very quickly. Much of this functionality is bundled with "objects".

Object specific functions

As it turns out, the types that we have looked at are somewhat more complex than they first appear – they carry some functionality around with them. We will talk more about this later, but the basic idea is that in most cases, functionality only makes sense for particular kinds of data. In an “object-oriented” language (which Python is), this functionality can be bundled together with the value for which it makes the most sense.

For example, consider a function that takes a string as an argument and returns a copy of the string with all of the letters converted to uppercase. Clearly, this only makes sense for strings. So, rather than having a function that only works properly when we pass it a string, we give the functionality to strings themselves. To access these functions that are built into values, we use “dot-notation”. We write the value (or, more typically, a variable containing the value), followed by a period, and then by the function name (with no spaces). When the function is called, it is as if the value to the left of the dot is passed in as the first argument.

Try it

Strings contain a function called upper() which returns a new uppercase version of the string.

Create a string variable:
>>> str1 = 'Run for your life!'

Call upper
>>> str1.upper()

As with our other expressions, the result is printed out on the console, and then is no longer available. To keep the result, assign it to another variable: str2 = str1.upper()

Strings have many functions defined on them. There are a couple of ways to learn what they are.

  1. You can use dir(s) (where s is a string variable) in the console to get a list of all available functions (ignore any that start with __).

  2. You can consult the official documentation.

To learn what a function does and how to use it, you can similarly read the documentation in one of a couple ways.

  1. You can use the builtin help() function. For example, type help(str1.upper). Note that we do not include the () after upper. If we do, the help method will give us help on the value returned by the function, rather than on the function itself.

  2. You can read the description in the online documentation listed above.

Spend a few minutes and try out a couple of string methods.

Modules

Some functionality doesn’t necessarily make sense to include on all values of a particular type. Other functionality is specialized and doesn’t need to be included in every Python program. The way we handle this in Python is through the use of “modules”. These are packages containing functions and frequently, new types.

To use these modules, they need to be “imported”. This tells the Python interpreter that you wish to use the module in your code. You can put these import statements at the top of a Python script (and for experimenting we can import them in the console directly).

You will find that working with modules is very much like working with objects. We can use dir() to figure out what functionality comes with the module and help() to learn more about it. We access the contents of a module by again using the dot notation, prefacing items from the module with module_name.*item_name. Google will also help you find documentation online for most of the modules that we use (make sure you are looking at the Python 3.6 documentation and not the 2.7 documentation).

There are a number of ways to import libraries:

>>> import my_module
>>> import my_long_module_name as m
>>> from my_module import a, b, c
>>> from my_module import *

Try it

Our first module will be the math module, which includes some basic mathematical functions not covered by our basic operators.

Import the module
>>> import math

Take a look at what is included
>>> dir(math)

Try a function
>>> math.sqrt(9)

Notice that there are some constant values included in the math module like math.pi and math.e, and we do not use parenthese after them. These can be used like variables, though we shouldn’t change them (really – don’t change them).

Get some practice

Here are a couple things to investigate or try; they are just designed to get you looking through the documentation and using these functions.

  1. Do the trigonometric functions (sine, cosine, tangent, etc) in the math library use degrees measurements or radians?
  2. What is the log of 100?
  3. The circumference of a circle is 2 times the radius of the circle times PI. What is the circumference of a circle with radius 1?
  4. Create a variable called s and assign it the string 'the imitation game'. What single statement uses s to produce 'The Imitation Game'?
  5. Write a statement that returns the index of the first space in s.
  6. Write a statement that returns the index of the last space in s.
  7. Write one line that changes our string from 'the imitation game' to 'the imitation movie'. Use string functions, not slices.
  8. Repeat the above substitution, this time using slices rather than a function.
  9. Write an expression that returns the number of spaces in our string variable s.
  10. Write a line that transforms s into 'IMITATION'.