Directorate of Technical Education,
Guindy, Chennai – 25
BOARD EXAMINATION – April 2023
SOLUTION AND SCHEME OF EVALUATION
Course Name & Code : CE
Subject Code : 4052510
Subject Name : Python Programming
Scheme : N Scheme Qp.
Code : 342
Prepared By
Mrs. S.Ahalya,
GPT, Coimbatore,
Coimbatore – 14
(Contact No: 9443522450)
QP Code: 342
Python Programming
Scheme of Evaluation
Q No Mark of Evaluation Remarks
PART A
10*3 = 30 Marks
1 3 marks for explanation with example
2 3 marks for explanation
3 3 marks for program
4 3 marks for explanation
5 3 marks for explanation
6 3 marks for explanation
7 3 marks for explanation
8 3 marks for explanation
9 3 marks for Program
10 3 marks for explanation
PART B
5*14 = 70 Marks
11. a 7 marks for Method1 + 7 marks for Method 2
11.b.i) 7 marks for explanation
11.b.ii) 7 marks for explanation
12.a.i) 7 marks for explanation with example
12.a.ii) 7 marks for explanation with example
12.b.i) 7 marks for program
12.b.ii) 7 marks for explanation with example
13.a.i) 7 marks for program
13.a.ii) 7 marks for explanation with example
13.b.i) 7 marks for explanation + example
13.b.ii) 7 marks for explanation + example
14.a 14 marks for function
14.b 7 marks for methods + 7 marks for example
15.a 7 marks for attributes + 7 marks for example
15.b 7 marks for method + 7 marks for explanation
PART A
1.What is meant by reserved keywords?
The Python language reserves a small set of keywords that designate special language
functionality. No object can have the same name as a reserved word.
Eg: lambda, pass
2. What is meant by data type conversion? Give example.
Python defines type conversion functions to directly convert one data type to another which is
useful in day-to-day and competitive programming.
There are two types of Type Conversion in Python:
1. Implicit Type Conversion
2. Explicit Type Conversion
3. What is the output of the following function? range(5,12).
OUTPUT: 5,6,7,8,9,10,11
4. How is a function called? Example.
To call a function, use the function name followed by parenthesis. Information can be passed
into functions as arguments.
def my_function():
print("Hello from a function")
my_function()
5. Which character is used as escape character? List some of them.
To insert characters that are illegal in a string, use an escape character.
\' - Single Quote
\\ - Backslash
\n - New Line
6. What is meant by mutable property?
Whenever an object is instantiated, it is assigned a unique object id. The type of the object is
defined at the runtime and its state can be changed if it is a mutable object.
7. How does the repetition operator work on tuples?
The * symbol is commonly used to indicate multiplication, however, it becomes the repetition
operator when the operand on the left side of the * is a tuple. The repetition operator
duplicates a tuple and link all of them together.
num_tuple = (10, 20, 30) * 2
print(num_tuple)
10 20 30 10 20 30
8. Give an example for all(dict) and any(dict).
The Python all() function returns true if all the elements of a given iterable (Dictionary) are
True otherwise it returns False.
The any() function returns True if any item in an iterable are true, otherwise it returns False.
9. Give the syntax for deleting a file on Python with an example.
To delete a file, you must import the OS module, and run its os.remove() function
import os
if os.path.exists("demofile.txt"):
os.remove("demofile.txt")
else:
print("The file does not exist")
10. What are the user defined exceptions? Give an example.
Python throws errors and exceptions when the code goes wrong, which may cause the
program to stop abruptly. Python also provides an exception handling method with the help
of try-except.
class CustomError(Exception):
pass
raise CustomError("Example of Custom Exceptions in Python")
PART B
11. A. What are the two ways of getting input from the keyboard? Explain in detail with
examples. (7 marks for Method1 + 7 marks for Method 2)
There are two functions that can be used to read data or input from the user in python:
raw_input() and input(). The results can be stored into a variable.
raw_input() – It reads the input or command and returns a string.
input() – Reads the input and returns a python type like list, tuple, int, etc.
name = raw_input (“what is your name?”) # return type of raw input is always string
age = input (“what is your age ”) # This can be different from string
print “user entered name as: ” + name
print “The type of the name is: ”,
print type (name)
print “user entered age as: ” + str (age)
print “The type of age is: ”,
print type (age)
11.b.i Describe with example about the immutable objects. ( 7 marks for explanation )
In Python programming language, whenever an object is susceptible to internal change or has
the ability to change its values is known as a mutable object. Mutable objects in Python are
generally changed after creation.
For mutable objects in Python, their values can be changed but the identity of the object
remains intact. The objects in Python which are mutable are provided below:
● Lists
● Dicts
● Sets
11.b.ii What are the various set operations performed in Python? Explain with an example. (
7 marks for explanation)
A set has optimized functions for checking whether a specific element is a member of the set
or not. The numbers 2, 4, and 6 are distinct objects when considered separately, but when
they are considered collectively they form a single set of size three, written {2,4,6}.
Set operators: Union, Intersect, Difference, Complement.
12.a. Write notes on for loop with an example. (7 marks for explanation with example)
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.
Example:
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
"apple", "banana", "cherry"
12.With suitable example describe the break, continue and pass statements. (7 marks for
explanation with example)
‘Break’ in Python is a loop control statement. It is used to control the sequence of the loop.
Continue:
The continue statement in Python returns the control to the beginning of the while loop. The
continue statement rejects all the remaining statements in the current iteration of the loop and
moves the control back to the top of the loop.
The continue statement can be used in both while and for loops
for letter in 'Python': # First Example
if letter == 'h':
continue
print 'Current Letter :', letter
var = 10 # Second Example
while var > 0:
var = var -1
if var == 5:
continue
print 'Current variable value :', var
print "Good bye!"
OUTPUT:
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : o
Current Letter : n
Current variable value : 10
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6
Current variable value : 4
Current variable value : 3
Current variable value : 2
Current variable value : 1
Good bye!
PASS statement:
pass statement is a null statement.
n = 10
for i in range(n):
# pass can be used as placeholder
# when code is to added later
Pass
12.b. i. Write a function that accepts two numbers and return their sum.(7 marks for program)
def add(num1, num2):
return num1 + num2
sum = add(5, 10)
print("The sum of 5 and 10 is", sum)
12.b.ii. Explain the return statement with a suitable example.(7 marks for explanation with
example)
A return statement is used to end the execution of the function call and “returns” the result
(value of the expression following the return keyword) to the caller. The statements after the
return statements are not executed.
def cube(x):
r=x**3
return r
13.a i. Write a function to search a character in a string. ( 7 marks for program)
str="Python Tutorial"
print("The string 'tut' is present in the string: ", "Tut" in str)
13.a.ii Explain about escape characters in string. (7 marks for explanation with example)
the backslash character (\) is used to escape values within strings. The character following the
escaping character is treated as a string literal.
\' Single Quote
\\ Backslash
\n New Line
\r Carriage Return
\t Tab
\b Backspace
\f Form Feed
13.b i. How to copy the lists using [:] operator? ( 7 marks for explanation + example)
List slicing is a frequent practice in Python, and it is the most prevalent technique used by
programmers to solve efficient problems. Consider a Python list. You must slice a list in order
to access a range of elements in it. One method is to utilize the colon as a simple slicing
operator (:)
List[Start : Stop : Stride]
Program:
list= [5,2,9,7,5,8,1,4,3]
print(list[0:6])
print(list[1:9:2])
print(list[-1:-5:-2])
Output:
[5, 2, 9, 7, 5, 8]
[2, 7, 8, 4]
[3, 1]
13.b.ii Explain with an example about the remove operator in list. (7 marks for explanation +
example )
To remove an element from a list using the remove() method, specify the value of that
element and pass it as an argument to the method.
remove() will search the list to find it and remove it.
programming_languages = ["JavaScript", "Python", "Java", "C++"]
print(programming_languages)
programming_languages.remove("JavaScript")
print(programming_languages)
14.a. Write a function called sum that takes any number of arguments and return their
sum.(14 marks for program )
def my_sum(*args):
print(args)
return sum(args)
my_sum(1, 2, 3, 4, 5)
14.b. Explain any seven built-in dictionary methods with examples.(7 marks for methods + 7
marks for example)
Method Description
clear() Removes all the elements from the dictionary
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
car.clear()
copy() Returns a copy of the dictionary
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
x = car.copy()
fromkeys() Returns a dictionary with the specified keys and value
x = ('key1', 'key2', 'key3')
y=0
thisdict = dict.fromkeys(x, y)
get() Returns the value of the specified key
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
x = car.get("model")
items() Returns a list containing a tuple for each key value pair
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
x = car.items()
keys() Returns a list containing the dictionary's keys
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
x = car.keys()
pop() Removes the element with the specified key
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
car.pop("model")
15.a Explain in detail with examples about the file object attributes. ( 7 marks for attributes +
7 marks for example)
File.closed: Returns true if file is closed, false otherwise.
File.mode: Returns access mode with which file was opened.
File.name: Returns name of the file.
File.softspace: Returns false if space explicitly required with print, true otherwise.
Eg:
file = open("test.txt", "r")
print ("Name of the file: ", file.name)
print ("Closed or not : ", file.closed)
print ("Opening mode : ", file.mode)
read_data = file.read()
print(read_data)
15.b Explain in detail about various directory methods. (7 marks for method + 7 marks for
explanation)
1 os.access(path, mode)
Use the real uid/gid to test for access to path.
2 os.chdir(path)
Change the current working directory to path
3 os.chflags(path, flags)
Set the flags of path to the numeric flags.
4 os.chmod(path, mode)
Change the mode of path to the numeric mode.
5 os.chown(path, uid, gid)
Change the owner and group id of path to the numeric uid and gid.
6 os.chroot(path)
Change the root directory of the current process to path.
7 os.close(fd)
Close file descriptor fd.