Lecture 7-8

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 26

FCCS002

Lecture 7-8
Dictionaries, modules & packages

Dr. Poonam Rani


Assistant professor
Computer Science Engineering
NSUT
3/5/21 1
Dictionary
• A mapping object maps immutable values to arbitrary objects.
• There is currently only one standard mapping type, the dictionary.
• Dictionaries are used to store data values in key:value pairs.
• A dictionary is a collection which is ordered*, changeable and does not allow duplicates.
• Dictionaries are written with curly brackets, and have keys and values.

Create and print a dictionary:





Output:

{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}

3/5/21 2

Change value in Dictionary
Change value of a specific item by its key name:

Change year to 2018:


Output:
{'brand': 'Ford', 'model': 'Mustang', 'year': 2018}

Add items in Dictionary
Adding an item to the dictionary is done by
using a new index key and assigning a value to it:






Output:
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'red'}

3/5/21 3

Remove items in Dictionary
There are several methods to remove items from a dictionary::
1.  pop() method removes the item with the specified key name:






Output:
{'brand': 'Ford‘, 'year': 2018}



2. popitem() method removes the last inserted item





Output:
{'brand': 'Ford', 'model': 'Mustang'}

3/5/21 4

3/5/21 5
3/5/21 6
Remove items in Dictionary
3.  The del keyword removes the item with
the specified key name:







Output:
{'brand': 'Ford‘, 'year': 2018}

4. The clear() method empties the dictionary:







Output:
{ }
3/5/21
7
3/5/21 8
Lists Tuples Sets Dictionary
List is a non-
Tuple is also a non-
homogeneous data Set data structure is also Dictionary is also a non-
homogeneous data
structure which stores non-homogeneous data homogeneous data
structure which stores
the elements in single structure but stores in structure which stores
single row and multiple
row and multiple rows single row key value pairs
rows and columns
and columns

List can be represented Tuple can be represented Set can be represented Dictionary can be
by [ ] by ( ) by { } represented by { }

Set will not allow


List allows duplicate Tuple allows duplicate Set will not allow
duplicate elements but
elements elements duplicate elements
keys are not duplicated

Set is mutable i.e we can


Tuple is immutable i.e
List is mutable i.e we can make any changes in set. Dictionary is mutable. But
we can not make any
make any changes in list. But elements are not Keys are not duplicated.
changes in tuple
duplicated.

List is ordered Tuple is ordered Set is unordered Dictionary is ordered

list can be created Tuple can be created Set can be created Dictionary can be created
using list() function using tuple() function. using set() function using dict() function.
Applications of List, Set, Tuple, and Dictionary
List:
• Used in JSON format
• Useful for Array operations
• Used in Databases
Tuple:
• Used to insert records in the database through SQL query at a
time
Ex: (1.’sravan’, 34).(2.’geek’, 35)
• Used in parentheses checker
Set
• Finding unique elements
• Join operations
Dictionary
• Used to create a data frame with lists
• Used in JSON

Modules and Packages

•  As we create more and more classes we need a way to


organize them in such a way to make then available
and easy to find.
•  Modules in Python are simply files, if you have two
files in the same folder we can load a class from one
module for use in the other module.
•  A module is a file containing Python code.
•  A package, however, is like a directory that holds
sub-packages and modules.
ü  A package must hold the file __init__.py. This does
not apply to modules.
What is a Module?

•  A module is a file containing a set of functions you want to


include in your application.
•  Create a Module: To create a module just save the code you
want in a file with the file extension .py:
•  Ex: Save this code in a file named mymodule.py
def greeting(name):
print("Hello, " + name)
Use a Module
•  Now we can use the module we just created, by using
the import statement:
Example
•  Import the module named mymodule, and call the greeting
function:
•  import mymodule
mymodule.greeting("Jonathan")

•  Output:
•  Hello, Jonathan
Naming a Module
You can name the module file whatever you like, but it must have
the file extension .py
Re-naming a Module
You can create an alias when you import a module, by using
the as keyword:
Example:
Create an alias for mymodule called mx:

import mymodule as mx

Built-in Modules

There are several built-in modules in Python, which you can


import whenever you like.
Example: Import and use the platform module:

import platform
x = platform.system()
print(x)
Output: Windows

from...import Statement

Python's from statement lets you import specific attributes from


a module into the current namespace. The from...import has the
following syntax −
from modname import name
For example, to import the function fibonacci from the module
fib:
from fib import fibonacci

from...import * Statement
It is also possible to import all names from a module into the
current namespace by using the following import statement −

from modname import *
Locating Modules

When you import a module, the Python interpreter searches for


the module in the following sequences −
q The current directory.
q If the module isn't found, Python then searches each directory
in the shell variable PYTHONPATH.
q If all else fails, Python checks the default path. On UNIX, this
default path is normally /usr/local/lib/python/.

Differences
•  Module is a file which contains various Python functions and
global variables. It is simply just .py extension file which has
python executable code.
•  Package is a collection of modules. It must contain an init.py
file as a flag so that the python interpreter processes it as such.
The init.py could be an empty file without causing issues.
•  Library is a collection of packages.
•  Framework is a collection of libraries. This is the architecture
of the program.
3/5/21 26
36

You might also like