Python L1 Assignments:
Here we are going to list Python L1 Assignments questions with answers you can see the solution and learn from it.
Happy Learning !!
- What will be the output of ‘seclist’ in print commands of below code?
mylist = range(4)
seclist = mylist
print seclist
mylist.append(4)
print seclist
seclist = mylist[:]
print seclist
mylist.append(5)
print seclist
Answer:
[0,1,2,3]
[0,1,2,3,4]
[0,1,2,3,4]
[0,1,2,3,4]
- What is the output of following code:
def f(n):
for x in range(n):
yield x**3
for x in f(6):
print (x)
Answer:
0 1 8 27 64 125
- Write a program to receive a string fromkeybordand check if the string has two ‘e’ in the characters. If yes return True else False.
Answer:
c=raw_input(“”)
n=c.count(“e”,0,len(c))
if n==2:
print “True”
else:
print “False”
- What is the output of following code:
counter = 1
def dolots(count):
global counter
for i in (1, 2, 3):
counter = count + i
print dolots(4)
print counter
Answer:
7
5. Write a code to read the data from input file called input.txt and count the number of characters per line, number of words per line and write these into output file called as output.txt
Answer :
fname = “input.txt”
oFname = “output.txt”
num_words = 0
num_chars = 0
with open(fname, ‘r’) as f:
with open(oFname, ‘w’) as f1:
for line in f:
words = line.split()
f1.writelines(‘%d’ %len(words))
f1.writelines(‘\n’);
f1.writelines(‘%d’ %len(line))
f1.writelines(‘\n’);
6. Create 3 Lists ( list1,list2,list3) with numbers and perform following operations
-
- a) CreateMaxlist by taking 2 maximum elements from each list.
- b) Find average value from all the elements ofMaxlist.
- c) Create aMinlIst by taking 2 minimum elements from each list
- d) Find the average value from all the elements ofMinlist
Answer :
list1=range(10,20)
list2=range(30,40)
list3=range(50,60)
list1.sort()
list2.sort()
list3.sort()
minList=[]
maxList=[]
minList=[list1[0],list1[1],list2[0],list2[1],list3[0],list3[1]]
maxList=[list1[len(list1)-2],list1[len(list1)-1],list2[len(list2)-2],list2[len(list2)-1],list3[len(list3)-2],list3[len(list3)-1]]
print sum(minList) / float(len(minList))
print sum(maxList) / float(len(maxList))
7. Write program to convert prefix/net mask to IP. eg: input:16 output: 255.255.0.0
Answer:
import socket
import struct
host_bits = 32 – int(16)
netmask = socket.inet_ntoa(struct.pack(‘!I’, (1 << 32) – (1 << host_bits)))
print netmask
8. Create a suitable data construct to read the data from an xml document as shown below:
<bookstore shelf=”New Arrivals”>
<book category=”COOKING”>
<title lang=”en“>Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category=”CHILDREN”>
<title lang=”en“>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category=”WEB”>
<title lang=”en“>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
Answer:
import xml.etree.ElementTree as ET
tree = ET.parse(‘BookXML.xml’)
root = tree.getroot()
count=0
count1=0
for child in root:
print “\n”
print “Name :”+root[count1][0].text
print “Author :”+root[count1][1].text
print “Year :”+root[count1][2].text
print “\n”
count1+=1
9. Create a suitable object type and check for file size of 0 bytes of the directory contents as shown below
02/15/2016 10:49 PM 962 switchfinal.py
02/15/2016 10:49 PM 943 switchfinal.py.bak
01/27/2016 11:46 AM 15 t.py
03/31/2016 12:39 PM 840 t1.py
01/25/2016 10:34 AM 2,407 tc1.py
02/14/2017 09:13 AM 0 teat.py
03/15/2016 05:52 PM 5 tes.py
Answer:
import os
basedir = ‘C:/Py‘
count=0
names = os.listdir(basedir)
sizes = [(path, os.stat(path).st_size) for path in names]
for size in sizes:
if size[1]==0:
count+=1
if count==0:
print “No file with zero size”
else:
print “There are files with zero size”
10. Create a suitable object type to eliminate the duplicate elements
Answer:
t = [1, 2, 3, 1, 2, 5, 6, 7, 8]
list=set(t)
print list