0% found this document useful (0 votes)
30 views44 pages

Unit Iii

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views44 pages

Unit Iii

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Python Programming (R19)

Unit-3
Topics to be discussed:
List and Dictionaries: Lists, Defining Simple Functions, Dictionaries
Design with Function: Functions as Abstraction Mechanisms, Problem
Solving with Top Down Design, Design with Recursive Functions, Case Study
Gathering Information from a File System, Managing a Program’s Namespace,
Higher Order Function.
Modules: Modules, Standard Modules, Packages.

Introduction to Lists
A list is a sequence of data values called items or elements. An item can be of
any type. Here are some real-world examples of lists:

• A shopping list for the grocery store


• A to-do list
• A roster for an athletic team
• A guest list for a wedding
• A recipe, which is a list of instructions
• A text document, which is a list of lines
• The words in a dictionary
• The names in a phone book
The logical structure of a list is similar to the structure of a string. Each of
the items in a list is ordered by position. Like a character in a string, each
item in a list has a unique index that specifies its position. The index of the
first item is 0, and the index of the last item is the length of the list minus 1.
List Literals and Basic Operators
In Python list is sequence in which elements or items are written as comma-
separated items that are enclosed in pair of square brackets. A list can contain
different types of items or elements such integers, floating point numbers,
string, complex numbers etc. Examples:
 [1,2,3,4] -> List of integers
 [‘apple’,’mango’,’orange’] -> List of strings
 [1.23,3.45,6.34] ->List of floating point numbers
 [1,’apple’, 3+4j, 4.56] - >List of different data types.
We can put lists as items in the list. Then it is called nested list.
Example:
L=[ [1,2,3,4],[‘raj’,’Rohan’,’kavya’,’sruthi’],[78.9,83.2,75.8,91.3] ]

1
Python Programming (R19)

If the items in the list are literals then they will be put into resulting list
directly. If the items are variables or expressions, then their values after
evaluating them will be placed in the resulting string.
X=2
[x,math.sqrt(x)] -> [2, 1.41421]
We can use range() and list() functions to create a list of integers.
Example:
>>> L=list(range(1,6))
>>> L
[1, 2, 3, 4, 5]
We can use Concatenation (+) to combine two lists and equality (==)
to test whether two lists are same or not. We can use print() function to print
the list. We can use for() loop to iterate over the list items. We can use the ‘in’
and ‘not in’ membership operators to find some item is a member of a list or
not.
Replacing an Element in a List
The subscript operator is used to replace an element at a given position, as
shown below:
Lsit_name[position]=new_item
L[2]=30
List Methods for Inserting and Removing Elements

LIST METHOD Description

L.append(element) Adds element to the end of L.

L.extend(aList) Adds the elements of a List to the end of L.

L.insert(index, Inserts element at index if index is less than the length


element) of L. Otherwise, inserts element at the end of L.

L.pop() Removes and returns the element at the end of L

L.pop(index) Removes and returns the element at index

2
Python Programming (R19)

L.remove(element) Deletes first occurrence of element from list

L.reverse() Reverses the elements of List

L.count(element) Counts the number of times element occurs in the list

L.index(element) Returns index of first occurrence of the element

Searching a List
After elements have been added to a list, a program can search for a given
element. The ‘in’ operator can be used to determine whether an item is
present in the list or not. We have no find() method with list as it is present
with string methods.

Example Program:

L=[]
size=int(input('Enter the number of elements:’))
#reading elements into list
for i in range(size):
L.append(int(input('Enter next elemnt:')))
print('The List items are :',L)
element=int(input('Enter the element to search :'))
if element in L:
print(element, 'is found in the list')
else:
print(element,'Not found')

Write a Python program to implement the Stack?


Stack.py Output
#stack implementation uisng list Enter no. of elements:3
s=[] Enter 0 element:3
n=int(input('Enter no. of elements:')) Enter 1 element:4
for i in range(n): Enter 2 element:1
s.append(int(input(f'Enter{i} element:'))) The elements of the stack are:
#display stack [3, 4, 1]

3
Python Programming (R19)

print('The elements of the stack are :',s) The stack after pop operation
s.pop() is: [3, 4]
print('The stack after pop operation is :',s)

Write a Python program to implement the Queue?


Queue.py Output
#Queue implementation using list Enter no. of elements into
q=[] Queue:3
n=int(input('Enter no. of elements into Enter 0 element:2
Queue:')) Enter 1 element:3
for i in range(n): Enter 2 element:1
q.append(int(input(f'Enter {i} element:'))) The elements of the Queue
#display Queue are: [2, 3, 1]
print('The elements of the Queue are :',q) The Queue after deletion
#deletion operation operation is: [3, 1]
q.pop(0)
print('The Queue after deletion operation is
:',q)

Sorting a List
Although a list’s elements are always ordered by position, it is possible to
impose a natural ordering on them as well. In other words, you can arrange
some elements in numeric or alphabetical order. A list of numbers in
ascending order and a list of names in alphabetical order are sorted lists. The
list method sort() mutates a list by arranging its elements in ascending order.

Example: L.sort()
Mutator Methods and the Value None
Normally function or method returns a value to the caller when it is called,
the caller will use that value to complete its task. Some methods will change
the internal state of the object, but will not return any value. Such methods
are called Mutators. The list methods insert(), extend(), append(), and sort()
are all mutators. Python however automatically returns the special value
None even when a method does not explicitly return a value.
Example:
>>> x=L.sort()
>>> print(x)
None

4
Python Programming (R19)

Aliasing and Side Effects


We know that numbers and strings are immutable. That is, you cannot
change their internal structure. However, because lists are mutable, you can
replace, insert, or remove elements. The mutable property of lists leads to
some interesting phenomena called ‘Aliasing’. When you assign a list object
to another list object both will be referring the same memory. The change in
one object would affect the other object as well. This is called ‘side affect due
to the aliasing’. To prevent aliasing, you can create a new object and copy
the contents of the original to it.

Equality: Object Identity and Structural Equivalence


The == operator returns True if the variables are aliases for the same object.
>>> x=2
>>> y=x
>>> x==y
True
Unfortunately, == also returns True if the contents of two different objects are
the same. The first relation is called object identity, whereas the second
relation is called structural equivalence. The == operator has no way of
distinguishing between these two types of relations.
>>> L1=[1,2,3]
>>> L2=[1,2,3]
>>> L1==L2
True
Using a List to Find the Median of a Set of Numbers
Researchers who do quantitative analysis are often interested in the median
of a set of numbers. For example, the U.S. Government often gathers data to
determine the median family income. Roughly speaking, the median is the
value that is less than half the numbers in the set and greater than the other
half. If the number of values in a list is odd, the median of the list is the value
at the midpoint when the set of numbers is sorted; otherwise, the median is
the average of the two values surrounding the midpoint. Thus, the median of

5
Python Programming (R19)

the list [1, 3, 3, 5, 7] is 3, and the median of the list [1, 2, 4, 4] is also 3. The
following script inputs a set of numbers from a text file and prints their
median:
Median.py Output
l=[] Enter the number of
s=int(input('Enter the number of elements:5
elements:')) Enter number:4
for i in range(s): Enter number:2
l.append(int(input('Enter number:'))) Enter number:3
l.sort() Enter number:1
print('Print sorted of elements :',l) Enter number:7
if s%2!=0: Print sorted of elements : [1, 2,
m=len(l)//2 3, 4, 7]
print('Median is :',l[m]) Median is : 3
else: Mean is : 3.4
m=len(l)//2
print('Median is:',(l[m]+l[m-1])/2)
tot=sum(l)
avg=tot/len(l)
print('Mean is :',avg)

Tuples
A tuple is a type of sequence that resembles a list, except that, unlike a list,
a tuple is immutable. You indicate a tuple literal in Python by enclosing its
elements in parentheses instead of square brackets. Creating the tuple is
similar to the List. The only difference is, the items or elements that are
separated by comma are enclosed within parenthesis.
Syntax:
T=() Empty tuple
T=(2) Tuple with one element
T=(1,’apple’,2.34) Tuple with different elements.
We can put a tuple inside another Tuple, which also known as Nested Tuple.
Tuple operations and methods
 We can find the length of the tuple using the len() function.
 We can use min(), max(), and sum() functions on Tuple and list also.
 We can perform concatenation operation between two tuples (t=t1+t2)
 We can use membership operator in and not in on the tuple.
 We can use for() loop to iterate over the tuple.

6
Python Programming (R19)

 We have two methods:


 T.index(element) → returns the index of the given element in the
tuple
 T.count(element) → Return the number of times the given
element present in the tuple
Advantages of tuples
 Tuples are immutable, that means once created they cannot be
changed. Hence, they can be used in write-protected data applications.
 Tuples can be used for format string.
 These are useful in representing the records or structures.
 Example: Student=(1, ‘raj’, ‘B.Tech’, 560, 93.3)

Sets
Set is another data structure in python. Sets are similar to the lists, with a
major difference that sets are lists with no duplicate entries. A set is created
by placing all the elements inside the curly brackets {}, separated by comma
operator.
Syntax:
S={item1,item2,item3,….itme n}
Example: S={1,2,4,5}
Operations on sets and methods
We can use min(), max(), and sum() functions on Tuple and list also. All the
set operations such as union, intersection, set difference, subset and superset
can be performed with various methods as Shown in the Table.

Methods Description
s.update(t) Adds elements of t to the set s
s.add(element) Adds element to the set
s.remove(element) Removes element from the set. Returns Error if the
element is not present
s.discard(element) Same as remove, but does not raise error if element is
not present.
s.pop() Removes an arbitrary element from the set
s.issuperset(t) Returns True if every element of t is present in set s.
s.intersection(t) Returns a new set that has elements common
elements of sets s and t
s.union(t) Returns a set s that elements from s and t
s.difference(t) Returns elements that are in s, but not in t
s.isdisjoint(t) Returns True if both sets are disjoint sets

7
Python Programming (R19)

Comprehension
Python includes more powerful advanced operation known as “list
comprehension expression”. Comprehension is powerful way to process the
structures like matrix. Comprehensions are used to create new_list by
running an expression on each item in a list or sequence from Left-to-Right.
Comprehensions can be written to list, tuple, sets, dictionaries and strings.
List comprehension
These are the ways to build new_list by applying an expression to each item
in the sequence.
Syntax: [ expression loop]
Example:

• Create a list which contains even numbers from 2 to 20.


• l=[i for i in range(1,21) if i%2==0]
• l=[i**2 for i in range(1,21) if i%2==0] #for squares
Write a comprehension for creating list of odd numbers form 1 to 20
od=[i for i in range(1,21) if i%2!=0]
List of numbers divisible by 5 from 2 to 40
se=[i for i in range(5,31) if i%5==0]
Set comprehension
Set comprehension runs a loop on a sequence and collects the result of an
expression on each iteration.
Syntax: S={expression loop}
s1={ i for i in [2,3,4,5] if i%2==0 }
Squares of each item in the sequence
s2={ i**2 for i in range(2,10) }

Note: Set arranges elements in arbitrary manner.


s3={x for x in "INDIA"}
Output
{'I', 'N', 'D', 'A'}
s1={i*4 for i in "PYT“}
Output
{'TTTT', 'PPPP', 'YYYY'}

8
Python Programming (R19)

Tuple comprehension
These are similar to the list comprehension, but produce an object instead of
building a new list on demand. This object needs to be converted into list to
see result.
Syntax
T=(expression loop)
Example:
t=(i for i in range(2,21) if i%2==0)
Output:
<generator object <genexpr> at 0x0222CF60>
Convert it to list
l=list(t)
print(l) #output [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
t1=(i**2 for i in [1,2,3])
<generator object <genexpr> at 0x0222CF90>
l1=list(t1)
Print(l1) # output [1, 4, 9]

Introduction to Functions
Definition – A Function is a block of program statements that perform single,
specific and well-defined task. Python enables it’s programmers to break the
program into functions, each of which has some specific task. When a
function is called, the program control is passed to the first statement in the
function definition. All the statements in the function are executed in sequence
and the control is transferred back to function call.
The function that calls another function is known as “Calling
Function”, and the function that is being called by another function is known
as “Called Function”.
Uses of Functions

• Functions can be used in another scripts to support code reusability.


• Defining our own functions will help to organize our code more
effectively.
• Function can help faster development.
• Programmers can test each function in a program individually, to
determine whether it is correctly performing its operation.

9
Python Programming (R19)

Syntax of Function Definition


To create a function we write its definition. Here is the general format of a
function definition in Python:

The first line is known as the function header. It marks the beginning of the
function definition. The function header begins with the key word def,
followed by the name of the function, followed by a set of parentheses with
parameters, followed by a colon (:). The function body contains one or more
statements. These statements are executed in sequence to perform the task
for which it is intended to define.
Example: square function

def square(x): #function Header


'''Performs the Square of a number'''
print('The square of {} is {}'.format(x,x**2))
#main function starts here
a=int(input('Enter any number :'))
square(a) #function call

Parameters and Arguments


A parameter is the name used in the function definition for an argument that
is passed to the function when it is called. The parameters are also called
Formal Parameters. The arguments are also called Actual Arguments. For
now, the number and positions of the arguments of a function call should
match the number and positions of the parameters in that function’s
definition. These are also called Positional Parameters. Some functions expect
no arguments, so they are defined with no parameters.
Various types of Arguments

• Positional Arguments
• Keyword Arguments
• Default Arguments
• Variable length Arguments

10
Python Programming (R19)

Positional Arguments
When a function is called arguments in the function call are assigned to the
parameters in the function definition. Actual arguments are assigned to
formal parameters according to their position in the order from left to the
right. We need to remember the position or order of the formal parameters,
otherwise arguments may be assigned in wrong way.

Keyword Arguments
When we call a function with some actual arguments, these are passed to the
formal parameters in the function definition based on their position (in the
order). Python also allows functions to be called using the keyword arguments
in which the position (order) of the arguments can be changed. The values are
not copied according to their position, but based on their name.
Syntax:
The actual arguments in the function call can be written as follow:
function_name(Arg_name1=value1,Arg_name2=value2)
An argument that is written according to this syntax is known as “Keyword
Argument”. Lets us understand this with an example.

def simpleinter(principal,time,rate): #function Header


i=(principal*time*rate)/100
print("The interest is:",i)
print("Total amount is:",principal+i)
#function call
Simpleinter(time=3, rate=7.25,principal=5000)

Default Arguments
Python allows functions to be called without or with less number of arguments
than that are defined in the function definition. The default value to an
argument is provided using the assignment (=) operator. If the same number
of arguments are passed then the default arguments are not considered. The
values of actual arguments are assigned or copied to formal arguments if
passed, default arguments are considered otherwise. Hence, the formal
arguments are overwritten by the actual arguments if passed.
Example:

def add(x=12,y=13,z=14):
t=x+y+z

11
Python Programming (R19)

print("The sum is:",t)


#function call without arguments
add()
#function call with one argument
add(1)
#function call with two arguments
add(10,20)

Variable-length arguments
In some situations, it is not known in advance how many number of
arguments have to be passed to the function.
In such cases, Python allows programmers to make function calls with
arbitrary (or any) number of arguments.
When we use arbitrary arguments or variable-length arguments, then the
function definition uses an asterisk ( *) before the formal parameter name.
Example:

#function definition
def var_len_arg(name,*args):
print ("\n name, “Hobbies are:”)
for x in args:
print(x)
#function call
var_len_arg("Suman","Cricket","Movies","Traveling")

The return Statement


The programmer places a return statement at each exit point of a function
when that function should explicitly return a value.
The syntax of the return statement is the following:
return <expression>
If any function is returning a value it is said to be Fruitful Function.
Boolean Functions
A Boolean function usually tests its argument for the presence or absence of
some property. The function returns True if the property is present, or False
otherwise.

12
Python Programming (R19)

def even(x):
if x%2==0:
return True
else:
return False
a=int(input('Enter any number:'))
#function call
b=even(a)
print(b)

Defining a main Function


In scripts that include the definitions of several cooperating functions, it is
often useful to define a special function named main that serves as the entry
point for the script. This function usually expects no arguments and returns
no value. Its sole purpose is to take inputs, process them by calling other
functions, and print the results. The definition of the main function and the
other function definitions can appear in no particular order in the script, as
long as main is called at the very end of the script.

def main():
a=int(input('Enter any number :'))
square(a)
def square(x):
'''Performs the Square of a number'''
print('The square of {} is {}'.format(x,x**2))
#main function calling
main()

Dictionaries
Lists, and tuples organize elements by position. This way of arranging
elements is useful when we want to fetch first, last and any other element in
the sequence. However, the position of the element in the sequence in some
applications is irrelevant. We are interested in association of an element with
some other element in the sequence. For example, looking up phone number
in the phonebook, we don’t care where it is present in the phonebook.
Dictionary Literals

13
Python Programming (R19)

• A dictionary organizes the information by association, not by position.


• In python, a dictionary associates with set of keys and data values.
• A dictionary is defined as a sequence of items separated by comma, and
each item is a pair of key and value separated by colon(:). The items are
enclosed in a pair of curly braces.
• Syntax:
o dict={key1:val1, key2:val2, key3:val3,…..,keyn:valn}
• We can even create empty dictionary as follow: dict={}
Adding Keys and Replacing Values
We can add new key/value pair to the dictionary using the subscript
operator. The form of this operation is the following:
<dictionary_name>[<key>]=<value>
Example:

fruits={} #empty dictionary


fruits['apple']=125 #adding key and value pair
fruits['mango']=300
fruits['orange']=250
print(fruits)

Note: The subscript is also used to replace a value at an existing key, as


follows:

fruits={}
fruits['apple']=125
fruits['mango']=300
fruits['orange']=250
print(fruits)
fruits['mango']=450 #replacing value at key

Accessing Values
You can also use the subscript to obtain the value associated with a key.
However, if the key is not present in the dictionary, Python raises an error.
Syntax:
>>> fruits['apple']
125

14
Python Programming (R19)

We can also use get() method of dictionary data structure for accessing
values as follow:
print(fruits.get(‘apple’,None)),
if the given key is present then it gives value, otherwise gives None.
Removing Keys
We can remove an entry or item from the dictionary using the pop() method
of the dictionary data structure. The pop() method takes key as an argument
and an optional default value as another argument. If the key is present it will
be removed an it value is returned. Otherwise default value will be returned.
If pop() is used with one argument, and key is absent then Python raises error.
fruits.pop('apple',None)
125
>>> fruits
{'mango': 450, 'orange': 250}
Traversing a Dictionary
When a for loop is used with a dictionary, the loop’s variable is bound to each
key in an unspecified order. Alternatively, you could use the dictionary
method items() to access a list of the dictionary’s entries or items. Syntax:
dob.items(), displays all the items as pair of key and value.

dob={'ravi':'12 oct 2002',


'radhika':'05 june 2004',
'rajeev':'28 dec 2003'}
for key in dob:
print('name is',key, 'Date of birth is',dob[key])

Some other methods of dictionary

• clear() : used to remove all the items from the dictionary.


• copy(): used to copy all the items to another dictionary.
• popitem(k,v): used to remove item with key and value
• keys(): used to get all the keys in a list
• values(): used to get all the values a list
• update(dictionary): used to update dictionary with given dictionary

Design with Functions

15
Python Programming (R19)

Design is important in many fields such as designing a building, designing a


bridge, designing a machine, and designing a military camp etc. The design
includes all the structure of the system and other coordinating components
within it. The design is also important in constructing the software systems,
some of which are complex.
Let us learn the use of functions in designing software systems.
Functions as Abstraction Mechanisms
Definition: Abstraction hides details and allow person to view many things
as just one thing.
We can write any program using the built-in operators and control
statements without using the function. But sometimes, as the program size
is increasing it would be very complex to understand, difficult to verify, and
maintain also.
The problem with human brain is, it can easily receive few things at
once. (3 things comfortably and at most 7 things with some stress). The people
cope with this complexity by developing a mechanism to simplify it or hide it.
This mechanism is called Abstraction.
This abstraction refers to most common tasks in our day-to-day life.
For example, if you as your mother what you are doing? She may
answer you ‘doing my laundry’.

This includes a complex process of:

• Fetching dirty clothes from hamper


• Separating them into white and colour clothes
• Loading them into washer
• Transferring them to dryer
• Folding and putting them into dresser
Abstraction makes our job of referring daily tasks simple.
Likewise, effective designers must develop useful abstractions to hide
complexity. There are various ways in which functions serve as abstraction
mechanisms in a program.

16
Python Programming (R19)

• Functions eliminate redundancy


• Functions hide complexity
• Functions Support General Methods with Systematic Variations
• Functions Support the Division of Labour.
Functions eliminate redundancy

• If the sum() function does not exist, we need to write the sum function
multiple times in our program where ever we need summation.
• In other words redundant code would appear in the program.
• This code redundancy is bad for several reasons.
• Copy the same code and paste again and again and correct it if required.
• If a modification is required then all the instances of same code needs
to corrected.
• By using the single function definition just in one place called module,
we can avoid redundant code.
• Any other module or program can then import function for its use for
any number of times.
• When a programmer needs to debug, repair, or improve the function,
he/she needs to edit and test single function definition.
Functions Hide Complexity

• This is another way how functions server as mechanism of abstraction.


• Although the function appears to be simple, but internal working of it
may not simple. For example, summation (low, upper)
• It contains many interacting components, such as variables that needs
to be updated for every iteration.

def summation(low,upper):
s=0
while low<=upper:
s=s+low
low=low+1
print('Sum is:',s)
#function call
summation(4,8)

Functions Support General Methods with Systematic


Variations

• An algorithm is a general method for solving a class of problems.


• The individual problems that make up a class of problems are known
as problem instances.

17
Python Programming (R19)

• The problem instances for our summation problem are numbers that
specify the lower and upper bounds of the range of numbers to be
summed.
• The problem instances may vary from program to program. For
example, summation(4,8), summation(12,20), summation(30,60).
• Here, the summation() functions provides both summation algorithm
and also problem instances.
• In other words, function is providing general method with systematic
variations.
Functions Support the Division of Labour
In a well-organized system, whether it is a living thing or something created
by humans, each part does its own job or plays its own role in collaborating
to achieve a common goal.
Specialized task are divided up and assigned to the specialized agents or
persons.
Some agents or persons may coordinate other tasks.
Good agents or persons can do their own business, and do not try to do job
of others.
A poorly organized system, by contrast, suffers from agents performing tasks
for which they are not trained or designed. Division of labor breaks down.
Functions can enforce a division of labor, which restricts each task
performing single, well defined, and special task.

Problem Solving with Top-Down Design


There are two design strategies:

• Top-down strategy - this starts from general to specific


• Bottom-up strategy - this starts from specific to general
Top-down strategy – starts with global view of the entire problem and breaks
problem into smaller, more manageable subproblems. This process is known
as Problem Decomposition. Example: C language
Bottom-up strategy - starts with specific modules and moves to the more
general solution. Example: C++ language, Java, Python
Problem decomposition may continue down to lower levels, when a
subproblem might in turn contain two or more lower-level problems to solve.
As functions are developed to solve each subproblem, the solution to the
overall problem is gradually filled out in the order. This process is also called
stepwise refinement.
The Design of the Text-Analysis Program

18
Python Programming (R19)

Let us try to explore the text-analysis program. This program requires


input and output components. These can be written in main function.
However, processing the input for getting the count of sentences, words,
syllables, and readability score is complex task.
Generally, we develop new function for each of these tasks. The
relationships among the functions in the design can be expressed using the
structure chart. The structure chart is diagram that shows the relationships
among functions and passage of data between them.

Structure chart for Text Analysis Program

Each box in the structure chart is labelled with a function name. The
main function at the top is where the design begins, and decomposition leads
us to the lower-level functions on which main depends. The lines connecting
the boxes are labelled with data type names, and arrows indicate the flow of

19
Python Programming (R19)

data between them. For example, the function countSentences takes a string
as an argument and returns the number of sentences in that string. Note that
all functions except one are just one level below main.
The Design of the Sentence-Generator Program
Here, you decompose the problem by simply following the grammar rules for
phrases. To generate a sentence, you generate a noun phrase followed by a
verb phrase, and so on. Each of the grammar rules presents a problem that
is solved by a single function.
Example: subject +verb +object (Simple present tense)

The Design of the Doctor Program (Doctor –Patient Conversation)

20
Python Programming (R19)

21
Python Programming (R19)

Design with Recursive Functions


In top-down design, you decompose a complex problem into a set of
simpler problems and solve these with different functions. In some cases, you
can decompose a complex problem into smaller problems of exactly the same
form. In these cases, the subproblems can all be solved by using the same
function. This design strategy is called recursive design, and the resulting
functions are called recursive functions.
Defining a Recursive Function:
A recursive function is a function that calls itself. To prevent a function from
repeating itself indefinitely, it must contain at least one selection
statement. This statement examines a condition called a base case to
determine whether to stop or to continue with another recursive step. Let us
try to learn how to convert an iterative algorithm into recursive function.
The task is to display the range of numbers from lower to upper.
Converting iterative algorithm into recursive function:

# Iterative algorithm
def displayrange(low,upper):
while low<=upper:
print(low)
low=low+1
#main function
l=int(input('Enter low value:'))
u=int(input('Enter upper value:'))
#Function Call
displayrange(l,u)

Two important facts to note:


1. The body of the loop executes when the low<=upper.
2. When the function executes, it increments low by 1.

Steps to convert into recursive function


1. Replace loop with selection statement.
2. Replace the assignment with recursive call

22
Python Programming (R19)

def displayrange(low,upper):
if low<=upper:
print(low)
displayrange(low+1,upper)
#main function
l=int(input('Enter low value:'))
u=int(input('Enter upper value:'))
#Function Call
displayrange(l,u)

Another example for summation- summation(low , upper)

Tracing a Recursive Function

23
Python Programming (R19)

Using Recursive Definitions to Construct Recursive Functions


 Recursive functions are frequently used to design algorithms for
computing values that have a recursive definition. (Factorial, Fibonacci)
 A recursive definition consists of equations that state what a value is
for one or more base cases and one or more recursive cases.
 For example, the Fibonacci sequence is a series of values with a
recursive definition.
 The first and second numbers in the Fibonacci sequence are 1.
 Thereafter, each number in the sequence is the sum of its two
predecessors, as follows: 1 1 2 3 5 8 13 . . .
 Fib(n)=1, when n=1 or n=2
Fib(n)=Fib(n-1)+Fib(n-2) for n>2

Infinite Recursion
If a function continues to execute forever, this situation is known as
Infinite Recursion. Infinite recursion arises when the programmer fails to
specify the base case or to reduce the size of the problem in a way that
terminates the recursive process. The Python virtual machine keeps calling
runforever(1) until there is no memory left to support another recursive call.
Unlike an infinite loop, an infinite recursion eventually halts execution with
an error message.

24
Python Programming (R19)

The Costs and Benefits of Recursion


 This is a nice solution to the natural problem that have recursive nature.
 At program startup, the PVM reserves an area of memory named a call
stack.
 For each call of a function, the PVM must allocate on the call stack a small
chunk of memory called a stack frame.
 In this type of storage, the system places the values of the arguments and
the return address for the particular function call.
 When a call returns or completes its execution, the return address is used
to locate the next instruction in the caller’s code, and the memory for the
stack frame is deallocated.
The stack frames for the process generated by displayRange(1, 3) are shown
in Figure 6.4.

def displayrange(low,upper):
if low<=upper:
print(low)
displayrange(low+1,upper)
#main function
l=int(input('Enter low value:'))
u=int(input('Enter upper value:'))
#Function Call
displayrange(l,u)

The stack frames for the process generated by displayRange(1, 3) are shown
in Figure:
• Although this sounds like a complex process, the PVM handles it easily.
• However, when a function invokes hundreds or even thousands of
recursive calls, the amount of extra resources required, both in
processing time and in memory usage, can add up to a significant
performance hit.
• When, because of a design error, the recursion is infinite, the stack
frames are added until the PVM runs out of memory, which halts the
program with an error message.

Case Study: Gathering Information from a File System


We have two types of file systems to gather information as follow:
1. Graphical User Interface based file system
2. Command prompt based file system

25
Python Programming (R19)

Graphical User Interface based file system:

• Modern file systems come with a graphical browser, such as Microsoft’s


Windows Explorer or Apple’s Finder.
• These file systems allow user to select the appropriate folder or file from
the current working directory (CWD).
Command prompt based file system:

• The users of terminal-based purely depend on the commands at the


terminal to perform these operations.
• In the process, we will have an opportunity to exercise some skills in
top-down design and recursive design.

The program we will design in this case study is named files.py. It


provides some basic browsing capability, as well as options that allow you to
search for a given filename and find statistics on the number of files and
their size in a directory. At program startup, the current working directory
(CWD) is the directory containing the Python program file(files.py). The
program should display the path of the CWD, a menu of command options,
and a prompt for a command.

26
Python Programming (R19)

In the analysis process we identify the requirements. In the design


process we list the different tasks. And in the implementation we actually
implement the request using the programming language. Here in this we have
used python programming language to implement the requested problem.
Implementation: files.py

'''
files.py
author: Dr.KSR
Purpose: Menu-driven tool for navigating a file system.
'''
import os
QUIT='7'
COMMANDS=('1','2','3','4','5','6','7')
MENU="""
1 List the current working directory
2 Move up
3 Move down
4 Number of files in the directory
5 Size of directory in bytes
6 Search for a file
7 Quit the program"""
def main():
while True:

27
Python Programming (R19)

print(os.getcwd()) #1 listing all the files in the directory


print(MENU) #display menu
command=acceptcommand() #processing the commands
runcommand(command)
if command==QUIT:
print('Have a nice day')
break
def acceptcommand():
''' input and return correct command number'''
command=input('Enter command')
if command not in COMMANDS:
print('Enter correct command')
else:
return command
def runcommand(command):
if command=='1':
listcurrentdir(os.getcwd())
elif command=='2':
moveup()
elif command=='3':
movedown(os.getcwd())
elif command=='4':
print('Total number of files')
countfiles(os.getcwd())
elif command=='5':
print('Total number of Bytes')
countbytes(os.getcwd())
elif command=='6':
target=input('Enter the search string:')
filelist=findfiles(target,os.getcwd())

28
Python Programming (R19)

if not filelist:
print('File not found')
else:
for f in filelist:
print(f)
else:
print('Please choose correct command')
def listcurrentdir(dirname):
l=os.listdir(dirname)
for item in l:
print(item)
def moveup():
os.chdir('..')
def movedown(currentdir):
newdir=input('Enter subdirectory name:')
if os.path.exists(currentdir+os.sep+newdir) and os.path.isdir(newdir):
os.chdir(newdir)
else:
print('Error message: no such file')
def countfiles(path):
''' returns the number of file in the cwd and subdirectories'''
count=0
l=os.listdir(path)
for item in l:
if os.path.isfile(item):
count=count+1
else:
os.chdir(item)
count=count+countfiles(os.getcwd()) #recursion
os.chdir('..')

29
Python Programming (R19)

return count
def countbytes(path):
count=0
l=os.listdir(path)
for item in l:
if os.path.isfile(item):
count=count+os.path.getsize(item)
else:
os.chdir(item)
count=count+countbytes(os.getcwd()) #recursion
os.chdir('..')
return count
def findfiles(target,path):
files=[]
l=os.listdir(path)
for item in l:
if os.path.isfile(item):
if target in item:
files.append(item)
else:
os.chdir(item)
files.extend(findfiles(target,os.getcwd()))
os.chdir('..')
return files
#main function
main()

Managing a Program’s Namespace


Normally when we need to give name to a variable, we often choose
name that reflects its purpose. These variable names are meaningful to us

30
Python Programming (R19)

only, but not to the computer. The computer considers a variable as name
given to value stored in the computer memory. Let us learn how a program’s
name space which includes set of variables and their values are structured in
the program.
The variable in the program falls under any one of the following three
categories:

• Module variables –When module variables are used in a program,


they are immediately given values.

• Parameters- A parameter name behaves like a variable and is


introduced in a function or method header. The parameter does
not receive a value until the function is called.

• Temporary variable - Temporary variables receive their values as


soon as they are introduced.
The use of function definitions brought parameters and temporary
variables into play. When these variables are used in function definition scope
and lifetime also play major role.
Scope - The scope determines the parts of the program where you could use
or access that name.

• In general, the meanings of temporary variables are restricted to the


body of the functions in which they are defined, and are invisible
elsewhere in a module.
• Like temporary variables, parameters are invisible outside the function
definitions where they are defined.
• The module variables can be accessed to any part of the program and
by any function.

Lifetime - A variable’s lifetime is the period of time a variable exists during


the execution of program.

• When a variable comes into existence, storage is allocated for it; when
it goes out of existence, storage is reclaimed by the PVM.
• Module variable lifetime will be throughout the program execution.
• Parameters and temporary variables come into existence when function
is called, but go out of existence when the function call terminates.

31
Python Programming (R19)

The module variable can be accessed to any part of the program.

Higher-order Functions
 As we gain experience in writing the programs, we try to identify the
common and redundant patterns in the code.
 One common pattern that occurs again and again is applying the
function to a set of data values to produce some results.
 Examples
 Converting all the numbers in text file or list to integers or floats
 Putting grades in a list whose score is above average.
 Computing sum of squares of a list of numbers.
 Capturing these patterns in a new abstraction is called ‘Higher-
ordered function’.

32
Python Programming (R19)

 Higher-order function expects a function and set of data values as


arguments.
 The argument function is then applied on the set of data values which
returns set of values or a single value as result.
 Higher-order function separates the task of transforming each data
value from the logic of accumulating the result.
Functions as First-Class Data Objects
 In Python, functions can be treated as first-class data objects.
 This means that they can be assigned to variables, passed as
arguments to other functions, returned as the values of other
functions, and stored in data structures such as lists and dictionaries.
 We will see some simpler possibilities:

>>> import math


>>> abs
>>> func=[abs,math.sqrt]
<built-in function abs>
>>> func[0](-10)
>>> f=abs
>>> func[0](-10)
>>> f(-4)
10
4
>>> func[1](25)
5.0

Three types of higher-order functions


 Mapping
 Filtering
 Reducing
 Mapping- This is the first type of useful higher-order function. It
applies function argument to a sequence of data values and returns
new sequence of results. Python includes a map function for this
purpose. Suppose we have a list named words that contains strings that
represent integers. We can replace each string with its corresponding
integer value using the map function.
>>> words=['1','123','100','567']
>>> l=list(map(int,words))

33
Python Programming (R19)

>>> l
[1, 123, 100, 567]
 Filtering -This is the second type of Higher-order function. In this
process, a function called a Predicate or Condition is applied to each
value in a list. If the predicate returns True, the value passes the test
and is added to a filter object like list. Otherwise, the value is dropped
from consideration. Python includes a filter function to perform this
task.
>>>def odd(n): return n%2==1
>>>l=list(filter(odd , range(1,10)))
>>> l
[1, 3, 5, 7, 9]

 Reducing- This is the third type of Higher-order function. Here we take


a list of values and repeatedly apply a function to accumulate a single
data value. A summation is a good example of this process. Where the
first value is added to the second value, then the sum is added to the
third value, and so on, until the sum of all the values is produced. The
Python functools module includes a reduce function that expects a
function of two arguments and a list of values. The reduce function
returns the result by applying the function as just described above.
>>> from functools import reduce
>>> def add(x,y):return (x+y)
>>> def mul(x,y):return (x*y)
>>> data=[1,2,3,4,5]
>>> reduce(add , data)
15
>>>

Using lambda to Create Anonymous Functions


Python includes a mechanism called lambda that allows the programmer
to create function without name. A lambda is an anonymous function. It has
no name of its own, but contains the names of its arguments as well as a
single expression. When the lambda is applied to its arguments, its expression
is evaluated, and its value is returned.
The syntax of a lambda is very tight and restrictive:

34
Python Programming (R19)

variable=lambda arg1,arg2: expression #here expression is arithmetic


expression

>>> sub=lambda x,y: x-y


>>> sub(12,3)
9

Note: These functions can be used in the scope where they have been defined,
once the scope is ended we cannot reuse them.

Modules
A module is a file that contains Python code. This code contains functions and
variables that perform related tasks. This approach is called “Modularization”. This makes the
program easier to understand, test and maintain.
Modules also make it much easier to reuse same code in more than one program. If we
have written set of functions that are needed in several different programs, we can place them
in modules. Then we can import these modules in each program to call one of the functions.

Creating Modules
For example we use the functions such as area of circle, circumference of circle, area
of rectangle and circumference of rectangle in many programs. So we can create two modules
such as: circle and rectangle, and put the functions related to the circle in that module and
functions related to the Rectangle in another module called rectangle. We can just import these
modules into any number of programs if their functions are needed.
Rules for Module Names:
✓ A module file name should end with .py. If it is not ended with .py we cannot import it
into other programs.
✓ A module name cannot be keyword.
✓ The module must be saved within the same folder (directory) where you are accessing
it.

Import statement - Accessing these modules


To use these modules in program, we import them in program with import statement.
To import the circle module, we write it as follow:
import circle

When the Python interpreter reads this statement it will look for the file circle.py in the
same folder as the program that is trying to import it. If it finds the file it will load it into
memory. If it does not find the file, an error occurs.

Example program: circle.py module


1. Creating the Module

35
Python Programming (R19)

#definition of area function


def area(r):
a=3.14*r*r
return(a)
#definition of circumference
def circum(r):
c=2*3.14*r
return(c)
2. Using the Module in another program
testcircle.py

import circle
x=float(input("Enter the Radius:"))
#function call to area
res=circle.area(x)
print ("The area of the Circle is:",res)
#function call to circum
res=circle.circum(x)
print ("The Circumference of the Circle is:",res)

from. Import statement

There are Four ways of using import to import a module and subsequently use what was
imported. We will use the math module to illuminate:

# Way 1
import math
print math.cos(math.pi/2.0)

This method imports everything from the math module, and members of the module are
accessed
using dot notation.

#way 2
from math import cos
print cos(3.14159265/2.0)

This method imports only the dentition of cos from the math library. Nothing else is
imported.

#way 3
from math import cos, pi
print cos(pi/2.0)

36
Python Programming (R19)

This method imports only definitions of cos and pi from the math library. Nothing else is
imported.

#way 4
from math import *
print cos(pi/2.0)

This method also imports everything from the math module, the difference being that dot
notation is not needed to access module members.

Example Program for 4th way

circle.py module
#definition of area function from circle import*
def area(r): x=float(input("Enter the Radius:"))
a=3.14*r*r #function call to area
return(a) res=area(x)
#definition of circumference print ("The area of the Circle is:",res)
def circum(r): #function call to circum
c=2*3.14*r res=circum(x)
return(c) print ("The Circumference of the Circle
is:",res)

Knowing the current module name


If we want to know the name of the current module, then we can use the attribute
“__name__” to print the name of the module with help of the print() function.
Example:

Print “The module name is:”,__name__

Namespacing
A namespace is a syntactic container which permits the same name to be used in
different modules or functions. Each module determines its own namespace, so we can use the
same name in multiple modules without causing an identification problem.

For example, functions such as area(r) and circum(r) are part of the module circle.py.
We can also use same names in another module like “cir.py”. These functions are called from
these module independently.

mod1.py testans.py
question="What is your name" import mod1
ans="Write your answer" import mod2
mod2.py q=mod1.question
print( "Question is:",q)
a=mod1.ans
print ("Answer is:",a)

q=mod2.question

37
Python Programming (R19)

print ("Question is:",q)


a=mod2.ans
print ("Answer is:",a)
question="What is your name" Question is: What is your name
ans="Guido Van Rossum" Answer is: write your name
Question is: What is your name
Answer is: Guido Van Rossum

Standard Modules
 Python’s standard library is very extensive, offering a wide range of
facilities as indicated by the long table of contents listed below.
 The library contains built-in modules (written in C) that provide access
to system functionality such as file I/O that would otherwise be
inaccessible to Python programmers, as well as modules written in
Python that provide standardized solutions for many problems that
occur in everyday programming.
 Some of these modules are explicitly designed to encourage and
enhance the portability of Python programs by abstracting away
platform-specifics into platform-neutral APIs.
 More than 200 core modules sit at the heart of the standard library.
But in addition to this library, you can also access a growing collection
of several thousand components from the Python Package Index (PyPI).
 Some modules are as follows:
✓ numbers — Numeric abstract base classes
✓ math — Mathematical functions
✓ cmath — Mathematical functions for complex numbers
✓ decimal — Decimal fixed point and floating point arithmetic
✓ fractions — Rational numbers
✓ random — Generate pseudo-random numbers
✓ statistics — Mathematical statistics functions

38
Python Programming (R19)

Explore the statistics module


 This module provides functions for calculating statistics of data,
including averages, variance, and standard deviation.
Function Description
=============================================
mean Arithmetic mean (average) of data.
harmonic_mean Harmonic mean of data.
median Median (middle value) of data.
median_low Low median of data.
median_high High median of data.
median_grouped Median, or 50th percentile, of grouped data.
mode Mode (most common value) of data.
variance used to calculate variance of the data
stddev Used to calculate standard deviation of the data

>>> import statistics


>>> l=[12,3,3,4,5,6,9,10]
>>> statistics.mean(l)
6.5
>>> statistics.mode(l)
3
>>> statistics.median_low(l)
5
>>> l.sort()
>>> l
[3, 3, 4, 5, 6, 9, 10, 12]
>>> statistics.median_high(l)
6
v=statistics.variance(l,m)
>>> v
11.714285714285714

39
Python Programming (R19)

Packages
 When the number of modules (simply stated, a module might be just a
file containing some classes) in any project grows significantly, it is
wiser to organize them into packages – that is, placing functionally
similar modules/classes in the same directory.
 Steps to Create a Python Package
 Working with Python packages is really simple. All you need to
do is:
 Create a directory and give it your package's name.
 Put your modules in it.
 Create a __init__.py file in the directory
 That's all! In order to create a Python package, it is very easy. The
__init__.py file is necessary because with this file, Python will
know that this directory is a Python package directory other than
an ordinary directory.

Importing the package


 We can write another file “testrect.py” that really uses this package.
 Put this file outside the “ksr” directory, but in the “python programs”
directory as shown in the screenshot.

40
Python Programming (R19)

Introduction to PIP
So far we have been working with package that we have created and is in our system.
There may be situation where we want to install packages that are not in our system and created
by others residing in other system. This can be done using the PIP tool.

The Python Package Index is a repository of software for the Python programming
language. There are currently1,25,120 packages here. If we need one package we can
download it from this repository and install in our system. This can be performed using the PIP
tool.

41
Python Programming (R19)

The PIP command is tool for installing and managing the packages, such as found in
Python Packages Index repository (www.pypi.python.org). To search for a package say
numpy, type the following:

Installing Packages via PIP


To install a new package we can use the pip tool.
• To do this, first go to the path where Python software is installed and select the folder
‘Scripts” which contains pip tool.
• Use this path in the command prompt and type the pip command.
• The requested package is downloaded from the internet from the www.pypi.python.org
website.
• So you must have internet connection when you want to download, error will be raised
otherwise.
• To install package we write it as follow: pip install numpy in the command prompt.

To uninstall package we can use the pip tool as follow: pip uninstall numpy in the command
prompt.

Using Python numpy Packages


Write a python program using numpy to add two matrices?

42
Python Programming (R19)

Matrix addition is performed using element-by-element manner.

import numpy as np

m1=np.array([[1,2,3],[4,5,6],[7,8,9]])

m2=np.array([[1,2,3],[4,5,6],[7,8,9]])

mat_add=np.add(m1,m2)

print(mat_add)

Output:

[[ 2 4 6]

[ 8 10 12]

[14 16 18]]

Write a python program to perform subtraction between two matrices?

import numpy as np

m1=np.array([[3,4,5],[14,15,16],[17,18,19]])

m2=np.array([[1,2,3],[3,2,1],[5,6,7]])

mat_sub=np.subtract(m1,m2)

print('matrix subtraction is\n',mat_sub)

Output:

matrix subtraction is

[[ 2 2 2]

[11 13 15]

[12 12 12]]

Write a python program to perform matrix multiplication (element-by-elelment)

import numpy as np

m1=np.array([[1,2,3],[4,5,6],[7,8,9]])

m2=np.array([[1,2,3],[3,2,1],[5,6,7]])

mat_mul=np.multiply(m1,m2)

print('matrix mul is\n',mat_mul)

matrix mul is using element-by-element

Output:
[[ 1 4 9]
[12 10 6]
[35 48 63]]

43
Python Programming (R19)

Write a python program to perform matrix multiplication .

import numpy as np

m1=np.array([[1,2,3],[4,5,6],[7,8,9]])

m2=np.array([[1,2,3],[3,2,1],[5,6,7]])

mat_mul=np.dot(m1,m2)

print('matrix mul is\n',mat_mul)

Output:

matrix mul is
[[22 24 26]
[49 54 59]
[76 84 92]]

Write a program that uses the numpy package.


celfah.py
import numpy #numpy package
l=[12.34,45.56,68.78,98.78]
a=numpy.array(l) #numpy array() function
print("Temp in 'C' are:",a)
f=(a*9/5+32)
print("The temp in 'F' are:",f)
Output:

------------------------------End of 3rd Unit----------------------

44

You might also like