# Working with librariesÂ¶

##3 Most of the power of a programming language is in its libraries.

A

*library*is a collection of files (called*modules*) that contains functions for use by other programs.May also contain data values (e.g., numerical constants) and other things.

Libraryâ€™s contents are supposed to be related, but thereâ€™s no way to enforce that.

The Python [standard library][stdlib] is an extensive suite of modules that comes with Python itself.

Many additional libraries are available from [PyPI][pypi] (the Python Package Index).

We will see later how to write new libraries.

## Most of the power of a programming language is in its libraries.Â¶

A

*library*is a collection of files (called*modules*) that contains functions for use by other programs.May also contain data values (e.g., numerical constants) and other things.

Libraryâ€™s contents are supposed to be related, but thereâ€™s no way to enforce that.

The Python [standard library][stdlib] is an extensive suite of modules that comes with Python itself.

Many additional libraries are available from [PyPI][pypi] (the Python Package Index).

We will see later how to write new libraries.

Note

```
import math
print('pi is', math.pi)
print('cos(pi) is', math.cos(math.pi))
```

```
pi is 3.141592653589793
cos(pi) is -1.0
```

Have to refer to each item with the moduleâ€™s name.

`math.cos(pi)`

wonâ€™t work: the reference to`pi`

doesnâ€™t somehow â€śinheritâ€ť the functionâ€™s reference to`math`

.

## Use `help`

to learn about the contents of a library module.Â¶

Works just like help for a function.

```
help(math)
```

## Import specific items from a library module to shorten programs.Â¶

Use

`from ... import ...`

to load only specific items from a library module.Then refer to them directly without library name as prefix.

```
from math import cos, pi
print('cos(pi) is', cos(pi))
```

```
cos(pi) is -1.0
```

## Create an alias for a library module when importing it to shorten programs.Â¶

Use

`import ... as ...`

to give a library a short*alias*while importing it.Then refer to items in the library using that shortened name.

```
import math as m
print('cos(pi) is', m.cos(m.pi))
```

```
cos(pi) is -1.0
```

Commonly used for libraries that are frequently used or have long names.

E.g., the

`matplotlib`

plotting library is often aliased as`mpl`

.

But can make programs harder to understand, since readers must learn your programâ€™s aliases.

Exercise: Exploring the Math Module

What function from the

`math`

module can you use to calculate a square root*without*using`sqrt`

?Since the library contains this function, why does

`sqrt`

exist?

See Solution

Using

`help(math)`

we see that weâ€™ve got`pow(x,y)`

in addition to`sqrt(x)`

, so we could use`pow(x, 0.5)`

to find a square root.The

`sqrt(x)`

function is arguably more readable than`pow(x, 0.5)`

when implementing equations. Readability is a cornerstone of good programming, so it makes sense to provide a special function for this specific common case. Also, the design of Pythonâ€™s`math`

library has its origin in the C standard, which includes both`sqrt(x)`

and`pow(x,y)`

, so a little bit of the history of programming is showing in Pythonâ€™s function names.

Exercise: Locating the right Module

You want to select a random character from a string:

```
bases = 'ACTTGCTTGAC'
```

Which [standard library][stdlib] module could help you?

Which function would you select from that module? Are there alternatives?

Try to write a program that uses the function.

See Solution

The [random module][randommod] seems like it could help you.

The string has 11 characters, each having a positional index from 0 to 10.
You could use `random.randrange`

function (or the alias `random.randint`

if you find that easier to remember) to get a random integer between 0 and 10, and then pick out the character at that position:

```
from random import randrange
random_index = randrange(len(bases))
print(bases[random_index])
```

```
from random import randrange
print(bases[randrange(len(bases))])
```

Perhaps you found the `random.sample`

function? It allows for slightly less typing:

```
from random import sample
print(sample(bases, 1)[0])
```

Note that this function returns a list of values. Thereâ€™s also other functions you could use, but with more convoluted code as a result.