Home CPSC 110

Using Libraries

We have seen several Python built-in functions, like print, round, and len. We can use any of these directly in a Python program. However, there are lots of things that come with Python that are not included by default.

Python comes with lots of other libraries, or modules that we can use in our programs. These libraries come with extra things we can use in our programs.

There are also lots of other libraries that are not included with Python, but which can be downloaded from the Internet to use. Lastly, we can create our own libraries to use in our programs, or give to others.

Today, we will look at how libraries can be used, and look at a couple of the things included with Python.


The import Statement

In order to use a library, we must put an import statement in our program. To import Python's math library, we could do this:


import math

After that, we can use the things that are in the math library, like the sqrt function. We can do so like this:


import math

num = math.sqrt(100)
print(num)

Here we start with the import statement, and then later use sqrt. The import of math has to be before the use of anything from math. Generally import lines are at the top of a program, so we can see what the program is using.

Also note that we can't just call sqrt directly, we have to put the name of the library first, followed by a '.', followed by the name of the function we're using.

The reason for this is to keep things organized. The math library has over 50 things in it, and if we have lots of libraries, it could be hard to know what things are coming from where.

However, if we will be using sqrt a lot, and don't want to write out the whole thing each time, we could do so this way:


from math import sqrt

num = sqrt(100)
print(num)

Now, we can use sqrt inside of our programs without needing to say it comes from the math library. However, now we can't use anything from the math library except for sqrt. If we want to be able to use multiple things this way, we could do so like this:


from math import sqrt, sin

num1 = sqrt(100)
num2 = sin(3.0)

If we want to be able to use everything, we can import it this way:


from math import *

num1 = sqrt(100)
num2 = sin(3.0)


The math Library

We have seen that the math library contains a function for square roots and calculating sines. There are many other mathematical things in it. All Python libraries have a web page describing how they are used. The one for math is here.

There are functions, like sqrt that perform some kind of calculation for us, and there are also "constants" which are actual numbers, such as Pi and e.


Example: Password Entropy

Password entropy is a measure of how difficult a password would be to guess. This is based on how many available characters might be in the password, and the length of the password. The more possible characters might be in the password, the harder it is to guess, and the longer the password is, the harder it is to guess. It is calculated as:

$E = log_2(C^L)$

Where:
$C =$ the number of possible unique characters available
$L =$ the length of the password
$E =$ the password entropy

Password strength also takes into account things like not using dictionary words, but entropy doesn't consider that.

How could we write a program to ask the user for these values and then compute the password entropy?

What is the entropy of a 4-digit pin number? How about a 6 character computer password vs. a 10 character one?







The random Library

Another very useful library that is included with Python is the random library, for getting random numbers. You can read all about it on its web page.

Probably the most useful thing in it is randint which we can use to get random integers between two values. For example, the following program get the starting point and ending point from the user, and then prints a random number between those two (including both ends):


import random

a = int(input("Starting point: "))
b = int(input("Ending point: "))

num = random.randint(a, b)
print("Your random number is", num)

In actual fact, computers cannot really give us truly random numbers. The values that randint provides actually are produced from mathematical sequences. They seem random, but are in fact not.


Example: Guess the Number

We have done guess the number programs where we think of a number and the computer guesses it. Now that we have random numbers, could we also write a program where the computer picks a number and we try to guess it?







Using 3rd Party Libraries

When we give Python the command to import the math library, it works because math comes with Python and the interpreter knows where to find it. However we can also use libraries that are developed by others and don't come with Python.

This can be done lots of different ways. Python comes with a tool for installing packages and updating them, and managing different versions. For our purposes, we will simply do the following:

  1. Download the library from some place. For the graphics library we will use (described below), the library can be found at this address. It is just a regular .py file. You could read the code if you want, but it uses many things we haven't gotten to, and is a bit cryptic.
  2. Save the graphics.py file in the same location where you will create a program. In order to be able to import the library, Python needs to be able to find the file. There are several ways to do this, but the simplest is just to put it in the same folder where your program will go.
  3. Import the library using the name of the file (without the .py). So for the graphics library, we should be able to use "import graphics". If this works, nothing should happen on the import line. If Python can't find the library with that name you will get an error message.

Creating Our Own Libraries

Because Python libraries are just regular .py files, we can create our own if we want to. For example, if you create a Python file called "mylibrary.py" with the following in it:


mesg = "Hello"
number = 42

Then we can open another file, in the same directory, and import it, like this:


import mylibrary

# should print "Hello"
print(mylibrary.mesg)

# should print 42
print(mylibrary.number)

This is not terribly useful to us yet since we haven't seen how to make our own commands (called functions) yet.


The Graphics Library

Next week we will see how to use the graphics library to make graphical programs in Python. Here is a very simple graphics program:


from graphics import *

# open up a graphics window
win = GraphWin("Test Program", 400, 400)

# create a circle at (200, 200) with radius 100
c = Circle(Point(200,200), 100)

# make the circle red
c.setFill("red")

# draw the circle onto the window
c.draw(win)

# wait for the user to click the window
win.getMouse()

# close the window
win.close()

Notice that we use the "from" style of import so we don't have to type "graphics." before everything. We will discuss how the library works and what we can do with it next week. For now, you can run this program to see if you have the library installed and working or not. When you run the program it should open a window like this:

You can click in the window to close it.

Copyright © 2018 Ian Finlayson | Licensed under a Creative Commons Attribution 4.0 International License.