0% found this document useful (0 votes)
2 views8 pages

24msuctc05 Python

Uploaded by

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

24msuctc05 Python

Uploaded by

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

24MSUCTC05

SET-I/II
MUTHAYAMMAL COLLEGE OF ARTS AND SCIENCE (Autonomous), RASIPURAM
(For the Candidates admitted from 2023 onwards)
[Link]. Coputer Technology Degree Examinations APRIL - 2026
Semester – V
Programming in Python
Time: 3 Hrs [Link]: 75
Part –A
Answer all the Questions: (10x1=10 marks)

1. Which one is used to store data in key-value pair?


a) Set b) Tuple
c) Dictionary d) List
2. Select the name for white space at the beginning of the line.
a) Indentation b) Identification
c) Idnetifier d) Comments
3. Which statement is used to exit a loop prematurely?
a) Continue b) Pass
c) break d) stop
4. Find a short form of “else if statement.
a) .ifelse b) Elif
c) elif d) Elseif
5. What is the correct way to define a function in Python?
a) function myFunc() b) def myFunc():
c)define myFunc() d) func myFunc()
6. Identify which String function is raises an exception if string not found.
a) find() b) index()
c) Join() d) Search()
7. Which of the following functions will give the total length of a list?
a) Len b) max(len)
c) max len(list) d) len(list)
8. Which of the following is a Python tuple?
a) <7,8,9> b) [7, 8, 9]
c) {7, 8, 9} d) (7, 8, 9)
9. Identify which one is not file object attribute.
a) [Link] b) [Link]
c) [Link] d) [Link]
10. Select the default file access mode in Python.
a) write(w) b) read( r )
c) append d) read (rw)

Part – B
Answer all the questions: (5 x 5 = 25 marks)

11. a State the concepts of type conversion in python


(OR)
b Evaluate the usage of membership operator.
12. a Prepare a Python program to calculate the sum of numbers until user enters zero.
(OR)
b State the syntax to for loop and Explain.
13. a Discuss the concepts of function in python
(OR)
b Organize the concepts of namespace in Python.
14. a Differenciate List with Tuple.
(OR)
b Illustrate the creation of Dictionary in Python.
15. a Discuss on append method in Python file.
(OR)
b Evaluate different file handling modes in python.

Part – C
Answer all the questions: ( 5 X 8 = 40 marks)

16. a Survey the Feature of Python.


(OR)
b Enumerate the methods of array used in Python.
17. a Elucidate the continue statement with help an example.
(OR)
b Analyze the working of conditional statements in Python with suitable examples.
18. a Classification of formal arguments used in python function call.
(OR)
b Determine how will you define own module in Python explain with example.
19. a Survey the basic operation of tuple.
(OR)
b Determine how traversal and membership operation used in Dictionary.
Analyze the process of file handling in Python, including opening, reading, writing, and
20. a
closing files with examples.
(OR)
b Formulate the concepts for finding the file position of Python.

_______
SET-I/II Scheme/Key for valuation
24MSUCTC05

MUTHAYAMMAL COLLEGE OF ARTS AND SCIENCE (Autonomous), RASIPURAM


(For the Candidates admitted from 2023 onwards)
[Link]. Coputer Technology Degree Examinations APRIL - 2026
Semester – V
Programming in Python
Time: 3 Hrs [Link]: 75
Part –A
Answer all the Questions: (10x1=10 marks)
1. c) Dictionary 2. a) Indentation 3. c) break 4. c) elif 5. b) def myFunc():
6. b) index() 7. d) len(list) 8. d) (7, 8, 9) 9. a) [Link] 10. read( r )

Part – B
Answer all the questions: (5 x 5 = 25 marks)

11. a)
These are some built-in functions in the Python programming language that can convert one
type of data into another type. For example, the int function can take any number value and
convert it into an integer.
>>>str(5)
‘5’ # Output
# float to integer
>>>float(5.50)
5 # Output

b)
The membership operators are used to check an item or an element that is part of a string, a list or a
tuple. A membership operator reduces the effort of searching an element in the list.
Operator Description Example
in Return true, if item is in list or in sequence. x in y, results true

Return false, if item is not in list or in sequence.


not in Return false, if item is in list or in sequence.
Return true, if item is not in list or in sequence. x not in y, results false

12. a)
number = int(input('Enter a number: '))
total = 0 # iterate until the user enters 0
while number != 0:
total += number
number = int(input('Enter a number: '))
print('The sum is', total)
b)
The Python for loop is an iterator-based for loop. It goes through the elements in any ordered
sequence list, i.e., string, lists, tuples, the keys of dictionary and other iterables. In each iteration step,
a loop variable is set to a value.
Syntax
>>>for x in y :
Block 1
else: # Optional
Block 2 # excuted only when the loop exits normally
In the above section, we have seen the syntax of for loop. The for loop is used to iterate over a
sequence. Here, x is used to iterate over y and when the loop exits normally then the else part of the
for loop executes otherwise not

13. a)
Python also allows users to define their own functions. To use their own functions in Python, users
have to define the function first; this is known as Function Definition. In a function definition, users
have to define a name for the new function and also the list of the statements that will execute when
the function will be called. The block of the function starts with a keyword def after which the
function name is written followed by parentheses. We can also give some input parameters or
arguments to a function by placing them within these parentheses. The parameters can also be
defined within these parentheses. The block of statements always starts with a colon (:). After
writing the code statements, the block is ended with a return statement whose syntax is return
[expression].
Syntax
def functionname(parameters):
“function_docstring”
statement(s)
return [expression]

b)
A namespace is a container that provides a named context for identifiers. Two identifiers with the
same name in the same scope will lead to a name clash. In simple terms, Python does not allow
programmers to have two different identifiers with the same name. However, in some situations we need
to have same name identifiers. To cater to such situations, namespaces is the keyword. Namespaces
enable programs to avoid potential name clashes by associating each identifier with the namespace from
which it originates.
Example: #module1
def repeat_m(x):
return x*3;
#module2
def repeat_m(x):
return x*3;
import module1 import module2 result=repeat_m(10)
#ambiguous reference for identifier repeat_m

14. a)
1 List is mutable and tuple is non-mutable.
2 List elements are represented by using square brackets. Tuple elements are
represented by using parenthesis.
3 To store tuple elements, Python Virtual Memory requires less memory. To store
list elements, Python Virtual Memory requires more memory.
4 Tuple elements can be access within less time, because they are fixed
(Performance is more). Performance is less compared with tuples.

b)
It is a data structure in which we store values as a pair of key and value.
o Each key is separated from its value by a colon (:), and consecutive items are separated by commas.
o The entire items in a dictionary are enclosed in curly brackets ({}).
Syntax:
dictionary_name = {key_1: value_1, key_2: value_2, key_3: value_3}
Creating a Dictionary:
o The Syntax to create an empty dictionary can be given as: Dictionary_variable= { }
o The Syntax to create a dictionary with key-value pair is:
Dictionary_variable= {key1:val1, key2:val2......}
o A dictionary can be also created by specifying key-value pairs separated
by a colon in curly brackets as shown below.
o Note that one key value pair is separated from the other using a comma.

15. a)
Once you have stored some data in a file,you can always open that file again
to write more data or append data to it. To append a file, you must open it using „a‟ or „ab‟ mode
depending on whether it is text file or binary file. Note that if you open a file with „w‟ or „wb‟ mode
and then start writing data into it, then the existing contents would be overwritten.
To append data to an already existing file
file=open('[Link]','a')
[Link]('\nHave a nice day')
[Link]()
print('Data appended successful')

b)
File Mode
‘r’ Read-only. Raises I/O error if file doesn't exist.
‘r+’ Read and write. Raises I/O error if the file does not exist.
‘w’ Write-only. Overwrites file if it exists, else creates a new one.
‘w+’ Read and write. Overwrites file or creates new one.
‘a’ Append-only. Adds data to end. Creates file if it doesn't exist.
‘a+’ Read and append. Pointer at end. Creates file if it doesn't exist.
‘rb’ Read in binary mode. File must exist.
‘rb+’ Read and write in binary mode. File must exist.
‘wb’ Write in binary. Overwrites or creates new.
‘wb+’ Read and write in binary. Overwrites or creates new.
‘ab’ Append in binary. Creates file if not exist.
‘ab+’ Read and append in binary. Creates file if it does not exist.

Part – C
Answer all the questions: ( 5 X 8 = 40 marks)

16. a)
Simple, easy to learn, Versatile, free and open source, High level language, Interactive, Portable, object
oriented, interpreted, Dynamic and strongly typed language, Extensible, Embeddable,
Easy maintenance, secure, Robost, Multi-threaded, Garbage collection.
Explanation of these concepts 8 marks

b)
The various methods that can be performed in an array are :  Traverse − Print all the array elements
one by one.  Insertion − Adds an element at the given index.  Deletion − Deletes an element at the
given index.  Search − Searches an element using the given index or by the value.  Update −
Updates an element at the given index.
Insert operation is to insert one or more data elements into an array. Based on the requirement, a new
element can be added at the beginning, end, or any given index of array.
from array
import*
array1 =array('i', [10,20,30,40,50]) [Link](1,60)

Syntax and Explanation of each method 8 marks

17. a)
Python continue keyword is used to skip the remaining statements of the current loop and go to the
next iteration. In Python, loops repeat processes on their own in an efficient way. However, there
might be occasions when we wish to leave the current loop entirely, skip iteration, or dismiss the
condition controlling the loop. We use Loop control statements in such cases. The continue keyword
is a loop control statement that allows us to change the loop's control. Example for iterator in
range(10, 21): # If iterator is equals to 15, loop will continue to the next iteration if iterator == 15:
continue # otherwise printing the value of iterator print( iterator )
Output: 10 11 12 13 14 16 17 18 19 20

b) Python language supports different types of conditional branching statements


which are as follows:
 if Statement if test-condition: statement
 if-else Statement
 Nested if statement
 if-elif-else statement.
Sysntax with example of each type 4*2=8 Marks

18. a)
There are four types of formal arguments using which a function can be called which are as follows:
Required arguments ,Keyword arguments Default arguments and Variable-length arguments
Required Arguments
Required arguments are those supplied to a function during its call in a predetermined positional
sequence. The number of arguments required in the method call must be the same as those provided
in the function's definition.
Explanation of these four arguments 4*2=8 marks
b)

Every Python program is a module, that is, every file that you save as .py extension is a module.
Modules should be placed in the same directory as that of the program in which it is imported. It can
also be stored in one of the directories listed in [Link].
First write these lines in a file and save the file as [Link]
def display():
print(“Hello”)
print(“Name of called module is ….”, __name__)
str=”Welcome to the World of Python !!!”
Then open another file ([Link]) and write the lines of code given below.
import mymodule
print(“My module str = ”, [Link])
[Link]()
print(“Name of calling module is ….”, __name__)
19. a)

Operation Expresion Output


Length len((1,2,3,4,5,6)) 6
Concatenation (1,2,3)+(4,5,6) (1,2,3,4,5,6)
Repetition („Good..‟)*3 „Good ..Good..Good‟
Membership 5 in (1,2,3,4,5,6,7,8,9) True
Iteration for i in 1,2,3,4,5,6,7,8,910
(1,2,3,4,5,6,7,8,9,10):
print(i,end=‟ „)
Comparision(Use Tup1=(1,2,3,4,5) False
>,<,==) Tup2=(1,2,3,4,5)
print(Tup1>Tup2)
b)
Maximum max(1,0,3,8,2,9) 9
Traversing Traversing in dictionary is done on the basis of keys. For this, for loop is used, which
iterates over the keys in the dictionary and prints the corresponding values using keys.
Example
We will define a function print_dict. Whenever a dictionary is passed as an argument to this function, it
will print the keys and values of the dictionary.
def print_dict(d):
for c in d:
print c,d[c]
dict1 = {1:’a’,2:’b’,3:’c’,4:’d’}
print_dict(dict1)
OUTPUT: 1 a 2 b 3 c 4 d
Membership Using the membership operator (in and not in), we can test whether a key is in the
dictionary or not. We have seen the in operator earlier as well in the list and the tuple. It takes an input
key and finds the key in the dictionary. If the key is found, then it returns True, otherwise, False.

20. a)
File handling in Python follows a standard sequence: open, perform operations (read or write),
and close. The with statement is the recommended approach for automatic file management.
The built-in open() function is used to open a file and returns a file object.
file_object = open("[Link]", "mode")
Once a file is open in read mode ('r'), you can use various methods to retrieve its content.
with open("[Link]", "r") as file:
content = [Link]()
print(content)
The read(n) method can also take an optional numeric argument n to read a specific number of
characters (or bytes in binary mode).
To write data, the file must be opened in write mode ('w') or append mode ('a').
with open("[Link]", "w") as file:
[Link]("Hello, World!\n")
[Link]("This is a new line.\n")
It is crucial to close files after operations to free up system resources and ensure data is saved. The
best way to do this is using the with statement, which automatically handles closing the file, even if
errors occur.
file_object = open("[Link]", "r")
# perform file operations
file_object.close()
b)
With every file, the file management system associates a pointer often known as file
pointer that facilitates the movement across the file for reading and/ or writing data.
 The file pointer specifies a location from where the current read or write operation is initiated.
Once the read/write operation is completed, the pointer is automatically updated.
 Python has various methods that tells or sets the position of the file pointer.
For example, the tell() method tells the current position within the file at which the next read or write
operation will occur. It is specified as number of bytes from the beginning of the file.
 When you just open a file for reading, the file pointer is positioned at location 0, which is the
beginning of the file.
 The syntax for seek() function is
seek(offset[, from])
 The offset argument indicates the number of bytes to be moved and the from argument specifies
the reference position from where the bytes are to be moved.
------------------------------------------------------------------------------------------------------------------------
----

You might also like