CERTIFIED ASSOCIATE IN PYTHON PROGRAMMING
BY: IMRAN
Module 1: Modules in
• Contents
Python
•
Part 1: Introduction to Outcomes
Modules in Python • What is a module?
• Using a Module
• Importing a module
• CONTENTS • Working with standard modules
• Selected functions from the math module
• Outcomes
• Is there real randomness in computers?
• What is a module? • Selected functions from the random module
• Using a Module • How to know where you are?
• Importing a • Selected functions from the platform module
module • Python Module Index
• What is a package?
• Your first module
• Your first package
ImranNust imran_muet muhammad-imran-b7865495 2
Module 1: Modules in • Python packaging ecosystem and how to use it
Python
Part 1: Introduction to • The PyPI repo: the Cheese Shop
Modules in Python • How to install pip
• pip on MS Windows
• pip on Linux
• CONTENTS • Dependencies
• How to use pip
• Outcomes
• What is a module?
• Using a Module
• Importing a
module
ImranNust imran_muet muhammad-imran-b7865495 3
Module 1: Modules in • In this module, you will learn about:
Python
Part 1: Introduction to • importing and using Python modules;
Modules in Python • using some of the most useful Python standard library
modules;
• constructing and using Python packages;
• PIP (Python Installation Package) and how to use it to install
• CONTENTS and uninstall ready-to-use packages from PyPI.
• OUTCOMES
• What is a module?
• Using a Module
• Importing a
module
ImranNust imran_muet muhammad-imran-b7865495 4
Module 1: Modules in • In Python, Modules are simply files with the “. py”
Python
Part 1: Introduction to extension containing Python code that can be imported
Modules in Python inside another Python Program. In simple terms, we can
consider a module to be the same as a code library or a file
that contains a set of functions that you want to include in
• CONTENTS your application.
• Outcomes
• WHAT IS A
MODULE?
• Using a Module
• Importing a
module
ImranNust imran_muet muhammad-imran-b7865495 5
Module 1: Modules in • A module is a file containing Python definitions and
Python
Part 1: Introduction to statements, which can be later imported and used
Modules in Python when necessary.
• A module is identified by its name.
• A large number of modules is delivered together with
• CONTENTS Python itself.
• Outcomes
• What is a module?
• USING A
MODULE
• Importing a
module
ImranNust imran_muet muhammad-imran-b7865495 6
Module 1: Modules in • Each module consists of entities (like a book consists of
Python
Part 1: Introduction to chapters).
Modules in Python • These entities can be functions, variables, constants,
classes, and objects. If you know how to access a
particular module, you can make use of any of the
• CONTENTS entities it stores.
• Outcomes •
• What is a module?
• USING A
MODULE
• Importing a
module
• There is a frequently used module, named math. The
module contains a rich collection of mathematical
functions, like sin() or log().
ImranNust imran_muet muhammad-imran-b7865495 7
Module 1: Modules in • To make a module usable, you must import it.
Python
Part 1: Introduction to Importing a module is done by an instruction named
Modules in Python import.
• Note: import is also a keyword (with all the
consequences of this fact).
• CONTENTS
• Outcomes
• What is a module?
• Using a Module
• IMPORTING A
MODULE
ImranNust imran_muet muhammad-imran-b7865495 8
Module 1: Modules in • Let's assume that you want to use two entities provided
Python
Part 1: Introduction to by the math module:
Modules in Python
• a symbol (constant) representing a precise value of π
• a function named sin()
• CONTENTS
• Both these entities are available through the math
• Outcomes module, but the way in which you can use them strongly
• What is a module? depends on how the import has been done.
• Using a Module
• IMPORTING A
MODULE
ImranNust imran_muet muhammad-imran-b7865495 9
Module 1: Modules in • The simplest way to import a particular module is to use the
Python
Part 1: Introduction to import instruction as follows:
Modules in Python
import math
• If you want to (or have to) import more than one module, you
can do it by repeating the import clause (preferred):
• CONTENTS
• Outcomes import math
• What is a module? import sys
• Using a Module • or by listing the modules after the import keyword, like here:
• IMPORTING A import math, sys
MODULE
• The instruction imports two modules, first the one
named math and then the second named sys.
ImranNust imran_muet muhammad-imran-b7865495 10
Module 1: Modules in import math
Python
Part 1: Introduction to
Modules in Python
def sin(x):
if 2 * x == pi:
• CONTENTS return 0.99999999
• Outcomes else:
return None
• What is a module?
• Using a Module
• IMPORTING A pi = 3.14
MODULE
print(sin(pi/2))
print(math.sin(math.pi/2))
ImranNust imran_muet muhammad-imran-b7865495 11
Module 1: Modules in • Exercise: Perform the following tasks
Python
Part 1: Introduction to • Use from keyword to import pi from the math module
Modules in Python • Import e (the mathematical constant) from the math
module.
• Write a program to perform the following operation
• CONTENTS sin(pi/2)
• Outcomes
• What is a module?
• Using a Module
• IMPORTING A
MODULE
ImranNust imran_muet muhammad-imran-b7865495 12
Module 1: Modules in • Exercise: Perform the following tasks
Python
Part 1: Introduction to • Use from keyword to import pi from the math module
Modules in Python
from math import pi
• CONTENTS • Import e (the mathematical constant) from the math
• Outcomes module.
import math
• What is a module?
Print(math.e)
• Using a Module
• IMPORTING A • Write a program to perform the following operation
MODULE sin(pi/2)
from math import sin, pi
print(sin(pi/2))
ImranNust imran_muet muhammad-imran-b7865495 13
Module 1: Modules in pi = 3.14
Python
Part 1: Introduction to
Modules in Python
def sin(x):
if 2 * x == pi:
• CONTENTS return 0.99999999
• Outcomes else:
return None
• What is a module?
• Using a Module
• IMPORTING A print(sin(pi / 2))
MODULE
from math import sin, pi
print(sin(pi / 2))
ImranNust imran_muet muhammad-imran-b7865495 14
Module 1: Modules in • Importing a module: *
Python
Part 1: Introduction to • The following import's syntax is a more aggressive form of the
Modules in Python previously presented one:
from module import *
• As you can see, the name of an entity (or the list of entities'
• CONTENTS names) is replaced with a single asterisk (*).
• Outcomes • Such instruction imports all entities from the indicated module.
• Is it convenient? Yes, it is, as it relieves you of the duty of
• What is a module? enumerating all the names you need.
• Using a Module • Is it unsafe? Yes, it is - unless you know all the names provided
by the module, you may not be able to avoid name conflicts.
• IMPORTING A Treat this as a temporary solution, and try not to use it in
MODULE regular code.
ImranNust imran_muet muhammad-imran-b7865495 15
Module 1: Modules in • Importing a module: the as keyword
Python
Part 1: Introduction to • If you use the import module variant and you don't like a
Modules in Python particular module's name (e.g., it's the same as one of your
already defined entities, so qualification becomes troublesome)
you can give it any name you like - this is called aliasing.
• Aliasing causes the module to be identified under a different
name than the original. This may shorten the qualified names,
• CONTENTS too.
• Outcomes • Creating an alias is done together with importing the module,
and demands the following form of the import instruction:
• What is a module?
• Using a Module import module as alias
• IMPORTING A
MODULE • The "module" identifies the original module's name while the
"alias" is the name you wish to use instead of the original.
• Note: as is a keyword.
ImranNust imran_muet muhammad-imran-b7865495 16
Module 1: Modules in • Examples 1
Python
Part 1: Introduction to import math as m
Modules in Python print(m.sin(m.pi/2))
• Example 2
• CONTENTS
• Outcomes from math import pi as PI, sin as sine
print(sine(PI/2))
• What is a module?
• Using a Module
• IMPORTING A
MODULE
ImranNust imran_muet muhammad-imran-b7865495 17