PACKAGES &
DATASTRUCTURES in
PYTHON
1
By
B.Hemalatha
Assistant Professor
VEC
PACKAGES / LIBRARY
 In Computer we store files in organized hierarchies
 Subdirectories
 Files
 Large projects in Python are organized as Packages
which consists of many
 Sub-Packages
 Similar Modules
 Packages makes projects easy to manage and conceptually clear
2
PACKAGES / LIBRARY
 Python package is a directory containing
 Sub-packages
 Modules
 Along with an __init__.py file
Python module
 Is a . py file
 Contains functions, variables
3
double underscore
Mensuration
Square.py
Rectangle.py
Circle.py
Eg 1. PACKAGE
4
M
O
D
U
L
E
S
Directory
Package
• Game Development Package Organization
Eg 2.PACKAGES / LIBRARY
5
PACKAGE
list
SUBPACKAGES
M
O
D
U
L
E
S
• Steps to create a package
– Create a directory with the package name
– Create modules under it
– Create a file __init__.py in the directory
Note : File __init__.py is a simple file required to make python treat the
directory on the disk as packages of python
• Access packages and its modules
– from PACKAGE_NAME import MODULE_NAME
• Access functions in modules
– Use dot operator “ . ”
– MODULE_NAME . FUNCTION_NAME
PACKAGES / LIBRARY
6
PACKAGES / LIBRARY
mensuration
square.py rectangle.py
Modules/
Python Files
Directory /
Package
Example:
7
Step1 : Folder/Directory Creation:
Folder/directory named “mensuration”
creation
Step 2: Module Creation - I: “square.py”
# Square Module
def area(side):
return side * side
def peri(side):
return 4 * side
PACKAGES / LIBRARY
8
Step 2 : Module Creation - II: “ rectangle.py”
# Rectangle Module
def area(length,breadth):
return length*breadth
def peri(length,breadth):
return 2*(length+breadth)
Step 3 : file __init__.py Creation
PACKAGES / LIBRARY
9
To Access Packages and its Modules
from mensuration import square,rectangle
To Access functions inside the Modules
square . area (side)
rectangle . area (length,breadth)
PACKAGES / LIBRARY
10
#Rectangle Module
def area(length,breadth):
def peri(length,breadth):
# Square Module
def area(side):
def peri(side):
1. Package in Python is a
– (a) File
– (b) Database
– (c) hierarchical file directory
– (d) hierarchical database directory
»(Ans: c)
11
2. To include a package in python we use
– (a) include statement
– (b) import statement
– (c) package include statement
– (d) package import statement
»(Ans: b)
12
3. A Package contains
– (a) sub-packages
– (b) modules
– (c)__init__.py
– (d) All the above
»(Ans: d)
4. Python supports built in packages
– (a) Yes
– (b) No
»(Ans: a)
13
5. The package folder contains a special file
called__________
– (a) __init__.py,
– (b) Pack_.py
– (c)_pack_,py
– (d)_init_pack_.py
»(Ans: a)
14
INTRODUCTION TO
DATA STRUCTURES
15
Data structure
 way to store and organize data
 helps in writing efficient programs in any
language
Python is a high-level, interpreted, interactive
and object-oriented scripting language using
which we can study the fundamentals of data
structure in a simpler way as compared to
other programming languages.
Data structure in Python
16
Data structure in Python
17
Data Structures in Python
Built-in Data Structures User-Defined Data Structures
List
Tuple
Dictionary
Set
Stack
Queue
Tree
Graph
Linked
list
Today we will focus on few Data Structures
1.Stacks
2.Queues
3.Graph
Data structure in Python
18
Stack
Data structure - Stack
19
Stack
 is an ordered collection of items
 addition of new items
 removal of existing items
 this ordering principle is called Last-In First-Out(LIFO)
A Stack of Books A Stack of Primitive Python Objects
Data structure - Stack
20
always takes place at the same end
usually top
Data structure - Stack
21
Stack
Insertion
“PUSH”
Deletion
“POP”
Top
LIFO PRINCIPLE
Stack using LIST in Python
22
Stack LIFO PRINCIPLE
Stack using LIST in Python
Example:
# empty stack
stack = [ ]
# push operation # pop operation
23
OUTPUT
stack.append(10) stack =[10]
stack.append(20) stack =[10,20]
stack.append(30) stack =[10,20,30]
OUTPUT
stack.pop() stack =[10,20,30]
stack.pop() stack =[10,20]
top
top
top
top
top
Data structure - Queue
24
QUEUE
Queue
 is an ordered collection of items
 Addition of new items happens at one end, called the “rear,”
 Removal of existing items occurs at the other end, called the “front.”
 This ordering principle is sometimes called First-In First-Out (FIFO)
A Queue of Python Data Objects
Data structure - Queue
25
Insertion – Enqueue
Deletion - Dequeue
Data structure - Queue
26
QUEUE
Rear Front
FIFO PRINCIPLE
Queue using LIST in Python
Example:
# empty queue
queue = [ ]
# enqueue operation # dequeue operation
27
OUTPUT
queue.append(10) queue =[10]
queue.append(20) queue =[10,20]
queue.append(30) queue =[10,20,30]
OUTPUT
queue.pop(0) queue =[10,20,30]
queue.pop(0) queue =[20,30]
rear
rear
front
front
rear
QUEUE APPLICATIONS – SHARING PRINTER RESOURCE
Data structure - Queue
28
A
B
C
D
Job 1 Job 2
Job 3
Job 3 Job 2 Job 1
Printer queue
front
Data structure - Graph
Graph
Data is stored in a collection of vertices(nodes)
interconnected by edges (path)
29
Graph using DICTIONARY
Example:
# GRAPH structure
graph = { “a”:[“d”], “b”:[“c”],“c”:[“d”,”e”],
“d”: [“a”,”c”] ,”e”:[“c”]}
30
Application –
Undirected Graph
31
• Social Network – FACEBOOK
Suggest Friends
Application – Directed Graph
32
• World Wide Web
1. Consider the following operation performed on a stack
Push(1);
Pop();
Push(2);
Push(3);
Pop();
Push(4);
Pop();
After the completion of all operation, the number of elements
present in stack are
- (a) 1
- (b) 2
- (c) 3
- (d) 4
»(Ans: a)
33
2. A list of elements in which deletion can be
done from one end (front) and insertion can
take place only at the other end (rear) is known
as
- (a) Queue
- (b) Stack
- (c) Tree
- (d) Linked list
»(Ans: a)
34
3. Google Map is an application of
- (a) Tree
- (b) Stack
- (c) Graph
- (d) Queue
»(Ans: c)
35
Thank you
36

Packages and Datastructures - Python

  • 1.
  • 2.
    PACKAGES / LIBRARY In Computer we store files in organized hierarchies  Subdirectories  Files  Large projects in Python are organized as Packages which consists of many  Sub-Packages  Similar Modules  Packages makes projects easy to manage and conceptually clear 2
  • 3.
    PACKAGES / LIBRARY Python package is a directory containing  Sub-packages  Modules  Along with an __init__.py file Python module  Is a . py file  Contains functions, variables 3 double underscore
  • 4.
  • 5.
    • Game DevelopmentPackage Organization Eg 2.PACKAGES / LIBRARY 5 PACKAGE list SUBPACKAGES M O D U L E S
  • 6.
    • Steps tocreate a package – Create a directory with the package name – Create modules under it – Create a file __init__.py in the directory Note : File __init__.py is a simple file required to make python treat the directory on the disk as packages of python • Access packages and its modules – from PACKAGE_NAME import MODULE_NAME • Access functions in modules – Use dot operator “ . ” – MODULE_NAME . FUNCTION_NAME PACKAGES / LIBRARY 6
  • 7.
    PACKAGES / LIBRARY mensuration square.pyrectangle.py Modules/ Python Files Directory / Package Example: 7
  • 8.
    Step1 : Folder/DirectoryCreation: Folder/directory named “mensuration” creation Step 2: Module Creation - I: “square.py” # Square Module def area(side): return side * side def peri(side): return 4 * side PACKAGES / LIBRARY 8
  • 9.
    Step 2 :Module Creation - II: “ rectangle.py” # Rectangle Module def area(length,breadth): return length*breadth def peri(length,breadth): return 2*(length+breadth) Step 3 : file __init__.py Creation PACKAGES / LIBRARY 9
  • 10.
    To Access Packagesand its Modules from mensuration import square,rectangle To Access functions inside the Modules square . area (side) rectangle . area (length,breadth) PACKAGES / LIBRARY 10 #Rectangle Module def area(length,breadth): def peri(length,breadth): # Square Module def area(side): def peri(side):
  • 11.
    1. Package inPython is a – (a) File – (b) Database – (c) hierarchical file directory – (d) hierarchical database directory »(Ans: c) 11
  • 12.
    2. To includea package in python we use – (a) include statement – (b) import statement – (c) package include statement – (d) package import statement »(Ans: b) 12
  • 13.
    3. A Packagecontains – (a) sub-packages – (b) modules – (c)__init__.py – (d) All the above »(Ans: d) 4. Python supports built in packages – (a) Yes – (b) No »(Ans: a) 13
  • 14.
    5. The packagefolder contains a special file called__________ – (a) __init__.py, – (b) Pack_.py – (c)_pack_,py – (d)_init_pack_.py »(Ans: a) 14
  • 15.
  • 16.
    Data structure  wayto store and organize data  helps in writing efficient programs in any language Python is a high-level, interpreted, interactive and object-oriented scripting language using which we can study the fundamentals of data structure in a simpler way as compared to other programming languages. Data structure in Python 16
  • 17.
    Data structure inPython 17 Data Structures in Python Built-in Data Structures User-Defined Data Structures List Tuple Dictionary Set Stack Queue Tree Graph Linked list
  • 18.
    Today we willfocus on few Data Structures 1.Stacks 2.Queues 3.Graph Data structure in Python 18
  • 19.
  • 20.
    Stack  is anordered collection of items  addition of new items  removal of existing items  this ordering principle is called Last-In First-Out(LIFO) A Stack of Books A Stack of Primitive Python Objects Data structure - Stack 20 always takes place at the same end usually top
  • 21.
    Data structure -Stack 21 Stack Insertion “PUSH” Deletion “POP” Top LIFO PRINCIPLE
  • 22.
    Stack using LISTin Python 22 Stack LIFO PRINCIPLE
  • 23.
    Stack using LISTin Python Example: # empty stack stack = [ ] # push operation # pop operation 23 OUTPUT stack.append(10) stack =[10] stack.append(20) stack =[10,20] stack.append(30) stack =[10,20,30] OUTPUT stack.pop() stack =[10,20,30] stack.pop() stack =[10,20] top top top top top
  • 24.
    Data structure -Queue 24 QUEUE
  • 25.
    Queue  is anordered collection of items  Addition of new items happens at one end, called the “rear,”  Removal of existing items occurs at the other end, called the “front.”  This ordering principle is sometimes called First-In First-Out (FIFO) A Queue of Python Data Objects Data structure - Queue 25
  • 26.
    Insertion – Enqueue Deletion- Dequeue Data structure - Queue 26 QUEUE Rear Front FIFO PRINCIPLE
  • 27.
    Queue using LISTin Python Example: # empty queue queue = [ ] # enqueue operation # dequeue operation 27 OUTPUT queue.append(10) queue =[10] queue.append(20) queue =[10,20] queue.append(30) queue =[10,20,30] OUTPUT queue.pop(0) queue =[10,20,30] queue.pop(0) queue =[20,30] rear rear front front rear
  • 28.
    QUEUE APPLICATIONS –SHARING PRINTER RESOURCE Data structure - Queue 28 A B C D Job 1 Job 2 Job 3 Job 3 Job 2 Job 1 Printer queue front
  • 29.
    Data structure -Graph Graph Data is stored in a collection of vertices(nodes) interconnected by edges (path) 29
  • 30.
    Graph using DICTIONARY Example: #GRAPH structure graph = { “a”:[“d”], “b”:[“c”],“c”:[“d”,”e”], “d”: [“a”,”c”] ,”e”:[“c”]} 30
  • 31.
    Application – Undirected Graph 31 •Social Network – FACEBOOK Suggest Friends
  • 32.
    Application – DirectedGraph 32 • World Wide Web
  • 33.
    1. Consider thefollowing operation performed on a stack Push(1); Pop(); Push(2); Push(3); Pop(); Push(4); Pop(); After the completion of all operation, the number of elements present in stack are - (a) 1 - (b) 2 - (c) 3 - (d) 4 »(Ans: a) 33
  • 34.
    2. A listof elements in which deletion can be done from one end (front) and insertion can take place only at the other end (rear) is known as - (a) Queue - (b) Stack - (c) Tree - (d) Linked list »(Ans: a) 34
  • 35.
    3. Google Mapis an application of - (a) Tree - (b) Stack - (c) Graph - (d) Queue »(Ans: c) 35
  • 36.