php 2.
php 2.
Subject Name: Programming with Python Model Answer Subject Code: 22616
Page 1 of 23
The List has the variable The tuple has the fixed length
length
List operations are more error Tuples operations are safe
prone.
Lists can be used to store Tuples are used to store only
homogeneous and heterogeneous elements.
heterogeneous elements.
List is useful for insertion and Tuple is useful for readonly
deletion operations. operations like accessing
elements.
List iteration is slower and is Tuple iteration is faster.
time consuming.
d) Explain Local and Global variable 2M (1m each)
Local Variables: Local variables are those which are initialized
inside a function and belongs only to that particular function. It
cannot be accessed anywhere outside the function
Example:
def f():
# local variable
s = "I love Python Programming"
print(s)
# Driver code
f()
Output
I love Python Programming
Global Variables: The global variables are those which are defined
outside any function and which are accessible throughout the
program i.e. inside and outside of every function.
Example:
# This function uses global variable s
def f():
print("Inside Function", s)
# Global scope
s = "I love Python Programming"
f()
print("Outside Function", s)
Output:
Inside Function I love Python Programming
Outside Function I love Python Programming
e) Define class and object in python 2M (Any suitable
Class: A class is a user-defined blueprint or prototype from which definition: 1M
objects are created. Classes provide a means of bundling data Each)
and functionality together.
Page 2 of 23
f) How to give single and multiline comment in Python 2M (1m each)
Single line comment: Single-line comments are created simply by
beginning a line with the hash (#) character, and they are
automatically terminated by the end of line.
Example:
# print is a statement
print(‘Hello Python’)
for i in range(1,5):
for j in range(1,i+1):
print(j,end=' ')
print()
b) Explain four Buit-in tuple functions in python with example 4M ( 1M for each
function with
example)
Page 3 of 23