The Python tutorials are written as Jupyter notebooks and run directly in Google Colab—a hosted notebook environment that requires no setup. Click the Run in Google Colab button.
Colab link - Open colab
Module** A module allows you to logically organize your Python code. Grouping related code into a module makes the code easier to understand and use. A module is a Python object with arbitrarily named attributes that you can bind and reference.
Simply, a module is a file consisting of Python code. A module can define functions, classes and variables. A module can also include runnable code.
Create a Module** To create a module just save the code you want in a file with the file extension **.py**
def greeting(name):
print("Hello, " + name)
Save this code in a file named **mymodule.py**
Use a Module** Now we can use the module we just created, by using the ** import ** statement:
import mymodule
mymodule.greeting("Joey")
You can use any Python source file as a module by executing an import statement in some other Python source file.
The import has the following syntax − **import module1[, module2[,... moduleN]**
When the interpreter encounters an import statement, it imports the module if the module is present in the search path. A search path is a list of directories that the interpreter searches before importing a module. For example, to import the module support.py, you need to put the following command at the top of the script
Variables in Module** The module can contain functions, as already described, but also variables of all types (arrays, dictionaries, objects etc)
person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}
Save the above code in the file **mymodule.py**
import mymodule
a = mymodule.person1["age"]
#Imports the module named mymodule,
#and access the person1 dictionary
print(a)
Import From Module- You can choose to import only parts from a module, by using the **from** keyword
The syntax is: **from modname import name1[, name2[, ... nameN]]**
This statement does not import the entire module fib into the current namespace; it just introduces the item fibonacci from the module fib into the global symbol table of the importing module.
The module named mymodule has one function and one dictionary
def greeting(name):
print("Hello, " + name)
person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}
from mymodule import person1
print (person1["age"])
# Imports only the person1 dictionary from the module
Re-naming a Module - You can create an alias when you import a module, by using the **as** keyword
import mymodule as mx
# Creates an alias for mymodule called mx
a = mx.person1["age"]
print(a)
Built-in Modules- There are several built-in modules in Python, which you can import whenever you like.
import platform
x = platform.system()
#Imports and use the platform module
print(x)
The built-in function dir() is used to find out which names a module defines. It returns a sorted list of strings
import platform
x = dir(platform)
# Lists all the defined names belonging to the platform module
print(x)
import fibo, sys
dir(fibo)
**output : **
['__name__', 'fib', 'fib2']
Exercise ** Write a Python program to create an array of 5 integers and display the array items. Access individual element through indexes
from array import *
array_num = array('i', [1,3,5,7,9])
for i in array_num:
print(i)
print("Access first three items individually")
print(array_num[0])
print(array_num[1])
print(array_num[2])
Exercise ** Write a Python program to append a new item to the end of the array.
from array import *
array_num = array('i', [1, 3, 5, 7, 9])
print("Original array: "+str(array_num))
print("Append 11 at the end of the array:")
array_num.append(11)
print("New array: "+str(array_num))
Exercise ** Write a Python program to reverse the order of the items in the array.
from array import *
array_num = array('i', [1, 3, 5, 3, 7, 1, 9, 3])
print("Original array: "+str(array_num))
array_num.reverse()
print("Reverse the order of the items:")
print(str(array_num))
Exercise ** Write a Python program to get the length in bytes of one array item in the internal representation.
from array import *
array_num = array('i', [1, 3, 5, 7, 9])
print("Original array: "+str(array_num))
print("Length in bytes of one array item: "+str(array_num.itemsize))