0% found this document useful (0 votes)
4 views5 pages

2 - Mark Final Answer Key Cat2

The document explains various Python programming concepts including the difference between local and global variables, fruitful functions, and the comparison between lists and tuples. It also covers file operations, escape sequences, control statements, and error types in Python. Additionally, it discusses modules, command line arguments, and string formatting using the '%' operator.

Uploaded by

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

2 - Mark Final Answer Key Cat2

The document explains various Python programming concepts including the difference between local and global variables, fruitful functions, and the comparison between lists and tuples. It also covers file operations, escape sequences, control statements, and error types in Python. Additionally, it discusses modules, command line arguments, and string formatting using the '%' operator.

Uploaded by

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

1).

Differentiate local and global variable

Local variables can only be accessed within the function

Global variables can be used throughout the entire program.

a = "Hi Everyone ! !" # Global variable


def func ( ) :
b = " Welcome " # Local variable
print ( a ) # Global variable accessed
print ( b ) # Local variable accessed
func ( )
print ( a ) # Global variable accessed, return Hi ! Everyone
print ( b ) # Local variable accessed, will give error : b is not defined

2). What is fruitful function? Give Example.


A function that returns a value is called fruitful function. The ” return” keyword is used to return
the values from the function.
Example: -
def add():
a=10
b=20
c=a+b
return c
c=add()
print(c)
3). Compare the data types list and tuple.
List is mutable, ordered collection of data item
List iteration is slower and time consuming.
List consumes more memory.
List provides many in-built methods.
Eg: Mylist=[ „a‟,123,‟7.5,‟d‟]
Tuple is immutable, ordered collection of data item
Tuple iterations are faster.
Tuples consumes less memory
Tuples have less in-built methods.
EG – my tuple=(„a‟,123,‟7.5,‟d‟)
4). List any four methods of list.
Syntax : list name.method name( element/index/list)
append()
sum()
index()
remove()
pop()
5). Is List mutable. Justify?
The list is a data type that is mutable. Once a list has been created:

 Elements can be modified.


 Individual values can be replaced.
 The order of elements can be changed.
 Lists are also dynamic. Elements can be added and deleted from a list, allowing it to grow
or shrink

6) Differentiate between aliasing and cloning operations.


Aliasing
Creating a copy of a list is called aliasing.
When you create a copy both the list will be having same memory location.
changes in one list will affect another list.
>>>b=a
>>>a is b
>>>True
Cloning
Creating a copy of a same list of elements in different memory locations is called cloning.
Cloning is a process of making a copy of the list without modifying the original list.
Changes in one list will not affect locations of another list.
>>>b=list[a]
>>>a is b
>>>False
7). Define a module. Give Example
A module is a file consisting of Python code. A module can define functions, classes, and
variables. A module can also include runnable code
Built in Module->import sys
User Built in Module->import Mypackage.Mymodule name
8). Write a program to write a data in a file for both write and append modes
file1 = open("myfile.txt", "w")
L = ["This is Delhi \n", "This is Paris \n", "This is London"]
file1.writelines(L)
file1.close()

# Append-adds at last
file1 = open("myfile.txt", "a") # append mode
file1.write("Today \n")
file1.close()

9). What is an escape sequence in a python string?


An escape sequence is a sequence of characters that, when used does not represent itself but is
converted into another character or series of characters that may be difficult to express directly, like
newline (\n), tab (\t).
>>> print("hai \nhello")
hai
hello
>>> print("hai\thello")
hai hello
10). List down the operations on file.

Open a file

Take input from that file / Write output to that file

Close the file


11). Difference between break and continue statements in with an example.
Break
It terminates the current loop executes the remaining statement outside the loop
for i in "python":
if(i=="t"):
break
print(i)
Output:
P
y
Continue
It terminates the current iteration and, transfers the control to the next iteration in the loop.
for i in "python":
if(i=="t"):
continue
print(i)
Output:
p
y
h
o
n
12). Distinguish between files and modules.
A file is some information or data which stays in the computer storage devices. Files can
be of music files, video files, text files. These files can be used as input for modules.
A Python module is a file containing Python definitions and statements. A module can
define functions and variables.

13). How can we use the format operator “%” in formatting a string?
Python uses string formatting to create new strings. The "%" operator is used to format a set of
variables enclosed in a "tuple" along with a format string. The format string contains text with argument
specifier symbols like "%f" and "%d".
>>> “ There are %d colors in the rainbow”,%7 “
There are 7 colors in the rainbow‟s
14). What are the different types of errors in python programming?
Errors – referred as bugs in the program. Errors occurs maximum by the fault of the
programmer. Debugging – Process of finding and correcting errors.
Two types of errors.:
Syntax errors
Runtime Errors
15). Write the Output of the following code:
def cube(x):
return x * x * x
x = cube(3)
print(x)
Output: 27
16). State the use of command line arguments.
Command line arguments are input parameters passed to the script when executing them. The
most common are:
1. sys.argv
2. getopt module
3. argparse module

You might also like