Python W22
Python W22
(Autonomous) (Autonomous)
(ISO/IEC - 27001 - 2013 Certified) (ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________ __________________________________________________________________________________________________
WINTER – 2022 EXAMINATION != Not Equal to
Subject Name: Programming with Python Model Answer Subject Code: 22616 < Less than
> Greater than
Important Instructions to examiners: XXXXX <= Less than and Equal to
1) The answers should be examined by key words and not as word-to-word as given in the model answer scheme.
>= Greater than and Equal to
2) The model answer and the answer written by candidate may vary but the examiner may try to assess the
c) Describe Tuples in Python. 2M
understanding level of the candidate.
3) The language errors such as grammatical, spelling errors should not be given more Importance (Not applicable for Ans A tuple is a collection of items which is ordered and unchangeable. 2M for
subject English and Communication Skills. Tuples are the sequence or series values of different types separated by commas (,). Definition.
4) While assessing figures, examiner may give credit for principal components indicated in the figure. The figures Example: tup1=(10,20,30)
drawn by candidate and model answer may vary. The examiner may give credit for any equivalent figure drawn.
5) Credits may be given step wise for numerical problems. In some cases, the assumed constant values may vary and d) Write use of lambda function in python. 2M
there may be some difference in the candidate’s answers and model answer.
6) In case of some questions credit may be given by judgement on part of examiner of relevant answer based on Ans The lambda function, which is also called anonymous function. A lambda function can 2M for use
candidate’s understanding. take any number of arguments, but can only have one expression.
7) For programming language papers, credit may be given to any other program based on equivalent concept. Syntax: lambda arguments : expression
8) As per the policy decision of Maharashtra State Government, teaching in English/Marathi and Bilingual (English + Example: x= lambda a,b : a*b
Marathi) medium is introduced at first year of AICTE diploma Programme from academic year 2021-2022. Hence if Print(x(10,5)
the students in first year (first and second semesters) write answers in Marathi or bilingual language (English
Output: 50
+Marathi), the Examiner shall consider the same and assess the answer based on matching of concepts with
model answer. e) Write syntax of defining class in Python. 2M
Ans Bitwise operators acts on bits and performs bit by bit operation. Assume a=10 (1010) 4M (for any Ans 4M (any four,
and b=4 (0100) four, 1M each) 1M each)
Operator Meaning Description Example
& Binary AND This operation a &b =
performs AND 1010 &
operation 0100 =
between 0000 =0
operands.
Operator copies a
bit, to the result,
if it exists in both
operands
| Binary OR This operation a|b = 1010 |
performs OR 0100 =
operation 1110 = 14
between
operands. It
copies a bit, if it
exists in either
operand.
^ Binary XOR This operation a^b=1010 ^
performs XOR 0100 =
operations 1110 =14
between
operands. It
copies the bit, if it
is set in one
operand but not
both.
~ Binary Ones It is unary ~a= ~ 1010
Complement operator and has = 0101
the effect of www.diplomachakhazna.in
'flipping' bits i.e.
opposite the bits
of operand.
<< Binary Left The left operand's a<<2 =
Page No: 3 | 21 Page No: 4 | 21
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous) (Autonomous)
(ISO/IEC - 27001 - 2013 Certified) (ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________ __________________________________________________________________________________________________
test()
print("global variable=",g)
output:
local variable= 20
Global variable= 10
global variable= 10
output:
i is 20
c) What is local and global variables? Explain with appropriate example. 4M (Similar type of program can consider)
Ans Global variables: global variables can be accessed throughout the program 4M (2M for
3. Attempt any THREE of the following: 12 M
body by all functions. explanation
Local variables: local variables can be accessed only inside the function in and 2M for a) Write basis operations of list. 4M
which they are declared example)
Concept Diagram: Ans 1)Accessing values in list: Any two
Accessing elements liters from a list in Python is a method to get values that are stared operations:
in the list at a particular location or index.
To access values in lists, use the square brackets for slicing along with the index or 2 M for each
indices to obtain value available at that index.
Example: accessing list values.
>>> list1 = ["one","two",3,10,"six",20]
>>> list1[0]
'one'
>>> list1[-2]
A global variable (x) can be reached and modified anywhere in the code, local 'six'
variable (z) exists only in block 3. >>> list1[1:3]
Example: ['two', 3]
g=10 #global variable g >>> list1[3:]
def test(): [10, 'six', 20]
l=20 #local variable l >>> list1[:4]
print("local variable=",l) ['one', 'two', 3, 10]
# accessing global variable >>>
print("Global variable=",g)
a) Compare list and dictionary. (Any 4 points) 4M Ans import collections Any proper
import pprint logic program
Ans List Dictionary Any four point, file_input = input('File Name: ') 4M
List is a collection of index values pairs Dictionary is a hashed structure of 1 M for 1 point with open(file_input, 'r') as info:
as that of array in c++. key and value pairs. count = collections.Counter(info.read().upper())
value = pprint.pformat(count)
List is created by placing elements in [ ] Dictionary is created by placing print(value)
separated by commas “, “ elements in { } as “key”:”value”,
each key value pair is separated by d) Write python program to read contents of abc.txt and write same content to 4M
commas “, “ pqr.txt.
The indices of list are integers starting The keys of dictionary can be of any Ans with open('abs.txt','r') as firstfile, open('prq.txt','w') as secondfile: Any proper
from 0. data type. # read content from first file logic program
for line in firstfile: for 4 M
The elements are accessed via indices. The elements are accessed via key- # write content to second file
values. secondfile.write(line)
The order of the elements entered are There is no guarantee for
maintained. maintaining order.
5. Attempt any TWO of the following: 12 M
b) What is command line argument? Write python code to add b) two 4M
a) Write different data types in python with suitable example. 6M
numbers given as input from command line arguments and print its sum.
Ans Data types in Python programming includes:
Numbers: Represents numeric data to perform mathematical operations.
Page No: 11 | 21 Page No: 12 | 21
MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION
(Autonomous) (Autonomous)
(ISO/IEC - 27001 - 2013 Certified) (ISO/IEC - 27001 - 2013 Certified)
__________________________________________________________________________________________________ __________________________________________________________________________________________________
String: Represents text characters, special symbols or alphanumeric data. 5. String Data Type: String is a collection of group of characters. Strings are identified
List: Represents sequential data that the programmer wishes to sort, merge etc. as a contiguous set of characters enclosed in single quotes (' ') or double quotes (" ").
6m for data
Tuple: Represents sequential data with a little difference from list. types
Any letter, a number or a symbol could be a part of the string. Strings are
Dictionary: Represents a collection of data that associate a unique key with each unchangeable (immutable). Once a string is created, it cannot be modified.
value. Example: For string data type.
Boolean: Represents truth-values (true or false). >>> s1="Hello" #string in double quotes
>>> s2='Hi' #string in single quotes
1. Integers (int Data Type): An integer is a whole number that can be positive (+) or >>> s3="Don't open the door" #single quote string in double quotes
negative (−). Integers can be of any length, it is only limited by the memory available. >>> s4='I said "yipee"' #double quote string in single quotes
Example: For number data types are integers. >>>type(s1)
>>>a=10 <class 'str'>
>>>b -10
To determine the type of a variable type() function is used. 6. List Data Type: List is an ordered sequence of items. It is one of the most used
>>>type(a) datatype in Python and is very flexible.
>>> <class 'int'> List can contain heterogeneous values such as integers, floats, strings, tuples, lists and
dictionaries but they are commonly used to store collections of homogeneous objects.
2. Boolean (Bool Data Type: The simplest build-in type in Python is the bool type, it The list datatype in Python programming is just like an array that can store a group of
represents the truth-values False and True. Internally the true value is represented as elements and we can refer to these elements using a single name. Declaring a list is
1 and false is 0. pretty straight forward. Items separated by commas ( , ) are enclosed within brackets [
For example ].
>>>a = 18 > 5 Example: For list.
>>>print(a) >>> first=[10, 20, 30] # homogenous values in list
True >>> second=["One","Two","Three"] # homogenous values in list
b=2>3 >>> first
print(b) [10, 20, 30]
False >>> second
['One', 'Two', 'Three']
3. Floating-Point/Float Numbers (Float Data Type): Floating-point number or Float is >>> first + second # prints the concatenated lists
a positive or negative number with a fractional part. A floating point number is [10, 20, 30, 'One', 'Two', 'Three']
accurate up to 15 decimal places. Integer and floating points are separated by decimal
points. 1 is integer, 1.0 is floating point number. 7. Tuple Data Type: Tuple is an ordered sequence of items same as list. The only
Example: Floating point number. difference is that tuples are immutable.
x=10.1 Tuples once created cannot be modified. It is defined within parentheses ( ) where
type(x) items are separated by commas ( , ).
<class 'float'> A tuple data type in python programming is similar to a list data type, which also
contains heterogeneous items/elements.
4. Complex Numbers (Complex Data Type): Complex numbers are written in the form, Example: For tuple.
x + yj, where x is the real part and y is the imaginary part. >>> a=(10,'abc',1+3j)
Example: >>> a
Complex number. (10, 'abc', (1+3j))
>>>x = 3+4j >>> a[0]
>>>print(x.real) 10
3.0 >>> a[0]=20
>>>print(x.imag) Traceback (most recent call last):
4.0 File "<pyshell#12>", line 1, in <module>
# function feed
def feed(self):
print("I eat only plants. I am vegetarian.")
herbi = Herbivorous()
herbi.feed()
# calling some other function
herbi.breathe()
Output:
I eat only plants. I am vegetarian.
I breathe oxygen.
Page No: 21 | 21