Module import Syntax Python Tutorial



Now that we've used a module, statistics, it would be a good time to explain some import syntax practices. As with many things in programming, there are many ways to import modules, but there are certainly some best practices.

So first, when you import a module, you are basically loading that module into memory. Think of a module like a script. Many if not most modules are just a single python script. So, when you go to import it, you use the file name. This can help keep code clean and easy to read. Many python developers just program everything in 1 script. Other developers, say from a language like java are going to be very used to doing lots of imports with a file for each type of job that's happening. Just like there are many ways to import, there are many more ways to program.

So let's talk about basic importing:

import statistics

Above, we have referenced the statistics module and loaded it into memory under the statistics object. This will allow us to reference any of the functions within the statistics module. To do so, we will need to mention statistics, followed by a period, then the function name. A simple exhibition of the mean function from statistics could look like this:

import statistics

example_list = [5,2,5,6,1,2,6,7,2,6,3,5,5]

print(statistics.mean(example_list))

The generated output from this will be the mean, or average, of the list.

That is the simplest way to import and use modules, but there are many other methods. In the video, we cover each one specifically, but here are a bunch of examples:

Sometimes, however, you will see people use the "as" statement in their imports. This will allow you to basically rename the module to whatever you want. People generally do this to shorten the name of the module. Matplotlib.pyplot is often imported as plt and numpy is often imported as np, for example.

import statistics as s

print(s.mean(example_list))

Above, we've imported statistics as the letter 's.' This means whenever we wish to reference the statistics module, we just need to type 's' instead of statistics.

What if you don't even want to type that S though? Well there's an app for that!

You can just import each function within the module you plan to use:

from statistics import mean
# so here, we've imported the mean function only.
print(mean(example_list))

# and again we can do as
from statistics import mean as m
print(m(example_list))

Above, you can see that we no longer had to type any reference to the statistics module, then you saw that we could even import the functions "as" something else.

What about more functions?

from statistics import mean, median
# here we imported 2 functions.
print(median(example_list))

What if we want to use the as as well?

from statistics import mean as m, median as d

print(m(example_list))
print(d(example_list))

What if we want to just import everything from statistics like we did initially, but we don't want to type the statistics because we have fat fingers and this will just slow us down?.

from statistics import *

print(mean(example_list))

There exists 3 quiz/question(s) for this tutorial. for access to these, video downloads, and no ads.

The next tutorial:





  • Python Introduction
  • Print Function and Strings
  • Math with Python
  • Variables Python Tutorial
  • While Loop Python Tutorial
  • For Loop Python Tutorial
  • If Statement Python Tutorial
  • If Else Python Tutorial
  • If Elif Else Python Tutorial
  • Functions Python Tutorial
  • Function Parameters Python Tutorial
  • Function Parameter Defaults Python Tutorial
  • Global and Local Variables Python Tutorial
  • Installing Modules Python Tutorial
  • How to download and install Python Packages and Modules with Pip
  • Common Errors Python Tutorial
  • Writing to a File Python Tutorial
  • Appending Files Python Tutorial
  • Reading from Files Python Tutorial
  • Classes Python Tutorial
  • Frequently asked Questions Python Tutorial
  • Getting User Input Python Tutorial
  • Statistics Module Python Tutorial
  • Module import Syntax Python Tutorial
  • Making your own Modules Python Tutorial
  • Python Lists vs Tuples
  • List Manipulation Python Tutorial
  • Multi-dimensional lists Python Tutorial
  • Reading CSV files in Python
  • Try and Except Error handling Python Tutorial
  • Multi-Line printing Python Tutorial
  • Python dictionaries
  • Built in functions Python Tutorial
  • OS Module Python Tutorial
  • SYS module Python Tutorial
  • Python urllib tutorial for Accessing the Internet
  • Regular Expressions with re Python Tutorial
  • How to Parse a Website with regex and urllib Python Tutorial
  • Tkinter intro
  • Tkinter buttons
  • Tkinter event handling
  • Tkinter menu bar
  • Tkinter images, text, and conclusion
  • Threading module
  • CX_Freeze Python Tutorial
  • The Subprocess Module Python Tutorial
  • Matplotlib Crash Course Python Tutorial
  • Python ftplib Tutorial
  • Sockets with Python Intro
  • Simple Port Scanner with Sockets
  • Threaded Port Scanner
  • Binding and Listening with Sockets
  • Client Server System with Sockets
  • Python 2to3 for Converting Python 2 scripts to Python 3
  • Python Pickle Module for saving Objects by serialization
  • Eval Module with Python Tutorial
  • Exec with Python Tutorial