Python Programming

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

PYTHON PROGRAMMING

(For M.Sc (Computer Science), Semester -I)

Paper code: 23PCS03


Table of Contents
Contents
UNIT-I ............................................................................................................................................ 1
1.1 INTRODUCTION ................................................................................................................. 1
1.2 NUMBERS............................................................................................................................ 2
1.3 STRINGS .................................................................................................................................. 3
1.4 VARIABLES......................................................................................................................... 7
1.5 LISTS .................................................................................................................................... 8
1.6 TUPLES .............................................................................................................................. 14
1.7 DICTIONARIES ................................................................................................................. 19
1.8 SETS .................................................................................................................................... 27
1.9 COMPARISION ................................................................................................................. 31
UNIT – II ...................................................................................................................................... 33
2.1 CODE STRUCTURES ...................................................................................................... 33
2.2 IF, ELIF, AND ELSE .......................................................................................................... 33
2.3 REPEAT WITH WHILE..................................................................................................... 36
2.4 ITERATE WITH FOR ........................................................................................................ 37
2.5 COMPREHENSIONS ......................................................................................................... 40
2.6 FUNCTIONS....................................................................................................................... 45
2.7 GENERATORS................................................................................................................... 49
2.8 DECORATORS .................................................................................................................. 51
2.9 NAMESPACES AND SCOPE ........................................................................................... 55
2.10 HANDLE ERRORS WITH TRY AND EXCEPT ............................................................ 56
2.11 USER EXCEPTIONS ....................................................................................................... 58
UNIT- III....................................................................................................................................... 60
3.1 MODULES, PACKAGES, AND PROGRAMS ................................................................. 60
3.1.1 STANDALONE PROGRAMS ....................................................................................... 60
3.1.2 COMMAND-LINE ARGUMENTS ............................................................................... 60
3.1.3 MODULES AND THE IMPORT STATEMENT .......................................................... 62
3.1.4 THE PYTHON STANDARD LIBRARY .................................................................... 64
3.2 OBJECTS AND CLASSES ................................................................................................ 66
3.2.1 DEFINE A CLASS WITH CLASS................................................................................. 66
3.2.2 INHERITANCE .............................................................................................................. 67
3.2.3 OVERRIDE A METHOD ............................................................................................... 69
3.2.4 ADD A METHOD .......................................................................................................... 71
3.2.5 GET HELP FROM YOUR PARENT WITH SUPER .................................................... 72
3.2.6 IN SELF DEFENSE ........................................................................................................ 72
3.2.7 GET AND SET ATTRIBUTE VALUES WITH PROPERTIES.................................... 73
3.2.8 NAME MANGLING FOR PRIVACY ........................................................................... 74
3.2.9 METHOD TYPES ........................................................................................................... 75
3.2.10 DUCK TYPING ............................................................................................................ 77
3.2.11 SPECIAL METHODS .................................................................................................. 77
3.2.12 COMPOSITION ............................................................................................................ 78
UNIT- IV ...................................................................................................................................... 79
4.1 DATA TYPES AND WEB ................................................................................................. 79
4.1.1 TEXT STRINGS ............................................................................................................. 79
4.1.2 BINARY DATA.............................................................................................................. 80
4.2 STORING AND RETRIEVING DATA ............................................................................. 81
4.2.1 FILE INPUT/OUTPUT ................................................................................................... 81
4.2.2 STRUCTURED TEXT FILES ........................................................................................ 85
4.2.3 STRUCTURED BINARY FILES ................................................................................... 85
4.2.4 RELATIONAL DATABASES ....................................................................................... 86
4.2.5 NoSQL DATA STORES................................................................................................. 90
4.3 THE WEB ........................................................................................................................... 94
4.3.1 WEB CLIENTS ............................................................................................................... 94
4.3.2 WEB SERVERS .............................................................................................................. 97
4.3.3 WEB SERVICES AND AUTOMATION ...................................................................... 98
UNIT-V ....................................................................................................................................... 100
5.1 SYSTEMS ......................................................................................................................... 100
5.1.1 FILES ............................................................................................................................ 100
5.1.2 DIRECTORIES ............................................................................................................. 103
5.1.3 PROGRAMS AND PROCESSES ................................................................................ 105
5.1.4 CALENDARS AND CLOCKS..................................................................................... 106
5.2 CONCURRENCY ............................................................................................................. 109
5.2.1 QUEUES ....................................................................................................................... 109
5.2.2 PROCESSES ................................................................................................................. 110
5.2.3 THREADS..................................................................................................................... 110
5.2.4 GREEN THREADS AND GEVENT ............................................................................ 111
5.2.5 TWISTED ..................................................................................................................... 112
5.2.6 REDIS ........................................................................................................................... 112
5.3 NETWORKS ..................................................................................................................... 113
5.3.1 PATTERNS ................................................................................................................... 114
5.3.2 THE PUBLISH-SUBSCRIBE MODEL ....................................................................... 114
5.3.3 TCP/IP ......................................................................................................................... 115
5.3.4 SOCKETS ..................................................................................................................... 115
5.3.5 ZEROMQ ...................................................................................................................... 115
5.3.6 INTERNET SERVICES ................................................................................................ 116
5.3.7 WEB SERVICES AND APIS ....................................................................................... 117
5.3.8 REMOTE PROCESSING ............................................................................................. 117
5.3.9 BIG FAT DATA AND MAPREDUCE ........................................................................ 119
5.3.10 WORKING IN THE CLOUDS ................................................................................... 120
MODEL QUESTION PAPER ................................................................................................ 121
MODEL QUESTION PAPER ................................................................................................ 123
SYLLABUS
PYTHON PROGRAMMING
UNIT - I
Python: Introduction - Numbers - Strings - Variables - Lists - Tuples - Dictionaries - Sets -
Comparison.
UNIT - II
Code Structures: if, elif, and else - Repeat with while - Iterate with for - Comprehensions -
Functions - Generators - Decorators - Namespaces and Scope - Handle Errors with try and
except - User Exceptions.
UNIT - III
Modules, Packages, and Programs: Standalone Programs - Command-Line Arguments -
Modules and the import Statement - The Python Standard Library. Objects and Classes:
Define a Class with class - Inheritance - Override a Method - Add a Method - Get Help from
Parent with super - In self Defense - Get and Set Attribute Values with Properties - Name
Mangling for Privacy - Method Types - Duck Typing - Special Methods - Composition
UNIT-IV
Data Types: Text Strings - Binary Data. Storing and Retrieving Data: File Input/Output -
Structured Text Files - Structured Binary Files - Relational Databases - NoSQL Data Stores.
Web: Web Clients - Web Servers - Web Services and Automation
UNIT-V
Systems: Files -Directories - Programs and Processes - Calendars and Clocks.
Concurrency: Queues - Processes - Threads - Green Threads and gevent - twisted - Redis.
Networks: Patterns - The Publish-Subscribe Model - TCP/IP - Sockets - ZeroMQ -Internet
Services - Web Services and APIs - Remote Processing - Big Fat Data and MapReduce -
Working in the Clouds.
TEXT BOOK
1. Bill Lubanovic, "Introducing Python", O'Reilly, First Edition-Second Release, 2014.
2. Mark Lutz, "Learning Python", O'Reilly, Fifth Edition, 2013.
REFERENCE BOOKS
1. David M. Beazley,"Python Essential Reference", Developer's Library, Fourth
Edition,2009.
2. SheetTaneja,Naveen Kumar,”Python Programming –A Modular Approach”,Pearson
Publications.
UNIT-I

1.1 INTRODUCTION

Python is a popular programming language. It was created by Guido van Rossum,


and released in 1991.
It is used for :
• web development (server-side),
• software development,
• mathematics,
• system scripting.
• Python is a simple, general purpose, high level, and object-oriented programming
language.
• Python is an interpreted scripting language also. Guido Van Rossum is known as the
founder of Python programming.
• Python is a general-purpose, dynamic, high-level, and interpreted programming
language. It supports Object Oriented programming approach to develop applications. It
is simple and easy to learn and provides lots of high-level data structures
• Python is an easy-to-learn yet powerful and versatile scripting language, which makes it
attractive for Application Development.
• We don't need to use data types to declare variable because it is dynamically typed, so
we can write a=10 to assign an integer value in an integer variable.
• Python makes development and debugging fast because no compilation step is included
in Python development, and the edit-test-debug cycle is very fast.
• Python has many web-based assets, open-source projects, and a vibrant community.
Learning the language, working together on projects, and contributing to the Python
ecosystem are all made very easy for developers.
You’ll find Python in many computing environments, including the following:
• The command line in a monitor or terminal window
• Graphical user interfaces, including the Web
• The Web, on the client and server sides
• Backend servers supporting large popular sites
• The cloud (servers managed by third parties)

1
• Mobile devices
• Embedded devices

1.2 NUMBERS

There are three numeric types in Python:


• int
• float
• complex
Example:
x=1 # int
y = 2.8 # float
z = 1j # complex
Int
Int, or integer, is a whole number, positive or negative, without decimals, of unlimited
length.
Example:
x=1 # int
y = 2.8 # float
z = 1j # complex
output:
<class 'int'>
<class 'int'>
<class 'int'>
Float
Float, or "floating point number" is a number, positive or negative, containing one or
more decimals.
Example:
x = 1.10
y = 1.0
z = -35.59
print(type(x))

2
print(type(y))
print(type(z))
output:
<class 'float'>
<class 'float'>
<class 'float'>
print(type(z))

Complex
Complex numbers are written with a "j" as the imaginary part:
Example:
x = 3+5j
y = 5j
z = -5j
print(type(x))
print(type(y))
print(type(z))
output:
<class 'complex'>
<class 'complex'>
<class 'complex'>

1.3 STRINGS

Strings in python are surrounded by either single quotation marks, or double quotation
marks. You can display a string literal with the print() function.'hello' is the same as "hello".
Example:
print("Hello")
print('Hello')
output:
Hello
Hello

3
Assign String to a Variable
Assigning a string to a variable is done with the variable name followed by an equal
assign and the string.
Example:
a = "Hello"
print(a)
output:
Hello

Strings are Arrays


• Strings in Python are arrays of bytes representing unicode characters.
• However, Python does not have a character data type, a single character is simply a
• string with a length of 1.
• Square brackets can be used to access elements of the string.
Example:
a = "Hello, World!"
print(a[1])
output:
e

Looping Through a String


Since strings are arrays, we can loop through the characters in a string, with a for
loop.
Example:
Loop through the letters in the word "banana":
for x in "banana":
print(x)
output:
b
a
n
a

4
n
a

String Length
To get the length of a string, use the len() function.
Example:
The len() function returns the length of a string:
a = "Hello, World!"
print(len(a))
output:
13

Check String
To check if a certain phrase or character is present in a string, we can use the keyword
in.
Example:
Print only if "free" is present:
txt = "The best things in life are free!"
if "free" in txt:
print("Yes, 'free' is present.")
output:
Yes, 'free' is present.

Check if NOT
To check if a certain phrase or character is NOT present in a string, we can use the
keyword not in.
Example:
print only if "expensive" is NOT present:
txt = "The best things in life are free!"
if "expensive" not in txt:
print("No, 'expensive' is NOT present.")
output:

5
No, 'expensive' is NOT present

Python - Modify Strings


Python has a set of built-in methods that you can use on strings.
Upper Case
Example:
The upper() method returns the string in upper case:
a = "Hello, World!"
print(a.upper())
output:
HELLO, WORLD!

Lower Case
Example:
The lower() method returns the string in lower case:
a = "Hello, World!"
print(a.lower())
output:
hello, world!

Replace String
Example:
The replace() method replaces a string with another string
a = "Hello, World!"
print(a.replace("H", "J"))
output:
Jello, World!

Split String
The split() method returns a list where the text between the specified separator
becomes the list items. The split() method splits the string into substrings if it finds instances
of the separator.

6
Example:
a = "Hello, World!"
print(a.split(",")) # returns ['Hello', ' World!']
output:
['Hello', ' World!']

String Concatenation
To concatenate, or combine, two strings you can use the + operator.
Example:
Merge variable a with variable b into variable c:
a = "Hello"
b = "World"
c=a+b
print(c)
output:
HelloWorld

1.4 VARIABLES

Variables are containers for storing data values.


Creating Variables
• Python has no command for declaring a variable.
• A variable is created the moment you first assign a value to it.
Example:
x= 5
y = "John"
print(x)
print(y)
output:
5
John

7
Get the Type
You can get the data type of a variable with the type( ) function.
Example:
x=5
y = "John"
print(type(x))
print(type(y))
output:
<class 'int'>
<class 'str'>

Variable Names
A variable can have a short name (like x and y) or a more descriptive name (age,
carname, total_volume).

Rules for Python variables:


• A variable name must start with a letter or the underscore character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric characters and underscores (A-z, 0-
9, and _ )
• Variable names are case-sensitive (age, Age and AGE are three different variables)
• A variable name cannot be any of the Python keywords.

1.5 LISTS

• Lists are used to store multiple items in a single variable.


• Lists are one of 4 built-in data types in Python used to store collections of data, the
other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.
• Lists are created using square brackets
• List is a collection which is ordered and changeable. Allows duplicate members
List Items
List items are indexed, the first item has index [0] the second item has index [1] etc.

8
Example :
mylist = ["apple", "banana", "cherry"]
Changeable
The list is changeable, meaning that we can change, add, and remove items in a list
after it has been created.
Allow Duplicates
Since lists are indexed, lists can have items with the same value.
Example:
Lists allow duplicate values:
thislist = ["apple", "banana", "cherry", "apple", "cherry"]
print(thislist)
output:
['apple', 'banana', 'cherry', 'apple', 'cherry']

List Length
To determine how many items a list has, use the len( ) function:
Example:
Print the number of items in the list:
thislist = ["apple", "banana", "cherry"]
print(len(thislist))
output:
3

List Items - Data Types


List items can be of any data type.
Example:
list1 = ["abc", 34, True, 40, "male"]
print(list1)
output:
['abc', 34, True, 40, 'male']

9
The list( ) Constructor
It is also possible to use the list( ) constructor when creating a new list.
Example:
thislist = list(("apple", "banana", "cherry")) # note the
double round-brackets
print(thislist)
output:
['apple', 'banana', 'cherry']

Access Items
List items are indexed and you can access them by referring to the index number:
Example:
Print the second item of the list:
thislist = ["apple", "banana", "cherry"]
print(thislist[1])
output:
banana

Negative Indexing
Negative indexing means start from the end -1 refers to the last item, -2 refers to the
second last item etc.
Example:
Print the last item of the list:
thislist = ["apple", "banana", "cherry"]
print(thislist[-1])
output:
cherry

Range of Indexes
You can specify a range of indexes by specifying where to start and where to end the
range. When specifying a range, the return value will be a new list with the specified items.

10
Example:
Return the third, fourth, and fifth item
thislist = ["apple", "banana", "cherry", "orange", "kiwi",
"melon", "mango"]
print(thislist[2:5])
output:
['cherry', 'orange', 'kiwi']

Check if Item Exists


To determine if a specified item is present in a list use the in keyword.
Example:
Check if "apple" is present in the list:
thislist = ["apple", "banana", "cherry"]
if "apple" in thislist:
print("Yes, 'apple' is in the fruits list")
output:
Yes, 'apple' is in the fruits list

Change Item Value


To change the value of a specific item, refer to the index number.
Example:
Change the second item
thislist = ["apple", "banana", "cherry"]
thislist[1] = "blackcurrant"
print(thislist)
output:
['apple', 'blackcurrant', 'cherry']

Change a Range of Item Values


To change the value of items within a specific range, define a list with the new values,
and refer to the range of index numbers where you want to insert the new values.

11
Example:
thislist = ["apple", "banana", "cherry", "orange", "kiwi",
"mango"]
thislist[1:3] = ["blackcurrant", "watermelon"]
print(thislist)
output:
['apple', 'blackcurrant', 'watermelon', 'orange', 'kiwi',
'mango']

Insert Items
• To insert a new list item, without replacing any of the existing values, we can use the
insert( ) method.
• The insert( ) method inserts an item at the specified index.
Example:
Insert "watermelon" as the third item:
thislist = ["apple", "banana", "cherry"]
thislist.insert(2, "watermelon")
print(thislist)
output:
['apple', 'banana', 'watermelon', 'cherry']

Append Items
To add an item to the end of the list, use the append( ) method.
Example:
thislist = ["apple", "banana", "cherry"]
thislist.append("orange")
print(thislist)
output:
['apple', 'banana', 'cherry', 'orange']

Remove Specified Item


The remove( ) method removes the specified item.

12
Example:
Remove "banana":
thislist = ["apple", "banana", "cherry"]
thislist.remove("banana")
print(thislist)
output:
['apple', 'cherry']

Clear the List


• The clear() method empties the list.
• The list still remains, but it has no content.
Example:
thislist = ["apple", "banana", "cherry"]
thislist.clear()
print(thislist)
output:
[]

Loop Through a List


You can loop through the list items by using a for loop.
Example:
thislist = ["apple", "banana", "cherry"]
for x in thislist:
print(x)
output:
apple
banana
cherry

List Comprehension
List comprehension offers a shorter syntax when you want to create a new list based
on the values of an existing list.

13
Example:
Based on a list of fruits, you want a new list, containing only the fruits with the letter
"a" in the name.
Without list comprehension you will have to write a for statement with a conditional
test inside.
Example:
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = []
for x in fruits:
if "a" in x:
newlist.append(x)
print(newlist)
output:
['apple', 'banana', 'mango']

Join Two Lists


• There are several ways to join, or concatenate, two or more lists in Python.
• One of the easiest ways are by using the + operator.
Example:
Join two list:
list1 = ["a", "b", "c"]
list2 = [1, 2, 3]
list3 = list1 + list2
print(list3)
output
['a', 'b', 'c', 1, 2, 3]

1.6 TUPLES

• Tuples are used to store multiple items in a single variable.


• A tuple is a collection which is ordered and unchangeable.

14
• Tuples are written with round brackets.
Example:
mytuple = ("apple", "banana", "cherry")

Create a Tuple
Example:
thistuple = ("apple", "banana", "cherry")
print(thistuple)
output:
('apple', 'banana', 'cherry')

Ordered
When we say that tuples are ordered, it means that the items have a defined order, and
that order will not change.
Unchangeable
Tuples are unchangeable, meaning that we cannot change, add or remove items after
the tuple has been created.
Allow Duplicates
Since tuples are indexed, they can have items with the same value.
Example:
thistuple = ("apple", "banana", "cherry", "apple", "cherry")
print(thistuple)
output:
('apple', 'banana', 'cherry', 'apple', 'cherry')

Tuple Length
To determine how many items a tuple has, use the len() function:
Example:
thistuple = ("apple", "banana", "cherry")
print(len(thistuple))
output:
3

15
Create Tuple With One Item
To create a tuple with only one item, you have to add a comma after the item,
otherwise Python will not recognize it as a tuple.
Example:
thistuple = ("apple",)
print(type(thistuple))
#NOT a tuple
thistuple = ("apple")
print(type(thistuple))
output:
<class 'tuple'>
<class 'str'>

Tuple Items - Data Types


Tuple items can be of any data type.
Example:
tuple1 = ("apple", "banana", "cherry")
tuple2 = (1, 5, 7, 9, 3)
tuple3 = (True, False, False)
output:
('apple', 'banana', 'cherry')
(1, 5, 7, 9, 3)
(True, False, False)

The tuple( ) Constructor


It is also possible to use the tuple( ) constructor to make a tuple.
Example:
thistuple = tuple(("apple", "banana", "cherry")) # note the
double round-brackets
print(thistuple)
output:
('apple', 'banana', 'cherry')

16
Access Tuple Items
You can access tuple items by referring to the index number, inside square brackets:
Example:
thistuple = ("apple", "banana", "cherry")
print(thistuple[1])
output:
banana

Negative Indexing
Negative indexing means start from the end -1 refers to the last item, -2 refers to the
second last item etc.
Example:
thistuple = ("apple", "banana", "cherry")
print(thistuple[-1])
output:
cherry

Change Tuple Values


• Once a tuple is created, you cannot change its values. Tuples are unchangeable, or
immutable as it also is called.
• But there is a workaround. You can convert the tuple into a list, change the list, and
convert the list back into a tuple.
Example:
x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x)
output:
("apple", "kiwi", "cherry")
Remove Items

17
Tuples are unchangeable, so you cannot remove items from it, but you can use the
same workaround as we used for changing and adding tuple items.
Example:
thistuple = ("apple", "banana", "cherry")
y = list(thistuple)
y.remove("apple")
thistuple = tuple(y)
output
('banana', 'cherry')

Loop Through a Tuple


You can loop through the tuple items by using a for loop.
Example:
thistuple = ("apple", "banana", "cherry")
for x in thistuple:
print(x)
output:
apple
banana
cherry

Using a While Loop


• You can loop through the tuple items by using a while loop.
• Use the len( ) function to determine the length of the tuple, then start at 0 and loop
your way through the tuple items by referring to their indexes.
• Remember to increase the index by 1 after each iteration.

Example:
thistuple = ("apple", "banana", "cherry")
i=0
while i < len(thistuple):
print(thistuple[i])

18
i=i+1
output:
apple
banana
cherry

Join Two Tuples


To join two or more tuples you can use the + operator .
Example:
tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)
tuple3 = tuple1 + tuple2
print(tuple3)
output:
('a', 'b', 'c', 1, 2, 3)

1.7 DICTIONARIES

• Dictionaries are used to store data values in key:value pairs.


• A dictionary is a collection which is ordered changeable and do not allow duplicates.
• Dictionaries are written with curly brackets, and have keys and values
Example:
thisdict =
{
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict)
output:
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
Dictionary Items
• Dictionary items are ordered, changeable, and does not allow duplicates.

19
• Dictionary items are presented in key:value pairs, and can be referred to by using the
key name.
Example:
thisdict =
{
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
print(thisdict["brand"])
output:
Ford

Duplicates Not Allowed


Dictionaries cannot have two items with the same key.
Example:
thisdict =
{
"brand": "Ford",
"model": "Mustang",
"year": 1964,
"year": 2020
}
print(thisdict)
output:
{'brand': 'Ford', 'model': 'Mustang', 'year': 2020}

Dictionary Length
To determine how many items a dictionary has, use the len( ) function.
Example:

20
thisdict =
{
"brand": "Ford",
"model": "Mustang",
"year": 1964,
"year": 2020
}
print(len(thisdict))
output:
3

Dictionary Items - Data Types


The values in dictionary items can be of any data type.
Example:
thisdict =
{
"brand": "Ford",
"electric": False,
"year": 1964,
"colors": ["red", "white", "blue"]
}
output:
{'brand': 'Ford', 'electric': False, 'year': 1964, 'colors': ['red',
'white', 'blue']}

The dict( ) Constructor


It is also possible to use the dict( ) constructor to make a dictionary.
Example:
thisdict = dict(name = "John", age = 36, country =
"Norway")
print(thisdict)

21
output:
{'name': 'John', 'age': 36, 'country': 'Norway'}

Accessing Items
You can access the items of a dictionary by referring to its key name, inside square
brackets.
Example:
thisdict =
{
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = thisdict["model"]
output:
Mustang

Get Keys
The keys( ) method will return a list of all the keys in the dictionary.
Example:
car =
{
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.keys()
print(x) #before the change
car["color"] = "white"
print(x) #after the change
output:

22
dict_keys(['brand', 'model', 'year'])
dict_keys(['brand', 'model', 'year', 'color'])

Get Values
The values( ) method will return a list of all the values in the dictionary.
Example:
car =
{
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x = car.values()
print(x) #before the change
car["year"] = 2020
print(x) #after the change
output:
dict_values(['Ford', 'Mustang', 1964])
dict_values(['Ford', 'Mustang', 2020])

Check if Key Exists


To determine if a specified key is present in a dictionary use the in keyword.
Example:
thisdict =
{
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
if "model" in thisdict:
print("Yes, 'model' is one of the keys in the this dict

23
dictionary")
output:
Yes, 'model' is one of the keys in the this dict dictionary

Update Dictionary
• The update( ) method will update the dictionary with the items from the given
argument.
• The argument must be a dictionary, or an iterable object with key:value pairs.

Example:
thisdict =
{
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.update({"year": 2020})
output:
{'brand': 'Ford', 'model': 'Mustang', 'year': 2020}

Adding Items
Adding an item to the dictionary is done by using a new index key and assigning a
value to it.
Example:
thisdict =
{
"brand": "Ford",
"model": "Mustang",
"year": 1964
}

24
thisdict["color"] = "red"
output:
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'red'}

Removing Items
Example:
thisdict =
{
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
thisdict.pop("model")
print(thisdict)
output:
{'brand': 'Ford', 'year': 1964}

The del keyword removes the item with the specified key name.
Example:
thisdict =
{
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
del thisdict["model"]
print(thisdict)
output:
{'brand': 'Ford', 'year': 1964}

Loop Through a Dictionary


• You can loop through a dictionary by using a for loop.
25
• When looping through a dictionary, the return value are the keys of the dictionary, but
there are methods to return the values as well.
Example:
thisdict =
{
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
for x in thisdict:
print(x)
output:
brand
model
year

Copy a Dictionary
• You cannot copy a dictionary simply by typing dict2 = dict1, because: dict2 will only
be a reference to dict1, and changes made in dict1 will automatically also be made in
dict2.
• There are ways to make a copy, one way is to use the built-in Dictionary method
copy( ).
Example:
thisdict =
{
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
mydict = thisdict.copy()
print(mydict)

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

Another way to make a copy is to use the built-in function dict( ).


Example:
thisdict =
{
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
mydict = dict(thisdict)
print(mydict)
output:
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}

1.8 SETS

• Sets are used to store multiple items in a single variable.


• A set is a collection which is unordered, unchangeable*, and unindexed.
• Sets are written with curly brackets.
Example:
thisset = {"apple", "banana", "cherry"}
print(thisset)
output:
{'banana', 'apple', 'cherry'}

Set Items
Set items are unordered, unchangeable, and do not allow duplicate values.

Duplicates Not Allowed


Sets cannot have two items with the same value.

27
Example:
thisset = {"apple", "banana", "cherry", "apple"}
print(thisset)
output:
{'banana', 'cherry', 'apple'}

Get the Length of a Set


To determine how many items a set has, use the len( ) function.
Example:
thisset = {"apple", "banana", "cherry"}
print(len(thisset))
output:
3

Set Items - Data Types


Set items can be of any data type.
Example:
set1 = {"apple", "banana", "cherry"}
set2 = {1, 5, 7, 9, 3}
set3 = {True, False, False}
output:
{'cherry', 'apple', 'banana'}
{1, 3, 5, 7, 9}
{False, True}

The set( ) Constructor


It is also possible to use the set( ) constructor to make a set.
Example:
thisset = set(("apple", "banana", "cherry")) # note the double
round-brackets
print(thisset)

28
output:{'apple', 'banana', 'cherry'}
{'banana', 'apple', 'cherry'}

Access Items
• You cannot access items in a set by referring to an index or a key.
• But you can loop through the set items using a for loop, or ask if a specified value is
present in a set, by using the in keyword
Example:
thisset = {"apple", "banana", "cherry"}
print("banana" in thisset)
output:
True

Add Items
• Once a set is created, you cannot change its items, but you can add new items.
• To add one item to a set use the add( ) method.
Example:
thisset = {"apple", "banana", "cherry"}
thisset.add("orange")
print(thisset)
output:
{'cherry', 'orange', 'apple', 'banana'}

Add Sets
To add items from another set into the current set, use the update( ) method.
Example:
thisset = {"apple", "banana", "cherry"}
tropical = {"pineapple", "mango", "papaya"}
thisset.update(tropical)
print(thisset)
output:

29
{'apple', 'mango', 'cherry', 'pineapple', 'banana', 'papaya'}

Remove Item
To remove an item in a set, use the remove( ), or the discard( ) method.
Example:
thisset = {"apple", "banana", "cherry"}
thisset.remove("banana")
print(thisset)
output:
{'cherry', 'apple'}

Loop Items
You can loop through the set items by using a for loop.
Example:
thisset = {"apple", "banana", "cherry"}
for x in thisset:
print(x)
output:
cherry
banana
apple

Join Two Sets


• There are several ways to join two or more sets in Python.
• You can use the union() method that returns a new set containing all items from both
sets, or the update() method that inserts all the items from one set into another.
Example:
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set3 = set1.union(set2)
print(set3)
output:

30
{1, 3, 2, 'c', 'b', 'a'}
Example:
set1 = {"a", "b" , "c"}
set2 = {1, 2, 3}
set1.update(set2)
print(set1)
output:
{'b', 3, 1, 'c', 'a', 2}

1.9 COMPARISION

Python has six comparison operators:


• less than ( < ),
• less than or equal to ( <= ),
• greater than ( > ),
• greater than or equal to ( >= ),
• equal to ( == ), and not equal to ( != ).
Less than operator (<)
The Less Than operator (<) compares two values and returns True if the value on the
left is less than the value on the right. Otherwise, it returns False.
The following example uses the Less Than (<) operator to compare two numbers:
>>> 10 < 20
True
>>> 30 < 20
False

Less than or equal to operator (<=)


The less than or equal to operator compares two values and returns True if the left
value is less than or equal to the right value. Otherwise, it returns False.
And this example shows how to use the less than or equal to operator to compare the
values of two variables.
>>> x = 10

31
>>> y = 20
>>> x <= y
True
>>> y <= x
False

Greater than operator (>)


The greater than operator (>) compares two values and returns True if the left value is
greater than the right value. Otherwise, it returns False.
And the following example uses the greater than operator (>) to compare two strings.
>>> 'apple' > 'orange'
False
>>> 'orange' > 'apple'
True

Greater Than or Equal To operator (>=)


The greater than or equal to operator (>=) compares two values and returns True if the
left value is greater than or equal to the right value. Otherwise, it returns False.
And the following example uses the greater than or equal to operator to compare two
strings:
>>> 'apple' >= 'apple'
True
>>> 'apple' >= 'orange'
False
>>> 'orange' >= 'apple'
True

Equal To operator (==)


The equal to operator (==) compares two values and returns True if the left value is
equal to the right value. Otherwise, it returns False.
UNIT – II

32
2.1 CODE STRUCTURES

Programming Structure of python


• Attributes and Imports: The structure Python Program consists of three files such as
: a.py,b.py and c.py.
• Functions: For example b.py defines a function called spam.
• Statements: Python import statement gives file a.py access to file b.py.
• Modules.
• Standard library files.

2.2 IF, ELIF, AND ELSE

An else statement can be combined with an if statement. An else statement contains


the block of code that executes if the conditional expression in the if statement resolves to 0
or a FALSE value.
The else statement is an optional statement and there could be at most only one else
statement following if.
Syntax:
if expression:
statement(s)
else:
statement(s)

Example:

33
var1 = 100
if var1:
print "1 - Got a true expression value"
print var1
else:
print "1 - Got a false expression value"
print var1
var2 = 0
if var2:
print "2 - Got a true expression value"
print var2
else:
print "2 - Got a false expression value"
print var2
print "Good bye!"
output:
1 - Got a true expression value
100
2 - Got a false expression value
0
Good bye!

The elif Statement


The elif statement allows you to check multiple expressions for TRUE and execute a
block of code as soon as one of the conditions evaluates to TRUE.
Similar to the else, the elif statement is optional. However, unlike else, for which
there can be at most one statement, there can be an arbitrary number of elif statements
following an if.

Syntax:

34
if expression1:
statement(s)
elif expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)

Example:
var = 100
if var == 200:
print "1 - Got a true expression value"
print var
elif var == 150:
print "2 - Got a true expression value"
print var
elif var == 100:
print "3 - Got a true expression value"
print var
else:
print "4 - Got a false expression value"
print var
print "Good bye!"
output:
3 - Got a true expression value
100
Good bye!

Else

35
The else keyword catches anything which isn't caught by the preceding conditions.
Example:
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
output:
a is greater than b

2.3 REPEAT WITH WHILE

Python while loop is used to run a block code until a certain condition is met.
• A while loop evaluates the condition
• If the condition evaluates to True, the code inside the while loop is executed.
• condition is evaluated again.
• This process continues until the condition is False.
• When condition evaluates to False, the loop stops.
The syntax of while loop is:
while condition:
# body of while loop

Example:
# program to display numbers from 1 to 5
# initialize the variable
i=1
n=5
# while loop from i = 1 to 5

36
while i <= n:
print(i)
i=i+1
output
1
2
3
4
5

2.4 ITERATE WITH FOR

• A for loop is used for iterating over a sequence (that is either a list, a tuple, a
dictionary, a set, or a string).
• This is less like the for keyword in other programming languages, and works more
like an iterator method as found in other object-orientated programming languages.
• With the for loop we can execute a set of statements, once for each item in a list,
tuple, set etc.
Example:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
output:
apple
banana
cherry

Looping Through a String


Even strings are iterable objects, they contain a sequence of characters.

Example:

37
for x in "banana":
print(x)
output
b
a
n
a
n
a

break Statement
With the break statement we can stop the loop before it has looped through all the
items.
Example:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break
output:
apple
banana

Continue Statement
With the continue statement we can stop the current iteration of the loop, and continue
with the next:
Example:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue

38
print(x)
output:
apple
cherry

Range( ) Function
• To loop through a set of code a specified number of times, we can use the range( )
function,
• The range( ) function returns a sequence of numbers, starting from 0 by default, and
increments by 1 (by default), and ends at a specified number.
Example:
for x in range(6):
print(x)
output:
0
1
2
3
4
5

Else in For Loop


The else keyword in a for loop specifies a block of code to be executed when the loop
is finished.
Example:
for x in range(6):
print(x)
else:
print("Finally finished!")
output
0

39
1
2
3
4
5
Finally finished!

2.5 COMPREHENSIONS

Comprehensions in Python are used to generate new sequences based on existing


sequences. In Python, we have comprehensions for lists, dictionaries, sets, and generators.
The type of the existing (original) sequence and the new sequence need not be the same.
Four types of Comprehensions
• List Comprehensions
• Dictionary Comprehensions
• Set Comprehensions
• Generator Comprehension
List Comprehension
List comprehension offers a shorter syntax when you want to create a new list based
on the values of an existing list.
Example:
Based on a list of fruits, you want a new list, containing only the fruits with the letter
"a" in the name.
Without list comprehension you will have to write a for statement with a conditional
test inside.
Syntax:
newlist = [expression for item in iterable if condition ==
True]
Example:
fruits = ["apple", "banana", "cherry", "kiwi", "mango"]
newlist = [x for x in fruits if "a" in x]

40
print(newlist)
output:
['apple', 'banana', 'mango']

Dictionary comprehension
Dictionary comprehension is a method for transforming one dictionary into another
dictionary. During this transformation, items within the original dictionary can be
conditionally included in the new dictionary, and each item can be transformed as needed.
Example:
square_dict = dict()
for num in range(1, 11):
square_dict[num] = num*num
print(square_dict)
output:
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100}

Using Dictionary Comprehension


From the above example, we can see that dictionary comprehension should be written
in a specific pattern.
The minimal syntax for dictionary comprehension is:
dictionary = {key: value for vars in iterable}

Let's compare this syntax with dictionary comprehension from the above example.

How to use Dictionary Comprehension


Example:
#item price in dollars
old_price = {'milk': 1.02, 'coffee': 2.5, 'bread': 2.5}

41
dollar_to_pound = 0.76
new_price = {item: value*dollar_to_pound for (item, value)
in old_price.items()}
print(new_price)
output:
{'milk': 0.7752, 'coffee': 1.9, 'bread': 1.9}

If Conditional Dictionary Comprehension


Example:
original_dict = {'jack': 38, 'michael': 48, 'guido': 57, 'john': 33}
even_dict = {k: v for (k, v) in original_dict.items() if v % 2
== 0}
print(even_dict)
output:
{'jack': 38, 'michael': 48}
if-else Conditional Dictionary Comprehension
Example:
original_dict = {'jack': 38, 'michael': 48, 'guido': 57, 'john': 33}
new_dict_1 = {k: ('old' if v > 40 else 'young')
for (k, v) in original_dict.items()}
print(new_dict_1)
output:
{'jack': 'young', 'michael': 'old', 'guido': 'old', 'john': 'young'}

Nested Dictionary Comprehension


We can add dictionary comprehensions to dictionary comprehensions themselves to
create nested dictionaries.
Example:
dictionary =
{
k1: {k2: k1 * k2 for k2 in range(1, 6)} for k1 in range(2, 5)

42
}
print(dictionary)
output:
{2: {1: 2, 2: 4, 3: 6, 4: 8, 5: 10}, 3: {1: 3, 2: 6, 3: 9, 4: 12, 5: 15}, 4:
{1: 4, 2: 8, 3: 12, 4: 16, 5: 20}}

set comprehension
A set comprehension is similar to a list comprehension but returns a set instead of a
list. The syntax is slightly different in the sense that we use curly brackets instead of square
brackets to create a set. The list includes a lot of duplicates, and there are names with only a
single letter.
The syntax for set comprehension :
newSet= { expression for element in iterable }

Description of the syntax:


• The iterable may be any iterable object or data structure in Python from which we
have to use the elements to create the new set.
• The element denotes the element from the iterable that has to be included in the set.
• The expression can be any mathematical expression derived from the element.
• newSet is the name of the new set which has to be created from the elements of the
iterable.
Example:
myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
newSet = {element*3 for element in myList}
print("The existing list is:")
print(myList)
print("The Newly Created set is:")
print(newSet)
output:
The existing list is:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The Newly Created set is:

43
{3, 6, 9, 12, 15, 18, 21, 24, 27, 30}

Generator Comprehension
• Generator Comprehension is a way to initialize a generator to access elements from a
container object or a sequence of data.
• Generally, We create a generator object by implementing a generator function using
the yield statement. For example, suppose you want to create a generator that gives
squares of elements of a given list as output.
We can create such a generator using the yield statement as follows:
Example:
def num_generator():
for i in range(1, 11):
yield i
gen = num_generator()
print("Values obtained from generator function are:")
for element in gen:
print(element)
output:
Values obtained from generator function are:
1
2
3
4
5
6
7
8
9
10

Syntax for generator comprehension

44
The syntax for generator comprehension is almost identical to list comprehension.
The syntax for set comprehension is:
generator= (expression for element in iterable if condition)

• The iterable can be any iterable object such as list, set, tuple, or dictionary from which
we have to create a new generator to access its elements.
• The element is the element of the iterable object for which we are creating the
generator.
• The expression contains a value or any mathematical expression derived from the
element.
• The condition is the conditional expression required to exclude or include an element
in the generator. The conditional statement is optional and “if condition” can be
omitted if you have to access all the elements of the iterable.
• generator is the name of a newly created generator using generator comprehension in
Python.
Benefits of generator comprehension
• Generator comprehension makes us able to implement the same functionality using
fewer lines of code.
• Generator comprehension does not initialize any object unlike list comprehension or
set comprehension. Thus, you can use generator comprehension instead of list
comprehension or set comprehension to reduce the memory requirement of the
program.
• Generator comprehension also makes the code more readable that helps in debugging
and maintenance of the source code.

2.6 FUNCTIONS

• A function is a block of code which only runs when it is called.


• You can pass data, known as parameters, into a function.
• A function can return data as a result.
Creating a Function
In Python a function is defined using the def keyword.
Example:

45
def my_function():
print("Hello from a function")

Calling a Function
To call a function, use the function name followed by parenthesis.
Example:
def my_function():
print("Hello from a function")
my_function()
output:
Hello from a function

Arguments
• Information can be passed into functions as arguments.
• Arguments are specified after the function name, inside the parentheses. You can add
as many arguments as you want, just separate them with a comma.
Example:
def my_function(fname):
print(fname + " Refsnes")
my_function("Emil")
my_function("Tobias")
my_function("Linus")
output:
Emil Refsnes
Tobias Refsnes
Linus Refsnes

Number of Arguments
By default, a function must be called with the correct number of arguments. Meaning
that if your function expects 2 arguments, you have to call the function with 2 arguments, not
more, and not less.
Example:

46
def my_function(fname, lname):
print(fname + " " + lname)
my_function("Emil", "Refsnes")
output:
Emil Refsnes

Arbitrary Arguments, *args


If you do not know how many arguments that will be passed into your function, add a
* before the parameter name in the function definition.
This way the function will receive a tuple of arguments, and can access the items
accordingly.
Example:
def my_function(*kids):
print("The youngest child is " + kids[2])
my_function("Emil", "Tobias", "Linus")
output:
The youngest child is Linus

Keyword Arguments
You can also send arguments with the key = value.
Example:
def my_function(child3, child2, child1):
print("The youngest child is " + child3)
my_function(child1 = "Emil", child2 = "Tobias", child3 =
"Linus")
output:
The youngest child is Linus

Arbitrary Keyword Arguments, **kwargs


If you do not know how many keyword arguments that will be passed into your
function, add two asterisk: ** before the parameter name in the function definition.

47
This way the function will receive a dictionary of arguments, and can access the items
accordingly.
Example:
If the number of keyword arguments is unknown, add a
double ** before the parameter name:
def my_function(**kid):
print("His last name is " + kid["lname"])
my_function(fname = "Tobias", lname = "Refsnes")
output:
His last name is Refsnes

Default Parameter Value


If we call the function without argument, it uses the default value.
Example:
def my_function(country = "Norway"):
print("I am from " + country)
my_function("Sweden")
my_function("India")
my_function()
my_function("Brazil")
output:
I am from Sweden
I am from India
I am from Norway
I am from Brazil

Passing a List as an Argument


You can send any data types of argument to a function (string, number, list, dictionary
etc.), and it will be treated as the same data type inside the function.
Example:
def my_function(food):

48
for x in food:
print(x)
fruits = ["apple", "banana", "cherry"]
my_function(fruits)
output:
apple
banana
cherry

Return Values
To let a function return a value, use the return statement.
Example:
def my_function(x):
return 5 * x
print(my_function(3))
print(my_function(5))
print(my_function(9))
output:
15
25
45

2.7 GENERATORS

In Python, a generator is a function that returns an iterator that produces a sequence of


values when iterated over. Generators are useful when we want to produce a large sequence
of values, but we don't want to store all of them in memory at once.
Create Python Generator
In Python, similar to defining a normal function, we can define a generator function
using the def keyword, but instead of the return statement we use the yield statement. Here,
the yield keyword is used to produce a value from the generator.

49
def generator_name(arg):
# statements
yield something

When the generator function is called, it does not execute the function body
immediately. Instead, it returns a generator object that can be iterated over to produce the
values.
Example:
def my_generator(n):
# initialize counter
value = 0
# loop until counter is less than n
while value < n:
# produce the current value of the counter
yield value
# increment the counter
value += 1
#iterate over the generator object produced by
my_generator
for value in my_generator(3):
# print each value produced by generator
print(value)
output:
0
1
2

Python Generator Expression


• In Python, a generator expression is a concise way to create a generator object.
• It is similar to a list comprehension, but instead of creating a list, it creates
a generator object that can be iterated over to produce the values in the generator.
Generator Expression Syntax

50
A generator expression has the following syntax
(expression for item in iterable)

Here, expression is a value that will be returned for each item in the iterable.
The generator expression creates a generator object that produces the values of
expression for each item in the iterable, one at a time, when iterated over.
Example:
# create the generator object
squares_generator = (i * i for i in range(5))
# iterate over the generator and print the values
for i in squares_generator:
print(i)
output:
0
1
4
9
16

Use of Python Generators


There are several reasons that make generators a powerful implementation.
• Easy to Implement
• Memory Efficient
• Represent Infinite Stream
• Pipelining Generators

2.8 DECORATORS

• A decorator is a design pattern in Python that allows a user to add new functionality to
an existing object without modifying its structure. Decorators are usually called
before the definition of a function you want to decorate.

51
• In Python, a decorator is a design pattern that allows you to modify the functionality
of a function by wrapping it in another function.
• The outer function is called the decorator, which takes the original function as an
argument and returns a modified version of it.
• A Python decorator is a function that takes in a function and returns it by adding some
functionality.
• In fact, any object which implements the special _call( )_ method is termed callable.
So, in the most basic sense, a decorator is a callable that returns a callable.
• Basically, a decorator takes in a function, adds some functionality and returns it.
Example:
def make_pretty(func):
def inner():
print("I got decorated")
func()
return inner
def ordinary():
print("I am ordinary")
output:
I am ordinary

We are calling the ordinary() function normally, so we get the output "I am ordinary".
Now, let's call it using the decorator function. def make_pretty(func):
Example:
# define the inner function
def inner():
# add some additional behavior to decorated function
print("I got decorated")
# call original function
func()
# return the inner function
return inner

52
# define ordinary function
def ordinary():
print("I am ordinary")
# decorate the ordinary function
decorated_func = make_pretty(ordinary)
# call the decorated function
decorated_func()
output:
I got decorated
I am ordinary

@ Symbol With Decorator


Instead of assigning the function call to a variable, Python provides a much more
elegant way to achieve this functionality using the @ symbol.
Decorating Functions with Parameters
Example :
def smart_divide(func):
def inner(a, b):
print("I am going to divide", a, "and", b)
if b == 0:
print("Whoops! cannot divide")
return
return func(a, b)
return inner
@smart_divide
def divide(a, b):
print(a/b)
divide(2,5)
divide(2,0)
output:
I am going to divide 2 and 5

53
0.4
I am going to divide 2 and 0
Whoops! cannot divide

Chaining Decorators in Python


• Multiple decorators can be chained in Python.
• To chain decorators in Python, we can apply multiple decorators to a single function
by placing them one after the other, with the most inner decorator being applied first.
Example:
def star(func):
def inner(*args, **kwargs):
print("*" * 15)
func(*args, **kwargs)
print("*" * 15)
return inner
def percent(func):
def inner(*args, **kwargs):
print("%" * 15)
func(*args, **kwargs)
print("%" * 15)
return inner
@star
@percent
def printer(msg):
print(msg)
printer("Hello")
output:
***************
%%%%%%%%%%%%%%%
Hello
%%%%%%%%%%%%%%%
***************

54
2.9 NAMESPACES AND SCOPE

A namespace is just a mapping from names to objects. A scope is a textual region of a


Python program where a namespace is directly accessible. Basically, each python object has a
scope, beyond which it does not exist (and hence cannot be accessed).

A variable is only available from inside the region it is created. This is called scope.
Local Scope
A variable created inside a function belongs to the local scope of that function, and
can only be used inside that function.
Example:
def myfunc():
x = 300
print(x)
myfunc()
output:
300

Function Inside Function


As explained in the example above, the variable x is not available outside the
function, but it is available for any function inside the function.
Example:
def myfunc():
x = 300
def myinnerfunc():
print(x)

55
myinnerfunc()
myfunc()
output:
300

2.10 HANDLE ERRORS WITH TRY AND EXCEPT

• The try block lets you test a block of code for errors.
• The except block lets you handle the error.
• The else block lets you execute code when there is no error.
• The finally block lets you execute code, regardless of the result of the try- and except
blocks.
Exception Handling
When an error occurs, or exception as we call it, Python will normally stop and
generate an error message
These exceptions can be handled using the try statement
Example:
try:
print(x)
except:
print("An exception occurred")
output:
An exception occurred

Many Exceptions
You can define as many exception blocks as you want, e.g. if you want to execute a
special block of code for a special kind of error:
Example:
try:
print(x)
except NameError:

56
print("Variable x is not defined")
except:
print("Something else went wrong")
output:
Variable x is not defined

Else
You can use the else keyword to define a block of code to be executed if no errors
were raised
Example:
try:
print("Hello")
except:
print("Something went wrong")
else:
print("Nothing went wrong")
output:
Hello
Nothing went wrong

Finally
The finally block, if specified, will be executed regardless if the try block raises an
error or not.
Example:
try:
print(x)
except:
print("Something went wrong")
finally:
print("The 'try except' is finished")
output:

57
Something went wrong
The 'try except' is finished

2.11 USER EXCEPTIONS

To create a User-defined Exception, we have to create a class that implements the


Exception class. We can raise(throw) these exceptions using the raise keyword. These
exceptions can be caught in the try-except block, just like common exceptions.
Defining Custom Exceptions
In Python, we can define custom exceptions by creating a new class that is derived
from the built-in Exception class.
Here's the syntax to define custom exceptions
class CustomError(Exception):
...
pass
try:
...
except CustomError:
...

Exceptions need to be derived from the Exception class, either directly or indirectly.
Although not mandatory, most of the exceptions are named as names that end in “Error”
similar to the naming of the standard exceptions in python.
Example:
# define Python user-defined exceptions
class InvalidAgeException(Exception):
"Raised when the input value is less than 18"
pass
# you need to guess this number
number = 18
try:
input_num = int(input("Enter a number: "))

58
if input_num < number:
raise InvalidAgeException
else:
print("Eligible to Vote")
except InvalidAgeException:
print("Exception occurred: Invalid Age")
output:
Enter a number: 35
Eligible to Vote

59
UNIT- III

3.1 MODULES, PACKAGES, AND PROGRAMS


3.1.1 STANDALONE PROGRAMS

A standalone executable is a program that runs without any other programs or


libraries. They can run independently, separate from the rest of the Python installation. They
can be helpful when a simplified user experience is prioritized above visibility and control
over an app's source code.
Python programs can be written as independent commands (like other basic Linux
commands). These progams do not require an interactive environemnt (like jupyter
notebook). In this tutorial, a simple application (vcorner) is used to illustrate the development
of a standalone program.

3.1.2 COMMAND-LINE ARGUMENTS

Python Command line arguments are input parameters passed to the script when
executing them. Almost all programming language provide support for command line
arguments. Then we also have command line options to set some specific options for the
program.
Here Python script name is script.py and rest of the three arguments - arg1 arg2
arg3 are command line arguments for the program.
Python Command Line Arguments provides a convenient way to accept some
information at the command line while running the program. The arguments that are given
after the name of the Python script are known as Command Line Arguments and they are
used to pass some information to the program.
Example:
$ python script.py arg1 arg2 arg3
.
There are following three Python modules which are helpful in parsing and managing the
command line arguments:
• sys module
• getopt module

60
• argparse module
sys module - System-specific parameters
The Python sys module provides access to any command-line arguments via the
sys.argv.
This serves two purposes −
• sys.argv is the list of command-line arguments.
• len(sys.argv) is the number of command-line arguments.
Example:
import sys
print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)

Now run above script as below. All the programs in this tutorial need to be run from
the command line, so we are unable to provide online compile & run option for these
programs.
$ python test.py arg1 arg2 arg3
output:
Number of arguments: 4 arguments.
Argument List: ['test.py', 'arg1', 'arg2', 'arg3']

Parsing Command-Line Arguments


Python provided a getopt module that helps you parse command-line options and
arguments. This module provides two functions and an exception to enable command line
argument parsing.
getopt.getopt method
This method parses command line options and parameter list.

Syntax:
getopt.getopt(args, options, [long_options])

61
3.1.3 MODULES AND THE IMPORT STATEMENT

Import in python is similar to #include header_file in C/C++. Python modules can


get access to code from another module by importing the file/function using import. The
import statement is the most common way of invoking the import machinery, but it is not the
only way.
The Python import statement imports code from one module into another program.
You can import all the code from a module by specifying the import keyword followed by the
module you want to import. import statements appear at the top of a Python file, beneath any
comments that may exist.
Create a Module
To create a module just save the code you want in a file with the file extension .py
Example:
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 mymodule
mymodule.greeting("Jonathan")
output:
Hello, Jonathan

Variables in Module
The module can contain functions, as already described, but also variables of all types
(arrays, dictionaries, objects etc):
Example:
person1 =
{
"name": "John",
"age": 36,

62
"country": "Norway"
}

Example:
import mymodule
a = mymodule.person1["age"]
print(a)
output:
36

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:
import mymodule as mx
a = mx.person1["age"]
print(a)
output:
36

Import From Module


You can choose to import only parts from a module, by using the from keyword.
Example:
def greeting(name):
print("Hello, " + name)
person1 =
{
"name": "John",
"age": 36,

63
"country": "Norway"
}

Import only the person1 dictionary from the module:


Example:
from mymodule import person1
print(person1["age"])
output:
36

3.1.4 THE PYTHON STANDARD LIBRARY

The Python Standard Library is a collection of script modules that may be used by a
Python program, making it unnecessary to rewrite frequently used commands and
streamlining the development process. By "calling/importing" them at the start of a script,
they can be used.
The Python Standard Library contains the exact syntax, semantics, and tokens of
Python. It contains built-in modules that provide access to basic system functionality like I/O
and some other core modules. Most of the Python Libraries are written in the C programming
language.
The Python standard library consists of more than 200 core modules. All these work
together to make Python a high-level programming language. Python Standard Library plays
a very important role. Without it, the programmers can’t have access to the functionalities of
Python. But other than this, there are several other libraries in Python that make a
programmer’s life easier.
Let’s have a look at some of the commonly used libraries:
TensorFlow: This library was developed by Google in collaboration with the Brain
Team. It is an open-source library used for high-level computations. It is also used in
machine learning and deep learning algorithms. It contains a large number of tensor
operations. Researchers also use this Python library to solve complex computations in
Mathematics and Physics.

64
Matplotlib: This library is responsible for plotting numerical data. And that’s why it
is used in data analysis. It is also an open-source library and plots high-defined figures like
pie charts, histograms, scatterplots, graphs, etc.
Pandas: Pandas are an important library for data scientists. It is an open-source
machine learning library that provides flexible high-level data structures and a variety of
analysis tools. It eases data analysis, data manipulation, and cleaning of data. Pandas support
operations like Sorting, Re-indexing, Iteration, Concatenation, Conversion of data,
Visualizations, Aggregations, etc.
Numpy: The name “Numpy” stands for “Numerical Python”. It is the commonly used
library. It is a popular machine learning library that supports large matrices and multi-
dimensional data. It consists of in-built mathematical functions for easy computations. Even
libraries like TensorFlow use Numpy internally to perform several operations on tensors.
Array Interface is one of the key features of this library.
SciPy: The name “SciPy” stands for “Scientific Python”. It is an open-source library
used for high-level scientific computations. This library is built over an extension of Numpy.
It works with Numpy to handle complex computations. While Numpy allows sorting and
indexing of array data, the numerical data code is stored in SciPy. It is also widely used by
application developers and engineers.
Scrapy: It is an open-source library that is used for extracting data from websites. It
provides very fast web crawling and high-level screen scraping. It can also be used for data
mining and automated testing of data.
Scikit-learn: It is a famous Python library to work with complex data. Scikit-learn is
an open-source library that supports machine learning. It supports variously supervised and
unsupervised algorithms like linear regression, classification, clustering, etc. This library
works in association with Numpy and SciPy.
PyGame: This library provides an easy interface to the Standard Directmedia Library
(SDL) platform-independent graphics, audio, and input libraries. It is used for developing
video games using computer graphics and audio libraries along with Python programming
language.
PyTorch: PyTorch is the largest machine learning library that optimizes tensor
computations. It has rich APIs to perform tensor computations with strong GPU acceleration.
It also helps to solve application issues related to neural networks.
PyBrain: The name “PyBrain” stands for Python Based Reinforcement Learning,
Artificial Intelligence, and Neural Networks library. It is an open-source library built for
65
beginners in the field of Machine Learning. It provides fast and easy-to-use algorithms for
machine learning tasks. It is so flexible and easily understandable and that’s why is really
helpful for developers that are new in research fields.

3.2 OBJECTS AND CLASSES

Python is an object oriented programming language. Almost everything in Python is


an object, with its properties and methods. A Class is like an object constructor, or a
"blueprint" for creating objects.
An Object is an instance of a Class. A class is like a blueprint while an instance is a
copy of the class with actual values. Python is an object-oriented programming language that
stresses objects i.e. it mainly emphasizes functions.
A class is a user-defined blueprint or prototype from which objects are created.
Classes provide a means of bundling data and functionality together. Creating a new class
creates a new type of object, allowing new instances of that type to be made.

3.2.1 DEFINE A CLASS WITH CLASS

• A class is a user-defined blueprint or a prototype, which we can use to create the


objects of a class.
• The class is defined by using the class keyword.
Defining a Class
A class in Python can be defined using the class keyword.
class <ClassName>:
<statement1>
<statement2>
.
.
<statementN>

66
3.2.2 INHERITANCE

• Inheritance allows us to define a class that inherits all the methods and properties
from another class.
• Parent class is the class being inherited from, also called base class.
• Child class is the class that inherits from another class, also called derived class.
Create a Parent Class
Any class can be a parent class, so the syntax is the same as creating any other class.
Example:
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
#Use the Person class to create an object, and then execute
the printname method:
x = Person("John", "Doe")
x.printname()
output:
John Doe

Create a Child Class


To create a class that inherits the functionality from another class, send the parent
class as a parameter when creating the child class.Create a class named Student, which will
inherit the properties and methods from the Person class.
Example:
class Student(Person):
pass

Use the Student class to create an object, and then execute the printname method.
Example:
class Person:

67
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
class Student(Person):
x = Student("Mike", "Olsen")
x.printname()
output:
Mike Olsen

Add the __init__() Function


• We want to add the __init__() function to the child class (instead of the pass
keyword).
• Add the __init__() function to the Student class.

class Student(Person):
def __init__(self, fname, lname):
#add properties etc.

To keep the inheritance of the parent's __init__() function, add a call to the parent's
__init__() function:
Example:
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
class Student(Person):
def __init__(self, fname, lname):

68
Person.__init__(self, fname, lname)
x = Student("Mike", "Olsen")
x.printname()
output:
Mike Olsen

Use the super() Function


Python also has a super() function that will make the child class inherit all the
methods and properties from its parent.
Example:
class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)
class Student(Person):
def __init__(self, fname, lname):
super().__init__(fname, lname)
x = Student("Mike", "Olsen")
x.printname()
output:
Mike Olsen

3.2.3 OVERRIDE A METHOD

Overriding a method in the same class is not allowed. So, you need to do that in the
child class by implementing the Inheritance concept. If you want to override the Parent Class
method, create a function in the Child with the same name and number of parameters. This is
called function overriding in Python.

69
The term "override" refers to a method in a subclass that replaces a method in a
superclass when both methods share the same name, parameters, signature, and return type
(or sub-type).
The object that calls a method will determine which version of the method is
executed. When a method is called from an object of a parent class, the method's parent class
version is executed; however, when a method is called from an object of a subclass, the child
class version is executed. In other words, the version of an overridden method depends on the
object being referenced, not the type of the reference variable.

Example:
# program to determine the method overriding
# Defining a parent class
class Parent():
# producer
def __init__(self):
self.value = "Inside Parent"
# showing a parents method
def show(self):
print(self.value)
# Defining a child class
class Child(Parent):
# Constructor
def __init__(self):
self.value = "Inside Child"

70
# showing the child's method
def show(self):
print(self.value)
# Chauffeur's code
obj1 = Parent()
obj2 = Child()
obj1.show()
obj2.show()
output:
Inside Parent
Inside Child

3.2.4 ADD A METHOD

Behaviors are called Instance Methods in Python. A method is a function that


belongs to a class. Just like functions, these methods can accept parameters, and they can
access the attributes of the object they belong to.
Example:
class Dog:
species = 'mammal'
def __init__(self, name, age):
self.name = name
self.age = age
def speak(self, sound):
print(f"{self.name} says {sound}")

Example:
philo = Dog("Philo", 5)
philo.speak("Gruff gruff!")

71
3.2.5 GET HELP FROM YOUR PARENT WITH SUPER

• The super( ) function is used to give access to methods and properties of a parent or
sibling class.
• The super( ) function returns an object that represents the parent class. The super()
function is used to give access to methods and properties of a parent or sibling class.
• The super( ) function returns an object that represents the parent class.
Syntax:
super( )

Example:
class Parent:
def __init__(self, txt):
self.message = txt
def printmessage(self):
print(self.message)
class Child(Parent):
def __init__(self, txt):
super().__init__(txt)
x = Child("Hello, and welcome!")
x.printmessage( )
output:
Hello, and welcome!

3.2.6 IN SELF DEFENSE

We'll use self in classes to represent the instance of an object. We can create multiple
of a class and each instance will have different values. And self helps us to get those property
values within the class instance.
Example:
class Laptop:

72
# init method
def __init__(self, company, model):
# self
self.company = company
self.model = model
# creating instances for the class Laptop
laptop_one = Laptop('Lenovo', 'ideapad320')
laptop_two = Laptop('Dell', 'inspiron 7000')
# printing the properties of the instances
print(f"Laptop One: {laptop_one.company}")
print(f"Laptop Two: {laptop_two.company}")
output:
Laptop One: Lenovo
Laptop Two: Dell

3.2.7 GET AND SET ATTRIBUTE VALUES WITH PROPERTIES

For the purpose of data encapsulation, most object oriented languages use getters and
setters method. This is because we want to hide the attributes of a object class from other
classes so that no accidental modification of the data happens by methods in other classes.
As the name suggests, getters are the methods which help access the private attributes
or get the value of the private attributes and setters are the methods which help change or set
the value of private attributes.
Using getters and setters
we will make a class, initialize is and then add a getter and setter method to each of
them. Then access the variables in these methods by instantiating the class and using these
getter and setter methods. So you can hide your logic inside the setter method.
Example:
class year_graduated:
def __init__(self, year=0):
self._year = year

73
# getter method
def get_year(self):
return self._year
# setter method
def set_year(self, a):
self._year = a
grad_obj = year_graduated()
# Before using setter
print(grad_obj.get_year())
# After using setter
grad_obj.set_year(2019)
print(grad_obj._year)
output:
0
2019

3.2.8 NAME MANGLING FOR PRIVACY

The name mangling process helps to access the class variables from outside the class.
The class variables can be accessed by adding _classname to it. The name mangling is closest
to private not exactly private. The above class variable is accessed by adding the _classname
to it.
A process in which any given identifier with one trailing underscore and two leading
underscores is textually replaced with the __ClassName__Identifier is known as Name
mangling. In __ClassName__Identifier name, ClassName is the name of current class where
identifier is present.
Example:
# A testing class for identifier
class Testing:
# Giving Name as an identifier
def __init__(self, name):

74
# Identifier initializing
self.__name = name
def PrintName(self):
print(self.__name)
t1 = Testing("JavaTpoint") # Calling variable name with the
class
t1.PrintName() # Printing name in the output
output:
JavaTpoint

3.2.9 METHOD TYPES

There are basically three types of methods in Python:


• Instance Method
• Class Method
• Static Method
Instance Methods
The purpose of instance methods is to set or get details about instances (objects), and
that is why they’re known as instance methods. They are the most common type of methods
used in a Python class.
Example:
# define the class
class My_class:
def __init__(self, a, b):
self.a = a
self.b = b
def instance_method(self):
return f"This is the instance method and it can access the
variables a = {self.a} and b = {self.b} with the help of self."
obj = My_class(2,4)
print(obj.instance_method())

75
output:
This is the instance method and it can access the variables a
= 2 and b = 4 with the help of self.

Class Methods
• The purpose of the class methods is to set or get the details (status) of the class.
• That is why they are known as class methods. They can’t access or modify specific
instance data. They are bound to the class instead of their objects.
Two important things about class methods:
• In order to define a class method, you have to specify that it is a class method with the
help of the @classmethod decorator
• Class methods also take one default parameter- cls, which points to the class. Again,
this not mandatory to name the default parameter “cls”.
Example:
class My_class:
@classmethod
def class_method(cls):
return "This is a class method."

Static Methods
Static methods cannot access the class data. In other words, they do not need to access
the class data. They are self-sufficient and can work on their own. Since they are not
attached to any class attribute, they cannot get or set the instance state or class state.
Example:
class My_class:
@staticmethod
def static_method():
return "This is a static method."

76
3.2.10 DUCK TYPING

Duck typing is a concept related to dynamic typing, where the type or the class of an
object is less important than the methods it defines. When you use duck typing, you do not
check types at all. Instead, you check for the presence of a given method or attribute.
Example:
Define the three classes
class Duck:
def quack(self):
print("I am a duck and I quack.")
class Goose:
def quack(self):
print("I am a goose and I quack.")
class Cat:
def meow(self):
print("I am a dog and I meow.")
# Define the method
def quack(animal):
animal.quack()

3.2.11 SPECIAL METHODS

A variety of instance methods that are reserved by Python, which affect an object’s
high level behavior and its interactions with operators. These are known as special methods.
__init__ is an example of a special method; recall that it controls the process of creating
instances of a class. Similarly, we will see that __add__ controls the behavior of an object
when it is operated on by the + symbol, for example.
In general, the names of special methods take the form of __<name>__, where the
two underscores preceed and succeed the name. Accordingly, special methods can also be
referred to as “dunder” (double-underscore) methods. Learning to leverage special methods
will enable us to design elegant and powerful classes of objects.

77
Example:
# Demonstrating (mis)use of special methods
class SillyClass:
def __getitem__(self, key):
"" Determines behavior of `self[key]` """
return [True, False, True, False]
def __pow__(self, other):
""" Determines behavior of `self ** other` """
return "Python Like You Mean It"
output:
silly = SillyClass()
silly[None]
[True, False, True, False]
silly ** 2
'Python Like You Mean It'

3.2.12 COMPOSITION

Composition is a concept that models a has a relationship. It enables creating complex


types by combining objects of other types.
This means that a class Composite can contain an object of another class Component .
This relationship means that a Composite has a Component.
Composition is represented through a line with a diamond at the composite class
pointing to the component class. The composite side can express the cardinality of the
relationship. The cardinality indicates the number or valid range of Component instances the
Composite class will contain.

78
UNIT- IV

4.1 DATA TYPES AND WEB


4.1.1 TEXT STRINGS

Strings in Python are arrays of bytes representing unicode characters. However,


Python does not have a character data type, a single character is simply a string with a length
of 1. Square brackets can be used to access elements of the string.
To create a string, put the sequence of characters inside either single quotes, double
quotes, or triple quotes and then assign it to a variable. You can look into how variables work
in Python in the Python variables tutorial. For example, you can assign a character 'a' to a
variable single_quote_character .
Example:
#Using single quotes
str1 = “Hello Python”
print(str1)
#Using double quotes
str2 = “Hello Python”
print(str2)
#Using triple quotes
str3 = '''''Triple quotes are generally used for
represent the multiline or
docstring'''
print(str3)
output:
Hello Python
Hello Python
Triple quotes are generally used for
represent the multiline or
docstring

79
4.1.2 BINARY DATA

In Python, the boolean data type is the binary variable and defined as True or False.
Additionally, the bool() function converts the value of an object to a boolean value. This
function returns True for all values except the following values. Empty objects (list, tuple,
string, dictionary)
The byte and bytearrays are used to manipulate binary data in python. These bytes
and bytearrys are supported by buffer protocol, named memoryview. The memoryview can
access the memory of other binary object without copying the actual data.
The byte literals can be formed by these options.
• b‘This is bytea with single quote’
• b“Another set of bytes with double quotes”
• b‘’’Bytes using three single quotes’’’ or b“””Bytes using three double quotes”””.
Example:
hexStr = bytes.fromhex('A2f7 4509')
print(hexStr)
byteString = b'\xa2\xf7E\t'
print(byteString.hex())
bArray1 = b"XYZ"
bArray2 = bArray1.replace(b"X", b"P")
print(bArray2)
byteArray1 = b'ABBACACBBACA'
print(byteArray1.count(b'AC'))
print(byteArray1.find(b'CA'))
bArr = b'Mumbai,Kolkata,Delhi,Hyderabad'
partList = bArr.partition(b',')
print(partList)
myByteArray = bytearray('String', 'UTF-8')
memView = memoryview(myByteArray)
print(memView[2]) #ASCII of 'r'
print(bytes(memView[1:5]))
output:

80
b'\xa2\xf7E\t'
a2f74509
b'PYZ'
3

4.2 STORING AND RETRIEVING DATA


4.2.1 FILE INPUT/OUTPUT

• A file is a container in computer storage devices used for storing data.


• When we want to read from or write to a file, we need to open it first. When we are
done, it needs to be closed so that the resources that are tied with the file are freed.
Printing to the Screen
The simplest way to produce output is using the print statement where you can pass
zero or more expressions separated by commas. This function converts the expressions you
pass into a string and writes the result to standard output as follows
Example:
#!/usr/bin/python
print "Python is really a great language,", "isn't it?"
output:
Python is really a great language, isn't it?

Reading Keyboard Input


Python provides two built-in functions to read a line of text from standard input,
which by default comes from the keyboard.
These functions are
• raw_input
• input
The raw_input Function
The raw_input([prompt]) function reads one line from standard input and returns it
as a string (removing the trailing newline).

81
Example:
#!/usr/bin/python
str = raw_input("Enter your input: ")
print "Received input is : ", str
output:
Enter your input: Hello Python
Received input is : Hello Python

The input Function


The input([prompt]) function is equivalent to raw_input, except that it assumes the
input is a valid Python expression and returns the evaluated result to you.
Example:
str = input("Enter your input: ")
print "Received input is : ", str
output:
Enter your input: [x*5 for x in range(2,10,2)]
Recieved input is : [10, 20, 30, 40]

Opening and Closing Files


The open Function
Before you can read or write a file, you have to open it using Python's built-in open()
function. This function creates a file object, which would be utilized to call other support
methods associated with it.
Syntax:
file object = open(file_name [, access_mode][, buffering])

Example:
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "wb")
print "Name of the file: ", fo.name
print "Closed or not : ", fo.closed

82
print "Opening mode : ", fo.mode
print "Softspace flag : ", fo.softspace
output:
Name of the file: foo.txt
Closed or not : False
Opening mode : wb
Softspace flag : 0

The close( ) Method


• The close() method of a file object flushes any unwritten information and closes the
file object, after which no more writing can be done.
• Python automatically closes a file when the reference object of a file is reassigned to
another file. It is a good practice to use the close() method to close a file.
Syntax:
fileObject.close( )
Example:
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "wb")
print "Name of the file: ", fo.name
# Close opend file
fo.close()
output:
Name of the file: foo.txt

Reading and Writing Files


The file object provides a set of access methods to make our lives easier. We would
see how to use read() and write() methods to read and write files.
The write( ) Method
• The write( ) method writes any string to an open file. It is important to note that
Python strings can have binary data and not just text.
• The write( ) method does not add a newline character ('\n') to the end of the string .

83
Syntax:
fileObject.write(string)

Example:
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "wb")
fo.write( "Python is a great language.\nYeah its great!!\n")
# Close opend file
fo.close( )
output:
Python is a great language.
Yeah its great!!

The read( ) Method


The read( ) method reads a string from an open file. It is important to note that Python
strings can have binary data. apart from text data.
Syntax:
fileObject.read([count])

Example:
#!/usr/bin/python
# Open a file
fo = open("foo.txt", "r+")
str = fo.read(10);
print "Read String is : ", str
# Close opend file
fo.close( )
output:
Read String is : Python is

84
4.2.2 STRUCTURED TEXT FILES

A text file is structured as a sequence of lines. And, each line of the text file consists
of a sequence of characters. Termination of each line in a text file is denoted with the end of
line (EOL). There are a few special characters that are used as EOL, but comma {,} and
newlines are the most common ones.
There are two types of files that can be handled in python, normal text files and binary
files (written in binary language, 0s, and 1s). Text files: In this type of file, Each line of text
is terminated with a special character called EOL (End of Line), which is the new line
character ('\n') in python by default.

4.2.3 STRUCTURED BINARY FILES

To open a file in binary format, add 'b' to the mode parameter. Hence the "rb" mode
opens the file in binary format for reading, while the "wb" mode opens the file in binary
format for writing. Unlike text files, binary files are not human-readable. When opened using
any text editor, the data is unrecognizable.
A binary file is a file whose content is in a binary format consisting of a series of
sequential bytes, each of which is eight bits in length. The content must be interpreted by a
program or a hardware processor that understands in advance exactly how that content is
formatted and how to read the data.
To write a binary string to a binary file, you need to open the file in “binary write”
mode using 'wb' as the second positional argument of the open() function. For instance, you'd
write open('my_file. bin', 'wb') instead of open('my_file. bin', 'w') .
Example:
# Open binary file for reading
f = open('myfile1.bin', 'rb')
# Get a string from binary file
d = f.read()
# Display this string.
# The output will be as a string of characters
print("d=",d)

85
#d=
b'\x80\x03]q\x00(K\x01\x88G@\x07\n=p\xa3\xd7\ne.'
# If print as a separate character,
# then the character code will be displayed - as an integer
print("d[5] = ", d[5]) # d[5] = 40
print("d[0] = ", d[0]) # d[0] = 128
# Use bin function for single character
print(bin(d[2])) # 0b1011101
f.close()
output:
d=
b'\x80\x03]q\x00(K\x01\x88G@\x07\n=p\xa3\xd7\ne.'
d[5] = 40
d[0] = 128
0b1011101

4.2.4 RELATIONAL DATABASES

A relational database organizes data into rows and columns, which collectively form a
table. Data is typically structured across multiple tables, which can be joined together via a
primary key or a foreign key.
We can connect to relational databases for analysing data using the pandas library as
well as another additional library for implementing database connectivity. This package is
named as sqlalchemy which provides full SQL language functionality to be used in python.
Installing SQLAlchemy
The installation is very straight forward using Anaconda which we have discussed in
the chapter Data Science Environment. Assuming you have installed Anaconda as described
in this chapter, run the following command in the Anaconda Prompt Window to install the
SQLAlchemy package.
conda install sqlalchemy

86
Reading Relational Tables
We will use Sqlite3 as our relational database as it is very light weight and easy to
use. Though the SQLAlchemy library can connect to a variety of relational sources including
MySql, Oracle and Postgresql and Mssql. We first create a database engine and then connect
to the database engine using the to_sql function of the SQLAlchemy library.
In the example we create the relational table by using the to_sql function from a
dataframe already created by reading a csv file.
Then we use the read_sql_query function from pandas to execute and capture the
results from various SQL queries.
Example:
from sqlalchemy import create_engine
import pandas as pd
data = pd.read_csv('/path/input.csv')
# Create the db engine
engine = create_engine('sqlite:///:memory:')
# Store the dataframe as a table
data.to_sql('data_table', engine)
# Query 1 on the relational table
res1 = pd.read_sql_query('SELECT * FROM data_table',
engine)
print('Result 1')
print(res1)
print('')
# Query 2 on the relational table
res2 = pd.read_sql_query('SELECT dept,sum(salary)
FROM data_table group by dept', engine)
print('Result 2')
print(res2)
output:
Result 1
index id name salary start_date dept

87
0 0 1 Rick 623.30 2012-01-01 IT
1 1 2 Dan 515.20 2013-09-23 Operations
2 2 3 Tusar 611.00 2014-11-15 IT
3 3 4 Ryan 729.00 2014-05-11 HR
4 4 5 Gary 843.25 2015-03-27 Finance
5 5 6 Rasmi 578.00 2013-05-21 IT
6 6 7 Pranab 632.80 2013-07-30 Operations
7 7 8 Guru 722.50 2014-06-17 Finance
Result 2
dept sum(salary)
0 Finance 1565.75
1 HR 729.00
2 IT 1812.30
3 Operations 1148.00

Inserting Data to Relational Tables


We can also insert data into relational tables using sql.execute function available in
pandas. In the below code we previous csv file as input data set, store it in a relational table
and then insert another record using sql.execute.
Example:
from sqlalchemy import create_engine
from pandas.io import sql
import pandas as pd
data =
pd.read_csv('C:/Users/Rasmi/Documents/pydatasci/input.csv')
engine = create_engine('sqlite:///:memory:')
# Store the Data in a relational table
data.to_sql('data_table', engine)
# Insert another row
sql.execute('INSERT INTO data_table VALUES(?,?,?,?,?,?)',
engine, params=[('id',9,'Ruby',711.20,'2015-03-27','IT')])

88
# Read from the relational table
res= pd.read_sql_query('SELECT ID,Dept,Name,Salary,start_date
FROM data_table', engine)
print(res)
output:
id dept name salary start_date
0 1 IT Rick 623.30 2012-01-01
1 2 Operations Dan 515.20 2013-09-23
2 3 IT Tusar 611.00 2014-11-15
3 4 HR Ryan 729.00 2014-05-11
4 5 Finance Gary 843.25 2015-03-27
5 6 IT Rasmi 578.00 2013-05-21
6 7 Operations Pranab 632.80 2013-07-30
7 8 Finance Guru 722.50 2014-06-17
8 9 IT Ruby 711.20 2015-03-27

Deleting Data from Relational Tables


We can also delete data into relational tables using sql.execute function available in
pandas.
Example:
from sqlalchemy import create_engine
from pandas.io import sql
import pandas as pd
data=
pd.read_csv('C:/Users/Rasmi/Documents/pydatasci/input.csv')
engine = create_engine('sqlite:///:memory:')
data.to_sql('data_table', engine)
sql.execute('Delete from data_table where name = (?) ', engine,
params=[('Gary')])
res= pd.read_sql_query('SELECT ID,Dept,Name,Salary,start_date
FROM data_table', engine)

89
print(res)
output:
id dept name salary start_date
0 1 IT Rick 623.3 2012-01-01
1 2 Operations Dan 515.2 2013-09-23
2 3 IT Tusar 611.0 2014-11-15
3 4 HR Ryan 729.0 2014-05-11
4 6 IT Rasmi 578.0 2013-05-21
5 7 Operations Pranab 632.8 2013-07-30
6 8 Finance Guru 722.5 2014-06-17

4.2.5 NoSQL DATA STORES

As more and more data become available as unstructured or semi-structured, the need
of managing them through NoSql database increases. Python can also interact with NoSQL
databases in a similar way as is interacts with Relational databases.we will use python to
interact with MongoDB as a NoSQL database.

In order to connect to MongoDB, python uses a library known as pymongo. You can
add this library to your python environment, using the below command from the Anaconda
environment.
conda install pymongo
This library enables python to connect to MOngoDB using a db client. Once
connected we select the db name to be used for various operations.
NoSQL databases are more flexible than relational databases. In these types of
databases, the data storage structure is designed and optimized for specific requirements.

90
There are four main types for NoSQL libraries
• Documents
• key-value,
• wide-column
• graphs.

Inserting Data
To insert data into MongoDB we use the insert() method which is available in the
database environment. First we connect to the db using python code and then we provide the
document details in form of a series of key-value pairs.
Example:
# Import the python libraries
from pymongo import MongoClient
from pprint import pprint
# Choose the appropriate client
client = MongoClient()
# Connect to the test db
db=client.test
# Use the employee collection
employee = db.employee
employee_details =
{
'Name': 'Raj Kumar',
'Address': 'Sears Streer, NZ',
'Age': '42'

91
}
# Use the insert method
result = employee.insert_one(employee_details)
# Query for the inserted document.
Queryresult = employee.find_one({'Age': '42'})
pprint(Queryresult)
output:
{u'Address': u'Sears Streer, NZ',
u'Age': u'42',
u'Name': u'Raj Kumar',
u'_id': ObjectId('5adc5a9f84e7cd3940399f93')}

Updating Data
Updating an existing MongoDB data is similar to inserting. We use the update()
method which is native to mongoDB. In the this code we are replacing the existing record
with new key-value pairs.
Example:
# Import the python libraries
from pymongo import MongoClient
from pprint import pprint
# Choose the appropriate client
client = MongoClient()
# Connect to db
db=client.test
employee = db.employee
# Use the condition to choose the record
# and use the update method
db.employee.update_one(
{"Age":'42'},
{
"$set":

92
{
"Name":"Srinidhi",
"Age":'35',
"Address":"New Omsk, WC"
}
}
)
Queryresult = employee.find_one({'Age':'35'})
pprint(Queryresult)
output:
{u'Address': u'New Omsk, WC',
u'Age': u'35',
u'Name': u'Srinidhi',
u'_id': ObjectId('5adc5a9f84e7cd3940399f93')}

Deleting Data
Deleting a record is also straight forward where we use the delete method.
Example:
# Import the python libraries
from pymongo import MongoClient
from pprint import pprint
# Choose the appropriate client
client = MongoClient()
# Connect to db
db=client.test
employee = db.employee
# Use the condition to choose the record
# and use the delete method
db.employee.delete_one({"Age":'35'})
Queryresult = employee.find_one({'Age':'35'})
pprint(Queryresult)

93
output:
None

4.3 THE WEB

Python web application development typically employs Python frameworks to


construct its backend. These tasks, including parsing HTTP requests and creating responses;
accessing databases; authenticating users; authorizing access; etc. are so frequently employed
that they are implemented on multiple backends.

4.3.1 WEB CLIENTS

Python can be used to support web programming that falls into one of two general
categories
Client programming – accesses web sites or web applications
Server programming – script executed by the server to provide a web site, perform server-
side applications, etc.
Web client
Any program that retrieves data from a web server using the HTTP protocol.

94
Examples:
• web browsers – contact web servers using HTTP protocol and display HTTP
responses
• web crawlers – traverse the web automatically to gather information
• web service clients (service requester) – request and process data from a web service
provider; the web service provider responds using some web service protocol such as
RSS (Rich Site Summary) or RPC (remote procedure call)
Python web client programming
modules that come standard with Python
• urllib – interface for fetching data across the web
• urllib2 – an interface for fetching data across the web that allows you to specify
HTTP request headers, handle authentication and cookies
• httplib – makes http requests; is used by urllib and urllib2
• HTMLParser – for parsing HTML and XHTML files
• xmlrpclib – allows clients to call methods on a remote server
• cookielib (used to be Clientcookie) – provides classes for handling HTTP cookies
• utidylib and mxTidy – for cleaning up html
• BeautifulSoup – permissive HTML parser
• html5lib – for parsing html into a tree
Python urllib
• This module provides a high-level interface for fetching data across the World Wide.
• urlopen() function is similar to the built-in function open(), but accepts Universal
Resource Locators (URLs) instead of filenames (also can only open for reading).

95
urllib methods
urlopen( url[, data[, proxies]]) - Open a network object denoted by a URL for reading. If
the URL does not represent a local file, it opens a socket (a connection between two
programs) to a server somewhere on the network; file-like object is returned,
• typically the request is a GET; if data is given, then the request is a POST
• proxies is a dictionary that can be used to specify the proxy for different types of
requests
prx = {'http': '
the file-like object returned supports the following methods
• read – read max number of bytes or entire file if size omitted
• readline – read a single line
• readlines – returns list of lines read
• fileno – returns integer file number descriptor
• close – close file
• info – returns header info
• geturl – returns actual URL used to access the resource
Example:
import urllib
import re
page = urllib.urlopen("
text = page.read()
atagstr = '<a href="([^"]+)">(.*)</a>'
matches = re.findall(atagstr, text)
for link, name in matches:
name = name.strip()
print "%s: %s" % (name, link)
output
Home: index.html
Handouts: handouts.html
Resources: resources.html
Homeworks: homeworks.html
Slides: slides.html
CS 5530, fall 2007

96
4.3.2 WEB SERVERS

Web servers respond to Hypertext Transfer Protocol (HTTP) requests from clients
and send back a response containing a status code and often content such as HTML, XML or
JSON as well. One important aspect of internet is the web servers that are at the root of the
client server model.

Leading web servers include Apache, Microsoft's Internet Information Services (IIS)
and Nginx -- pronounced engine X.
Gunicorn
Gunicorn is a stand-alone web server which has a central master process tasked with
managing the initiated worker processes of differing types.
Important Features
• It supports WSGI and can be used with any WSGI running Python application and
framework
• It can also be used as a drop-in replacement for Paster (ex: Pyramid), Django's
Development Server, web2py, etc
• Offers the choice of various worker types/configurations and automatic worker
process management
• HTTP/1.0 and HTTP/1.1 (Keep-Alive) support through synchronous and
asynchronous workers
• Comes with SSL support
• Extensible with hooks
CherryPy WSGI Server
CherryPy is a self contained web framework as it can run on its own without the need
of additional software. It has its own WSGI, HTTP/1.1-compliant web server.
Important Features
• It can run any Python web applications running on WSGI.
• It can handle static files and it can just be used to serve files and folders alone.

97
• It is thread-pooled.
• It comes with support for SSL.
• It is an easy to adapt, easy to use pure-Python alternative which is robust and reliable.
Twisted Web
It is a web server that comes with the Twisted networking library. Whereas Twisted
itself is "an event-driven networking engine", the Twisted Web server runs on WSGI and it is
capable of powering other Python web applications.
Important Features
• It runs WSGI Python applications
• It can act like a Python web server framework, allowing you to program it with the
language for custom HTTP serving purposes
• It offers simple and fast prototyping ability through Python Scrips (.rpy) which are
executed upon HTTP requests
• It comes with proxy and reverse-proxy capabilities
• It supports Virtual Hosts
• It can even serve Perl, PHP et cetera

4.3.3 WEB SERVICES AND AUTOMATION

Web services are consumed by data scientists, quality engineers, and application
developer. They can be consumed in Python, R, or via the API. Users can consume the
service directly using a single consumption call, which is referred to as a "Request Response"
approach.
Python is used to fill up online forms automatically. Any suitable Python script can
extract data from a file and fill an equivalent online form. You can automate filling up online
forms like Google forms or other sign-up / login forms.

98
Components of Web Services
The basic web services platform is XML + HTTP. All the standard web services
work using the following components :
• SOAP (Simple Object Access Protocol)
• UDDI (Universal Description, Discovery and Integration)
• WSDL (Web Services Description Language)
How Does a Web Service Work?
A web service enables communication among various applications by using open
standards such as HTML, XML, WSDL, and SOAP.
A web service takes the help of :
• XML to tag the data
• SOAP to transfer a message
• WSDL to describe the availability of service.
Example:
Consider a simple account-management and order processing system. The accounting
personnel use a client application built with Visual Basic or JSP to create new accounts and
enter new customer orders.
The processing logic for this system is written in Java and resides on a Solaris
machine, which also interacts with a database to store information.
The steps to perform this operation are as follows :
• The client program bundles the account registration information into a SOAP
message.
• This SOAP message is sent to the web service as the body of an HTTP POST request.
• The web service unpacks the SOAP request and converts it into a command that the
application can understand.
• The application processes the information as required and responds with a new unique
account number for that customer.
• Next, the web service packages the response into another SOAP message, which it
sends back to the client program in response to its HTTP request.

99
UNIT-V
5.1 SYSTEMS

Python is cross-platform and will work on Windows, macOS, and Linux. It is mostly
a matter of personal preferences when it comes to choosing an operating system.
Python System Command
• Python os. system( ) function. We can execute system command by using os.system( )
function.
• Python subprocess. call( ) Function. In the previous section, we saw that OS.
• Python subprocess. check_output( ) function. So far, we executed the system
commands with the help of python.

5.1.1 FILES

• In Python, files are treated in two modes as text or binary. The file may be in the text
or binary format, and each line of a file is ended with the special character.
• A module is a file containing Python definitions and statements. The file name is the
module name with the suffix .py appended.
• File handling is an important part of any web application.
• Python has several functions for creating, reading, updating, and deleting files.
File Handling
• The key function for working with files in Python is the open( ) function.
• The open ( ) function takes two parameters; filename, and mode.
There are four different methods (modes) for opening a file:
• "r" - Read - Default value. Opens a file for reading, error if the file does not exist
• "a" - Append - Opens a file for appending, creates the file if it does not exist
• "w" - Write - Opens a file for writing, creates the file if it does not exist
• "x" - Create - Creates the specified file, returns an error if the file exists
In addition you can specify if the file should be handled as binary or text mode
“t” - Text - Default value. Text mode
“b” - Binary - Binary mode (e.g. images)
To open a file for reading it is enough to specify the name of the file.

100
Syntax:
f = open("demofile.txt")

• To open the file, use the built-in open( ) function.


• The open( ) function returns a file object, which has a read() method for reading the
content of the file.
Example:
f = open("demofile.txt", "r")
print(f.read())
output:
Hello! Welcome to demofile.txt
This file is for testing purposes.
Good Luck!

Read Lines
You can return one line by using the readline( ) method.
Example:

f = open("demofile.txt", "r")
print(f.readline())
output:
Hello! Welcome to demofile.txt

Close Files
It is a good practice to always close the file when you are done with it.
Example:

f = open("demofile.txt", "r")
print(f.readline( ))
f.close( )
output:
Hello! Welcome to demofile.txt
Write to an Existing File

101
To write to an existing file, you must add a parameter to the open() function:
"a" - Append - will append to the end of the file
"w" - Write - will overwrite any existing content
Example:

f = open("demofile2.txt", "a")
f.write("Now the file has more content!")
f.close()
#open and read the file after the appending:
f = open("demofile2.txt", "r")
print(f.read( ))
output:
Hello! Welcome to demofile2.txt
This file is for testing purposes.
Good Luck!Now the file has more content!

Create a New File


To create a new file in Python, use the open() method, with one of the following
parameters:
• "x" - Create - will create a file, returns an error if the file exist
• "a" - Append - will create a file if the specified file does not exist
• "w" - Write - will create a file if the specified file does not exist
Example:
f = open("myfile.txt", "x")
Result: a new empty file is created!

Example:

Create a new file if it does not exist:


f = open("myfile.txt")
Delete a File
To delete a file, you must import the OS module, and run its os.remove( ) function.

102
Example:
Remove the file "demofile.txt":
Import os
os.remove("demofile.txt")

Check if File exist:


To avoid getting an error, you might want to check if the file exists before you try to
delete it.
Example:
Check if file exists, then delete it:
import os
if os.path.exists("demofile.txt"):
os.remove("demofile.txt")
else:
print("The file does not exist")

Delete Folder
To delete an entire folder, use the os.rmdir() method.

Example:
Remove the folder "myfolder":

import os
os.rmdir("myfolder")

5.1.2 DIRECTORIES

Directories are a way of storing, organizing, and separating the files on a computer.
The directory that does not have a parent is called a root directory. The way to reach the file
is called the path.
The dir() function returns all properties and methods of the specified object, without
the values.
This function will return all the properties and methods, even built-in properties
which are default for all object.
Syntax:

103
dir(object)

Example:
class Person:
name = "John"
age = 36
country = "Norway"
print(dir(Person))
output:
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__',
'__eq__', '__format__', '__ge__', '__getattribute__', '__gt__',
'__hash__', '__init__', '__init_subclass__', '__le__', '__lt__',
'__module__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
'__str__', '__subclasshook__', '__weakref__', 'age', 'country',
'name']

Python os.listdir() Method


• The os.listdir() method returns a list of the names of the entries in a directory.
• The list is in arbitrary order. It does not include the special entries '.' and '..' even if
they are present in the directory.
Syntax:
os.listdir(path)
Example:
#Import os Library
import os
#print all entries in current directory
print (os.listdir( ))

5.1.3 PROGRAMS AND PROCESSES

104
Program contains a set of instructions designed to complete a specific task. Process is
an instance of an executing program. Program is a passive entity as it resides in the secondary
memory. Process is a active entity as it is created during execution and loaded into the main
memory.
A program is a set of instruction codes that has been designed to complete a certain
task. It is a passive entity stored in the secondary memory of the computer system. A program
is considered as a passive and static entity.

A program is like a file which contains a set of instruction codes stored on a disk in
the form of an executable file. A program contains instructions written in any programming
language. Programs have an unlimited span of time.
A process is an instance of a program that is being currently executed. It is a dynamic
and active entity of a program. Processes are created when the programs are executing and
they reside in the main memory.

A process exists only for a limited time, and hence gets terminated as soon as the task
completes. A process always consists of instructions written in machine language. A process
contains temporary data, data selection, etc.

5.1.4 CALENDARS AND CLOCKS

105
Python defines an inbuilt module calendar that handles operations related to the
calendar.
The calendar module allows us to output calendars like the program and provides
additional useful functions related to the calendar. Functions and classes defined in the
calendar module use an idealized calendar, the current Gregorian calendar extended
indefinitely in both directions. By default, these calendars have Monday as the first day of the
week, and Sunday as the last (the European convention).
Example:
Display the Calendar of a given month.
# Python program to display calendar of given month of
the year
# import module
import calendar
yy = 2017
mm = 11
# display the calendar
print(calendar.month(yy, mm))
output:

Example:
Display the calendar of the given year.
# Python code to demonstrate the working of
# calendar() function to print calendar
# importing calendar module
# for calendar operations
import calendar

106
# using calendar to print calendar of year
# prints calendar of 2018
print ("The calendar of year 2018 is : ")
print (calendar.calendar(2018))
output:

Python - Date & Time


A Python program can handle date and time in several ways. Converting between date
formats is a common chore for computers. Python's time and calendar modules help track
dates and times.
Time intervals are floating-point numbers in units of seconds. Particular instants in
time are expressed in seconds since 00:00:00 hrs January 1, 1970(epoch).
There is a popular time module available in Python which provides functions for
working with times, and for converting between representations. The function time.time()
returns the current system time in ticks since 00:00:00 hrs January 1, 1970(epoch).
Example:
#!/usr/bin/python

107
import time; # This is required to include time module.
ticks = time.time()
print "Number of ticks since 12:00am, January 1, 1970:",
ticks
output:
Number of ticks since 12:00am, January 1, 1970:
7186862.73399

Getting current time


To translate a time instant from a seconds since the epoch floating-point value into a
time-tuple, pass the floating-point value to a function (e.g., localtime) that returns a time-
tuple with all nine items valid.
Example:
#!/usr/bin/python
import time;
localtime = time.localtime(time.time())
print "Local current time :", localtime
output:
Local current time : time.struct_time(tm_year=2013,
tm_mon=7, tm_mday=17, tm_hour=21, tm_min=26,
tm_sec=3, tm_wday=2, tm_yday=198, tm_isdst=0)

Getting formatted time


You can format any time as per your requirement, but simple method to get time in
readable format is asctime( ) .
Example:
#!/usr/bin/python
import time;
localtime = time.asctime( time.localtime(time.time()) )
print "Local current time :", localtime
output:

108
Local current time : Tue Jan 13 10:17:09 2009

5.2 CONCURRENCY
5.2.1 QUEUES

Queue in Python is a linear data structure with a rear and a front end, similar to a
stack. It stores items sequentially in a FIFO (First In First Out) manner.

Queue is a linear data structure that stores items in First In First Out (FIFO) manner.
With a queue the least recently added item is removed first. A good example of queue is any
queue of consumers for a resource where the consumer that came first is served first.
Example:
# Queue implementation in Python
class Queue
def __init__(self):
self.queue = []
# Add an element
def enqueue(self, item):
self.queue.append(item)
# Remove an element
def dequeue(self):
if len(self.queue) < 1:
return None
return self.queue.pop(0)
# Display the queue
def display(self):

109
print(self.queue)
def size(self):
return len(self.queue)
q = Queue()
q.enqueue(1)
q.enqueue(2)
q.enqueue(3)
q.enqueue(4)
q.enqueue(5)
q.display()
q.dequeue()
print("After removing an element")
q.display()

5.2.2 PROCESSES

In Python, a process is an instance of the Python interpreter that executes Python


code. In Python, the first process created when we run our program is called the
'MainProcess'. It is also a parent process and may be called the main parent process. the
main process will create the first child process or processes.
At first, we need to write a function, that will be run by the process. Then, we need to
instantiate a process object. If we create a process object, nothing will happen until we tell it
to start processing via start() function. Then, the process will run and return its result.

5.2.3 THREADS

Threading in python is used to run multiple threads (tasks, function calls) at the same
time. Note that this does not mean that they are executed on different CPUs. Python threads
will NOT make your program faster if it already uses 100 % CPU time. In that case, you
probably want to look into parallel programming.

110
Python threading allows you to have different parts of your program run concurrently
and can simplify your design. If you've got some experience in Python and want to speed up
your program using threads.

5.2.4 GREEN THREADS AND GEVENT

In computer programming, a green thread (virtual thread) is a thread that is


scheduled by a runtime library or virtual machine (VM) instead of natively by the underlying
operating system (OS).
Gevent is the use of simple, sequential programming in python to achieve scalability
provided by asynchronous IO and lightweight multi-threading (as opposed to the callback-
style of programming using Twisted's Deferred).
In python, we implement greenlets via the gevent package and we implement
pthreads via python's built-in threading module. Both green threads (greenlets) and POSIX
threads (pthreads) are mechanisms to support multithreaded execution of programs.

111
5.2.5 TWISTED

• Twisted is an open source network framework written entirely in Python. It allows


you to create a SMTP, HTTP, proxy and ssh servers (and more) in Python with
minimal effort.
• An event-driven networking engine. Written in Python. Licensed under the open
source MIT License.
• The core of Twisted is the reactor event loop. The reactor knows about network, file
system, and timer events. It waits on and then handles these events, abstracting away
platform-specific behavior and presenting interfaces to make responding to events
anywhere in the network stack easy.

5.2.6 REDIS

Redis is an in-memory key-value pair database typically classified as a NoSQL


database. Redis is commonly used for caching, transient data storage and as a holding area
for data during analysis in Python applications. Redis is an implementation of the NoSQL
database concept.
Popular Redis Use Cases
• Caching.
• Chat, messaging, and queues.
• Gaming leaderboards.
• Session store.
• Rich media streaming.
• Geospatial.

112
• Machine Learning.
• Real-time analytics.
Example
Ensure that you are able to use the following Redis command to connect to the Redis
instance.
redis-cli 127.0.0.1:6379>
The following Python code allows you to connect to the default Redis server instance
pip3 install redis

import redis
pool = redis.ConnectionPool(host='localhost', port=6379,
db=0)
redis = redis.Redis(connection_pool=pool)
edis.set('mykey', 'Hello from Python!')
value = redis.get('mykey')
print(value)
redis.zadd('vehicles', {'car' : 0})
redis.zadd('vehicles', {'bike' : 0})
vehicles = redis.zrange('vehicles', 0, -1)
print(vehicles)

5.3 NETWORKS

Python Network Programming is about using python as a programming language to


handle computer networking requirements. For example, if we want to create and run a local
web server or automatically download some files from a URL with a pattern.
Python Internet Modules
Python Module Protocol Function
httplib, urllib, xmlrpclib For HTTP protocol It deals with web pages
nntplib For NNTP protocol It deals with Usenet news
ftplib, urllib For FTP protocol It deals with the transfer of files
smtplib For SMTP protocol It deals with sending mails

113
5.3.1 PATTERNS

Python patterns encode programs in different shapes and formats to create recognized
patterns. These patterns are built using different combinations of codes to allow programmers
logical practice to implement the same strategy in real-life courses and improve programming
skills.
There are a total of 11 behavioral patterns in Python: Chain of responsibility,
Command, Interpreter, Iterator, Mediator, Memento, Observer, State, Strategy,
Template, Visitor.

5.3.2 THE PUBLISH-SUBSCRIBE MODEL

The publish/subscribe (pub/sub) model is a messaging pattern in which publishers


send messages to a message broker, and subscribers express interest in receiving certain
messages. The message broker is responsible for delivering the messages to the subscribed
clients.
Publish-subscribe messaging, or pub/sub messaging, is an asynchronous
communication model that makes it easy for developers to build highly functional and
architecturally complex applications in the cloud.

114
5.3.3 TCP/IP

TCP/IP Client and Server


• Then bind( ) is used to associate the socket with the server address.
• Calling listen( ) puts the socket into server mode, and accept() waits for an incoming
connection.
• accept( ) returns an open connection between the server and client, along with the
address of the client.
• TCP stands for Transmission Control Protocol, a communications protocol for
computers to exchange information over a network. IP stands for Internet Protocol.
IP identifies the IP address of the applications or devices to send data to and forms the
Network Layer in the OSI stack.

5.3.4 SOCKETS

Socket programming is a way of connecting two nodes on a network to communicate


with each other. One socket(node) listens on a particular port at an IP, while the other socket
reaches out to the other to form a connection.
A socket is one endpoint of a two-way communication link between two programs
running on the network. A socket is bound to a port number so that the TCP layer can
identify the application that data is destined to be sent to. An endpoint is a combination of an
IP address and a port number.
There are two type of sockets:
• SOCK_STREAM
• SOCK_DGRAM .

5.3.5 ZEROMQ

• ZeroMQ is a library that allows you to perform low-level message passing, but unlike
message-oriented middleware, an ZEROMQ system can run without a dedicated
message broker. To understand ZEROMQ, you need to think in the sense of network
sockets that carry atomic messages across various transports.

115
• ZeroMQ is an asynchronous messaging library, aimed at use in distributed or
concurrent applications. It provides a message queue, but unlike message-oriented
middleware, a ZeroMQ system can run without a dedicated message broker; the zero
in the name is for zero broker.

5.3.6 INTERNET SERVICES

To access/exchange a large amount of data such as software, audio clips, video clips,
text files, other documents, etc., we need internet services. You must use an Internet service
to connect to the Internet. Data can be sent from Internet servers to your machine via Internet
service.

Some of the commonly used internet services are :


• Communication Services
• Information Retrieval Services
• File Transfer
• World Wide Web Services
• Web Services
• Directory Services
• Automatic Network Address Configuration
• Network Management Services
• Time Services
• Usenet
• NewsGroup
• Ecommerce

116
5.3.7 WEB SERVICES AND APIS

Web services can be generally regarded as functions or functionality of applications


or systems exposed over the Web using standardised message formats and typically
interfaced to other software using traditional APIs, although "message-centric" usage of such
services is also possible and may be favoured by certain.

API stands for application programming interface. In essence, an API acts as a


communication layer, or as the name says, an interface, that allows different systems to talk
to each other without having to understand exactly what each other does. APIs can come in
many forms or shapes.
An API is an interface that allows you to build on the data and functionality of
another application, while a web service is a network-based resource that fulfills a specific
task.

5.3.8 REMOTE PROCESSING

Remote Procedure Call (RPC) system enables you to call a function available on a
remote server using the same syntax which is used when calling a function in a local library.
This is useful in two situations.You can utilize the processing power from multiple machines
using rpc without changing the code for making the call to the programs located in the remote
systems.The data needed for the processing is available only in the remote system.
So in python we can treat one machine as a server and another machine as a client
which will make a call to the server to run the remote procedure. In our example we will take
the localhost and use it as both a server and client.

117
JSON or JavaScript Object Notation is a lightweight data-interchange format. It is
easy for humans to read and write. It is easy for machines to parse and generate. The RPC
call made based on JSON is able to send data in a much compact and efficient manner than
the normal XML based RPC call. The python module jsonrpclib is able to create a simple
JSON based server and client.
Example:
# server program
from jsonrpclib.SimpleJSONRPCServer import
SimpleJSONRPCServer
def findlen(*args):
res = [ ]
for arg in args:
try:
lenval = len(arg)
except TypeError:
lenval = None
res.append((lenval, arg))
return res
def main():
server = SimpleJSONRPCServer(('localhost', 1006))
server.register_function(findlen)
print("Start server")
server.serve_forever()
if __name__ == '__main__':
main()
# Call by client
from jsonrpclib import Server
def main():
conn = Server('http://localhost:1006')
print(conn.findlen(('a','x','d','z'), 11, {'Mt. Abu': 1602, 'Mt.
Nanda': 3001,'Mt. Kirubu': 102, 'Mt.Nish': 5710}))

118
if __name__ == '__main__':
main( )
output:
[[4, [u'a', u'x', u'd', u'z']], [None, 11], [4, {u'Mt. Abu': 1602,
u'Mt. Kirubu': 102, u'Mt. Nanda': 3001, u'Mt.Nish': 5710}]]

5.3.9 BIG FAT DATA AND MAPREDUCE

Big Data Analysis with Python teaches you how to use tools that can control this data
avalanche for you.

MapReduce is a programming model used for efficient processing in parallel over


large data-sets in a distributed manner. The data is first split and then combined to produce
the final result. The libraries for MapReduce is written in so many programming languages
with various different-different optimizations.

119
5.3.10 WORKING IN THE CLOUDS

Cloud Code helps you write, run, and debug cloud-native apps quickly and easily.
Extensions to IDEs provide turnkey support for Python development including code
completion, linting, and snippets.
How To Create Word Cloud in Python
• Import Necessary Libraries
• Selecting the Dataset
• Selecting the Text and Amount of Text for Word Cloud.
• Check for NULL values
• Adding Text to a Variable
• Creating the Word Cloud
• Plotting the Word Cloud
• The Complete Code.
How to make a word cloud in Python and Jupyter Notebook, Step by Step:
• Install the wordcloud package.
• Import wordcloud and matplotlib into your notebook.
• Create a term-document matrix with TF-IDF values (Optional Step)

120
MODEL QUESTION PAPER
SALEM CHRISTIAN COLLEGE OF ARTS AND SCIENCE
CLASS I MSC(CS)
PYTHON PROGRAMMING
Time: 3 Hours Max. Marks: 75

PART-A (1X15=15)

Answer ALL the Questions.


1. Python is said to be easily _____.
(a) Readable language (b) Writeable language
(c) bug-able language (d) Script-able language
2.Dictionary makes use of ____.
(a) Keys (b) Key Values (c) Values (d) Key
3.Who Created Python ?
(a) Guido Van Rossum (b) James Gosling (c) Denis Ritchie (d) Tom crusie
4.Python can connect to __________ systems.
(a) sql (b) rdbms (c) database (d) None of above
5.In Python list are written with _________ brackets.
(a)Square (b) Curly (c) Round (d) None of above
6. Condtional statements are also known as ______ statements.
(a) Decision-making (b) Array (c) List (d) None of above
7. An ____ statement has less number of conditional checks than two sucessive ifs.
(a) if else if (b) if elif (c) if-else (d) None of above
8. Loops are known as _______ in programming.
(a) Control flow statements (b) Conditional statements (c)Data structure statements
(d) None of above
9.In Python,the break and continue statements,together are called ____ statements.
(a) Jump (b) goto (c) compound (d) None of above
10. Python is a ___object-oriented programming language.
(a) Genaral Purpose (b) Special Purpose
(c)Medium level programming language (d) All of the above
11. The Command used to start Python from the command prompt is ______.
(a) Python (b) execute python (c) py (d) run python
12. Which of the following advantage of using with clause while opening a file?

121
(a) The file read automatically (b) The file write automatically
(c) The file closed automatically (d) The file delete automatically
13. A variable defined outside a function is referred to as __________.
(a) global variable (b) a function variable (c) a block variable(d) a local variable
14. In Python code, on encountering a syntax error,the _____ does not execute the program.
(a) Interpeter (b) Processor (c) Editor (d)Converter
15. Which of the follwing keyword is used to access the numpy module in Python?
(a) import (b) access (c) fetch (d) from

PART-B (2X5=10)
Answer Any TWO Questions out of FIVE
16.Explain briefly sets.
17.Write a short note on while loop.
18.What do you mean by Override a Method?
19.Explain the role of Web Servers.
20.Explain the Green Threads and gevent.
PART-C (5X10=10)
Answer ALL the Questions.
21.a) Explain about Dictionaries. (OR)
b) Explaina about the Numbers
22.a) Explain the various Comprehensions. (OR)
b) Explain the various handle errors with try and except.
23.a) Explain about Modules and the import Statement. (OR)
b) Explain about the NoSQL Data Stores.
24. a) List and explain the Calendars and Clocks. (OR)
b) Write about Programs and Processes.
25 a) Discuss the The Publish-Subscribe Model. (OR)
b) Explain the Big Fat Data and MapReduce.

MODEL QUESTION PAPER


SALEM CHRISTIAN COLLEGE OF ARTS AND SCIENCE

122
CLASS I MSC CS
PYTHON PROGRAMMING

1.______ is the output of print str[2:5] if str = 'Hello World!'?


a.llo World! b.H c.llo d.None of the above.
2.______is the output of print tuple[0] if tuple =( 'abcd', 786 , 2.23, 'john', 70.2 )?
a.. ( 'abcd', 786 , 2.23, 'john', 70.2 ) b.abcd c.Error d.None of the above.
3._____ function convert an object to a string in python?
a.int(x [,base]) b.long(x [,base] ) c. float(x) d.str(x)
4.________ operator in python performs exponential (power) calculation on operands?
a.** b.// c.is d.not in
5.________function returns a randomly selected element from range?
a.choice(seq) b.randrange ([start,] stop [,step]) c.random() d.seed([x])
6.________function checks in a string that all characters are in lowercase?
a.islower() b.isnumeric() c.isspace() d.istitle()
7._________ function checks in a string that all characters are decimal?
a.upper() b.isdecimal() c.swapcase() d.title()
8._______ is the output of [1, 2, 3] + [4, 5, 6]?
a.[1, 2, 3, 4, 5, 6] b.[1, 2, 3],[4, 5, 6] c.[5, 7,9] d.21
9._________function returns item from the list with max value?
a.cmp(list) b.len(list) c.max(list) d.min(list)
10.______function do you use to read a string?
a.input("Enter a string") b.eval(input("Enter a string"))
c.enter("Enter a strisng") d.eval(enter("Enter a string"))

123

You might also like