Modules
To make your code more
modular
Modules
A module is a file containing Python code (functions, classes, variables) that you can
reuse in other Python programs.
Modules can be created simply by saving Python code in a .py file and then imported
using the import statement.
Its goal: group related pieces of functionality, which can then be imported and used
elsewhere, keeping your code clean and organized.
Module
Intro s
module1.py
project.py
module2.py
module3.py
Open Source Department –
Module
How TO? 1. You can import a module into another Python script using:
s
import module_name
module1.py
project.py
import module1
module2.py
module3.py
Open Source Department –
Module
How s
To
2. You can import specific functions or variables from a module using:
from ... import ...
from module_name import block_name
math.py
i.e. from math import
tan
Open Source Department –
Module
Packages: directory containing a collection of modules. s
Here, python imports only the specified block_name from the module, rather
than importing the entire module.
from pkge_name.module_name import block_name
Science Directory ( Folder )
math.py physics.p
y
i.e. from science.math import
tan
Open Source Department –
File Input & Output
File
Authoring
File Input &
Open Output
Files
open(file_name, mode)
mode Job description
r Open Files for reading
only
w Open Files for writing
only *
a Open Files for
appending *
r+ Open Files for reading and
writing *
rb Open Files for reading binary
files
rb+ Open Files for reading and writing binary
files *
* If the file not exist , It will
create it.
Open Source Department –
File Input &
Read Output
Files
fl = open(“D:/some_file.txt”, ‘r’)
#reads the entire content of the file some_file.txt
fl.read()
Some text on line
#output: Some text on line 1.
1. Other text on
Other text on line 2.
line 2.
#Read the first 4 characters of
the file
fl.read(4)
#output: Some
#Read the first line of
the file
fl.readline()
#output:text on line 1.
Open Source Department –
File Input &
Read Output
Files
#Read all lines into a
list fl.readlines() some_file.txt
#output: Some text on line
Some text on line 1. Other text on
1. line 2.
Other text on line
2.
fl =
open(“D:/some_file.
txt”, ‘r’)
for line in f1:
print(line)
#output: Some text
Open
on Source
line 1. Department –
File Input &
Write on Output
Files
some_file.txt
Some text on line
fl = open(“some_file.txt”, 1. Other text on
‘w’) line 2.
Open Source Department –
File Input &
Write on Output
Files
some_file.txt
Some
This istext
newon line
content
1.
fl = open(“some_file.txt”,
Other text on line
‘w’) fl.write(“This is new 2.
content”)
Open Source Department –
File Input &
Write on Output
some_file.txt
Files
This is new content
fl = open(“some_file.txt”, ‘w’)
fl.write(“This is new content”) Some text on line
1.
fl.seek(8)
Other text on line
2.
“””
fl.seek(8): the file pointer will move to just
after the first 8 characters of the written content.
The pointer will now be positioned at the start of the
word "new".
This does not change the file content by itself,
but it changes where the next read or write will
start.
“””
Open Source Department –
File Input &
Write on Output
Files
some_file.txt
Some new
This istext
old on line
content
1.
fl = open(“some_file.txt”,
Other text on line
‘w’) fl.write(“This is new 2.
content”) fl.seek(8)
fl.write(“old”)
Open Source Department –
File Input &
Write on Output
Files
some_file.txt
This istext
Some new
old on line
content
1.
fl = open(“some_file.txt”, content is
Other text on line
‘w’) fl.write(“This is new appended
2.
content”) fl.seek(8)
fl.write(“old”)
fl.close()
fl =
open(“some_fil
e.txt”, ‘a’)
fl.write(“\n
content is
appended”)
Open Source Department –
Python Standard
Library
Python Standard
math Library
math module provides access to the mathematical functions by the C
standard
import math
math.ceil(3.2) # 4 #It returns the smallest integer greater
than or equal to x.
math.floor(3.6) # 3 #It returns the largest integer less than
or equal to x.
math.sqrt(9) # 3
math.pi # 3.14
Open Source Department –
External
Libraries
pip
tool
External
pi Libraries
p
pip is a package management system used to install and manage
software packages written in Python
pip install “some library”
i.e. pip install libcloud
libcloud is a Python library that provides a
unified interface for interacting with various
cloud service providers.
Open Source Department –
Tips and
Tricks
Tips and
with statement Tricks
- Using the with statement for file handling:
-It ensures that files are properly closed after their use, even if
an error occurs during file processing.
with open(“file.txt”, ‘r’) as
fp:
fp.read()
Open Source Department –
Tips and
List Tricks
Comprehension
It is an easy method to construct
a list
Outpu Input
t sequence
[ x**2 for x in range(10) if x%2 == 0 ]
variabl Optional
e Condition
L = [ x**2 for x in range(10) if x%2 ==
0 ] #output: [0, 4, 16, 36, 64]
Open Source Department –
Thank
You