QP CODE 213993
SEM-II
Subject: Programming with Python Time: 02.00 Hours
Max. Marks: 60 Date: 15-09-2022
N.B 1. Q.1 is compulsory Subject Code:EC111
2. Attempt any two from the remaining three questions
Q.1. Attempt all M BT CO
a) 5 L2 CO1
What are Lambda and Map function in Python?
Python Lambda Functions are anonymous function means that the
function is without a name. As we already know that the def keyword is
used to define a normal function in Python. Similarly,
the lambda keyword is used to define an anonymous function in Python.
Example:
def lambda_cube(y): return y*y*y
print("Using lambda function, cube:", lambda_cube(5))
b) 5 L2 CO3
The map() function in Python takes in a function and a list as an
argument. The function is called with a lambda function and a list and a
new list is returned which contains all the lambda modified items returned
by that function for each item.
Example:
li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61]
final_list = list(map(lambda x: x*2, li))
print(final_list)
Write a short note on Dictionary data type in Python.
Dictionary in Python is a collection of keys values, used to store data
values like a map, which, unlike other data types which hold only a single
c) value as an element. 5 L2 CO2
In Python, a dictionary can be created by placing a sequence of elements
within curly {} braces, separated by ‘comma’. Dictionary holds pairs of
values, one being the Key and the other corresponding pair element being
1
QP CODE 213993
its Key:value. Values in a dictionary can be of any data type and can be
duplicated, whereas keys can’t be repeated and must be immutable.
Example of Dictionary in Python
Dictionary holds key:value pair. Key-Value is provided in the dictionary
to make it more optimized.
Dict = {1: 'Geeks', 2: 'For', 3: 'Geeks'}
print(Dict)
Explain nested while loop using suitable example.
Python while loop is just another Python statement. As you already know
that while loop body can contain statements, we can write while loop
inside while loop. While loop inside another while loop is called Nested
While Loop.
d) Syntax: 5 L3 CO5
while condition_1 :
#statement(s)
while condition_2 :
#statement(s)
Example:
Q.2. Attempt all
Write a python program to check whether a string is palindrome or not.
def isPalindrome(s):
rev = ''.join(reversed(s))
if (s == rev):
return True
return False
a) 4 L5 CO2
s = "malayalam"
ans = isPalindrome(s)
if (ans):
print("Yes")
else:
print("No")
Difference between Array and list in Python.
b) List Array 4 L3 CO3
Can consist of elements Only consists of elements
belonging to different data types belonging to the same data type
2
QP CODE 213993
No need to explicitly import a Need to explicitly import a
module for declaration module for declaration
Cannot directly handle arithmetic Can directly handle arithmetic
operations operations
Can be nested to contain Must contain either all nested
different type of elements elements of same size
Preferred for shorter sequence of Preferred for longer sequence of
data items data items
Greater flexibility allows easy Less flexibility since addition,
modification (addition, deletion) deletion has to be done element
of data wise
The entire list can be printed A loop has to be formed to print
without any explicit looping or access the components of array
Consume larger memory for easy Comparatively more compact in
addition of elements memory size
c) Design a class that store the information of students and display the same.
class student:
def __init__(self, name, rnum, percen):
self.name = name
self.rnum = rnum
self.percen = percen
def displayinfo(self):
print ("Student name :",self.name," \n student Roll number : ",self.rnum,
6 L6 CO4
”Student percentage :”,self.percen)
print ("Details of students \n")
for i in range(5):
x=input(“Enter student name”)
y=int(input(“Enter roll number”))
z=float(input(“Enter percentage”))
st = student(x,y,z)
st.displayinfo()
d) What are operators? Describe identity and membership operators.
Operators are special symbols in Python that carry out arithmetic or
logical computation. The value that the operator operates on is called the 6 L2 CO1
operand.
Identity operators:
3
QP CODE 213993
Identity operators are used to comparing the objects if both the objects are
actually of the same data type and share the same memory location.
There are different identity operators such as
‘is’ operator – Evaluates to True if the variables on either side of the
operator point to the same object and false otherwise.
Example:
is not’ operator – Evaluates to false if the variables on either side of the
operator point to a different object and true otherwise.
Example:
Membership operators:
Python offers two membership operators to check or validate the
membership of a value. It tests for membership in a sequence, such as
strings, lists, or tuples.
in operator: The ‘in’ operator is used to check if a character/ substring/
element exists in a sequence or not. Evaluate to True if it finds the
specified element in a sequence otherwise False.
Example:
not in’ operator- Evaluates to true if it does not finds a variable in the
specified sequence and false otherwise.
Example:
Q.3. Attempt all
Write a python program to implement multiple inheritances.
Multiple inheritances: When a child class inherits from multiple parent classes,
it is called multiple inheritances.
Example:
class Base1:
def __init__(self):
self.str1 = "Geek1"
print("Base1")
class Base2:
def __init__(self):
self.str2 = "Geek2"
a) print("Base2") 4 L3 CO4
class Derived(Base1, Base2):
def __init__(self):
Base1.__init__(self)
Base2.__init__(self)
print("Derived")
def printStrs(self):
print(self.str1, self.str2)
ob = Derived()
ob.printStrs()
4
QP CODE 213993
What are the different operations that can be performed on a list?
Explain Different operations with list:
concatenation
b) 4 L2 CO5
repetition
slicing
indexing
c) Explain tuples in Python? How it is different than list?
Tuple
List
1 Lists are mutable Tuples are immutable
The implication of
iterations is The implication of iterations is
2 Time-consuming comparatively Faster
The list is better for
performing operations,
such as insertion and Tuple data type is appropriate for 6 L3, L4 CO2
3 deletion. accessing the elements
Lists consume more Tuple consumes less memory
4 memory as compared to the list
Lists have several Tuple does not have many
5 built-in methods built-in methods.
The unexpected
changes and errors are
6 more likely to occur In tuple, it is hard to take place.
d) Describe the different access modes of the files with an example.
There are 6 access modes in python.
1. Read Only (‘r’) : Open text file for reading. The handle is positioned
at the beginning of the file. If the file does not exists, raises the I/O
error. This is also the default mode in which a file is opened.
6 L3 CO6
2. Read and Write (‘r+’): Open the file for reading and writing. The
handle is positioned at the beginning of the file. Raises I/O error if the
file does not exist.
3. Write Only (‘w’) : Open the file for writing. For the existing files, the
data is truncated and over-written. The handle is positioned at the
beginning of the file. Creates the file if the file does not exist.
5
QP CODE 213993
4. Write and Read (‘w+’) : Open the file for reading and writing. For an
existing file, data is truncated and over-written. The handle is
positioned at the beginning of the file.
5. Append Only (‘a’): Open the file for writing. The file is created if it
does not exist. The handle is positioned at the end of the file. The data
being written will be inserted at the end, after the existing data.
6. Append and Read (‘a+’) : Open the file for reading and writing. The
file is created if it does not exist. The handle is positioned at the end
of the file. The data being written will be inserted at the end, after the
existing data.
Q.4. Attempt all
Define Method Overloading in Python.
Method Overloading is an example of Compile time polymorphism. In
this, more than one method of the same class shares the same method
name having different signatures. Method overloading is used to add
more to the behavior of methods and there is no need for more than one
class for method overloading.
Method overloading is not available in python. writing more than one
method with the same anime is not possible in python. so, we can achieve
method overloading by writing the same name with several parameters.
a) class Myclass: 4 L2 CO4
def sum(self, a=None, b= None, c=None):
if(a!=None and b!=None and c!=None):
print(“Sum of three”, a+b+c)
elif( a!=None and b!=None):
print(“Sum of two ”, a+b)
else:
print(“Please enter two or three arguments”)
m=Myclass()
m.sum(5,7,8)
m.sum(7, 4)
Explain Exception Handling in Python.
An exception is an event, which occurs during the execution of a program
that disrupts the normal flow of the program's instructions. In general,
when a Python script encounters a situation that it cannot cope with, it
raises an exception. An exception is a Python object that represents an
error.
When a Python script raises an exception, it must either handle the
b) exception immediately otherwise it terminates and quits. 4 L4 CO5
Exception Handling
If you have some suspicious code that may raise an exception, you can
defend your program by placing the suspicious code in a try: block. After
the try: block, include an except: statement, followed by a block of code
which handles the problem as elegantly as possible.
6
QP CODE 213993
c) What is recursive function? Write a Python program to calculate factorial
of a number using Recursive function?
Recursion is a method of programming or coding a problem, in which a
function calls itself one or more times in its body. Usually, it is returning
the return value of this function call. If a function definition satisfies the
condition of recursion, we call this function a recursive function.
def fact(num): 6 L3 CO3
if num==0 or num==1:
return 1
else:
return num*fact(num-1)
N=int(input(“Enter number”)
print(“Factorial of entered number is “ fact(N))
d) What is the use of continue and break statements? Write a program to
check whether a number is prime or not.
The break statement terminates the loop containing it. Control of the
program flows to the statement immediately after the body of the loop.
The continue statement is used to skip the rest of the code inside a loop
for the current iteration only. Loop does not terminate but continues on
with the next iteration.
num=int(input(“Enter number to check”)
flag=0 6 L3, L5 CO1
i=2
while(i<num):
if(num%i==0);
flag==1
break
if (flag==0):
print ( "Enter number is prime number")
else:
print ("Enter number is not prime number")
CO1- Understand the structure, syntax of the Python language.
CO2- Interpret varied data types in python
CO3- Implement arrays and functions
CO4-Illustrate the concepts of object-oriented programming as used in Python.
CO5-Create Python applications using modules, packages, multithreading and exception handling
CO6-Gain proficiency in writing File Handling programs
BT Levels: - 1 Remembering ,2 Understanding, 3 Applying,4 Analyzing, 5 Evaluating, 6 Creating.
M-Marks, BT- Bloom's Taxonomy, CO-Course Outcomes.