Assume that you were working in a session and implemented a handful of functions. When you close that session, all those functions will be lost. For the next session, you would have to tediously rewrite and import each function.
A better approach to solving this problem is to import a python script that contains all the relevant functions. Such a script is known as a module, which can be imported with a single command.
Additionally, you could download a complete set of functions that are pre-written from the web. Such extensibility is what makes Python such an attractive language for data scientists and application programmers, who are able to use these packages to ease their workflow.
To create your own module, create a plain text file with the .py extension.
# module hi.py def HelloWorld(): print("Hello World!")
Now if we are in the same folder as hi.py, and we initialize Python, we may
import our module.
>>> hi.HelloWorld() Hello World!
Note that the function names are stored within the local symbol table, so you have to precede each function call with
hi. If you'd like to access the functions directly, you may do so:
>>> from sample import HelloWorld >>> HelloWorld() Hello World!
Importing all modules is sometimes considered bad practice, as you may be overwriting names that already exist. However, if you are sure no important names are overwritten, and want to make your code looking cleaner, you can do so using
from sample import *. This effectively imports all functions from the sample.py module.
In some cases you'll want to import just one function instead of the entire module. We may do so with the following syntax.
>>> from os import getcwd >>> getcwd() '/Users/JohnDoePC/Dropbox'
You may also want to import using a different name (alias). To do so, use the
>>> from os import getcwd as pwd >>> pwd() '/Users/JohnDoePC/Dropbox'
Two of the most commonly used modules are the OS and Random modules. Let's take a quick look at them and how they can be used.
os module provides an interface to the current operating system.
>>> import os >>> os.getcwd() '/Users/JohnDoePC/Dropbox' >>> os.getlogin() JohnDoePC
Another important module is one that allows you to generate random numbers. This is known as the
>>> import random >>> random.randint(2,4) # Generates a random number between 2 and 4 (inclusive) 2
Stretch out your back and relieve your back muscles with inversion therapy. This device counteracts the forces of gravity on the body by decompressing and elongating the spine. By using this product just ten minutes a day, you can be well on your way to improved circulation and posture while relieving muscle aches, back pain and stress.$$ Check price
This book doesn't make any assumptions about your background or knowledge of Python or computer programming. You will be guided step by step using a logical and systematic approach. As new concepts, commands, or jargon are encountered they are explained in plain language, making it easy for anyone to understand.$ Check price