2. # importing stuff from other places
# importing a function for random numbers
from random import randint
print "A random number:", randint(1,10)
# importing the "time" module
import time
for i in range(5):
print "I’m taking a rest!"
sleep(1)
importing
3. importing
---- mycode.py ----
# define a function
def sum_of(number):
""" Returns the sum of all numbers between 0 and number.
"""
count = 0
for i in range(number + 1):
count = count + 1
return count
# define another function
def say_hello(name):
""" Writes a friendly "hello" message to a person.
"""
print "Hello there", name
# define some parameters
parameter = 120
parameter_list = [120, 130, 150]
4. ---- myprogram.py ----
# importing the entire "module" (must be in the same directory)
import mycode
print mycode.parameter
mycode.say_hello("Marc")
# OR, import only certain parts
from mycode import parameter_two, sum_of
print parameter_list
print sum_of(300)
# OR, import using an alias
import mycode as mc
print mc.parameter
print mc.sum_of(1000)
importing
importing a module:
import module
usage:
module.some_function(args…)
importing from modules:
from something import some_function
usage:
some_function(args…)
importing a module:
import module as mod
usage:
mod.some_function(args…)
5. A typical file…
---- anyfile.py ----
from xxx import yyy
import zzz
def somefuntion(a,b,c)...
…
def anotherfunction (a, b, c):
…
the rest of your code