4 months ago

Use import in Python to access other programmers' code modules in your own programs

Import statements in Python enable you to access code created by other programmers.

As you learn to code in Python, you quickly realise you don’t have to program everything yourself.

We live in the modern world, so you’re not supposed to do all the work on your own. Instead, you will often stand on the shoulders of other programmers who have done the groundwork for you.

This article is based on Beginner’s Guide to Coding in issue 53 of The MagPi. Issue 54 of The MagPi has a companion piece on learning object orientated programming with a Raspberry Pi

- Click here to read Beginner’s Guide to Coding in issue 53
- Click here to read Object Orientated Programming in issue 54

See also:

- Programming a Raspberry Pi with Python
- Variables: learn Programming in Python
- Terminal and Python IDLE
- Loops: Using While and For in Python
- Branching: using If and Else in Python

Your programs can import code created by other people using the import statement. This enables you to import modules and use their functions – only they’re now known as ‘methods’.

You import the module at the command line, and then access the functions using dot notation. This is where you list the module, followed by a dot (.), then the method.

A common module to use is math. This allows you to access lots of maths methods. Open a Python Shell and enter:

1 |
import math |

You now have access to all the methods in math. You won’t notice any difference, but if you type:

1 |
type(math) |

…it will say ‘<class ‘module’>’. Let’s try out dot notation now. Type math followed by a dot and the name of the method (function) you want to use:

1 |
math.sqrt(16) |

This gives the square root of 16, which is 4.

Some methods have more than one argument. The math.pow() method raises a number to an exponent:

1 |
math.pow(64,3) |

This returns 262144.0.

You can also access constant values from a module, which are fixed variables contained in the module. These are like functions/methods, but without the parentheses.

1 |
math.pi |

This returns pi to 15 decimal spaces: 3.141592653589793.

1 |
math.e |

This returns Euler’s number to 15 decimal spaces: 2.718281828459045.

It’s also possible to import methods and constants from modules using from. This enables you to use them inside your programs without dot notation (like regular functions). For example:

1 2 3 |
from math import pi from math import e from math import pow |

Now, whenever you type pi or e, you’ll get pi and Euler’s number. You can also use pow() just like a regular function. You can change the name of the function as you import it with as:

1 |
from math import pi as p |

When you enter p you’ll get pi to 15 decimal spaces. Don’t go crazy renaming functions with as, but it’s common to see some methods and constants imported as single letters.

By creating your own functions, and importing those created by other people in modules, you can vastly improve the capabilities of your programs.

We’ve taken everything covered in our recent coding tutorials and used it to create a game of Pong; this is one of the world’s first videogames.

Download issue 53 of The MagPi and carefully type out the code on page 27.

In the Pong program you’ll find variables, functions, loops, and conditional branching: all the stuff we’ve talked about. Hopefully, you’ll now be able to decipher most of this code.

If you’re interested in taking Pong further, this program is similar to a version of a Pygame Pong program by Trever Appleton.

His version has a scorecard and more advanced code. We’ve kept ours simple so it’s easier to start with.

Hopefully this isn’t the end of your Python, or programming, journey. There are lots of places you can learn programming from. And we’ll have more programming resources for you in every issue of The MagPi.

If you want to learn more about Pygame, check out Make Games With Python, our free Essentials Guide to Pygame.