342 - Python Programming
342 - Python Programming
Guindy, Chennai – 25
BOARD EXAMINATION – April 2023
Prepared By
Mrs. S.Ahalya,
GPT, Coimbatore,
Coimbatore – 14
(Contact No: 9443522450)
QP Code: 342
Python Programming
Scheme of Evaluation
PART A
10*3 = 30 Marks
PART B
5*14 = 70 Marks
def my_function():
print("Hello from a function")
my_function()
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")
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
11.b.i Describe with example about the immutable objects. ( 7 marks for explanation )
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}.
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
if letter == 'h':
continue
var = var -1
if var == 5:
continue
OUTPUT:
Current Letter : P
Current Letter : y
Current Letter : t
Current Letter : o
Current Letter : n
Good bye!
PASS statement:
n = 10
for i in range(n):
Pass
12.b. i. Write a function that accepts two numbers and return their sum.(7 marks for program)
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
str="Python Tutorial"
the backslash character (\) is used to escape values within strings. The character following the
escaping character is treated as a string literal.
\\ 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 (:)
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.
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
car.clear()
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
x = car.copy()
thisdict = dict.fromkeys(x, y)
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()
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
x = car.keys()
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.softspace: Returns false if space explicitly required with print, true otherwise.
Eg:
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)
2 os.chdir(path)
3 os.chflags(path, flags)
4 os.chmod(path, mode)
Change the owner and group id of path to the numeric uid and gid.
6 os.chroot(path)