Professional Documents
Culture Documents
List Tuple
List Tuple
List Tuple
2)
List Tuple
Mutable data type Immutable data type
An ordered group of sequences enclosed by [ ] An ordered group of sequences enclosed by ()
L=[1,2,3] L=(1,2,3)
Sequences can be modified after creation Sequences cannot be modified after creation
Operations like add, delete and update can be Operations like add, delete and update cannot
performed be performed
3)
import datetime
t = datetime.datetime.now()
print ("Current date and time : ")
print (t.strftime("%Y-%m-%d %H:%M:%S"))
4) Decorators are callable objects which are used to modify functions or classes. It accepts some
arguments and returns some objects.
Functions can be passed as arguments to another function
def fun1(fun2):
print("Hello")
fun2()
def fun2():
print("Hi")
fun1(fun2)
5) Name mangling
The leading double underscore triggers python’s name mangling. This helps to avoid attribute
name collisions in subclasses. Hence, private members could be accessed using class name from
outside of the class.
6) Overriding is the property of a class to change the implementation of a method provided by
one of its base classes.
In Python method overriding occurs by simply defining in the child class a method with the
same name of a method in the parent class.
7) f = open("text.txt", "r")
s = f.read()
for i in reversed(s):
print(i)
8) Copy is to make a copy of the selected file or folder and place the duplicate in another drive or
folder, while move is to move the original files from one place to another location. The move
command deletes the original files, while copy retains them.
9) GET - fetch information from a server, and should not include sensitive data.
GET parameters are attached to a request via a query string at the end of a URL.
POST - to send data to a server, included parameters may be sensitive and these will also require
added protection such as SSL. POST parameters are attached to a request via the request body.
Part – B (5 x 13 = 65 Marks)
11)a.i) Python Program - Calculate Grade of Student
# Get the details for three assessments - 2M
print("Enter marks sports ");
ass1 = input();
print("Enter marks for three activities");
...
# Assign weight-age 20%, 30% and 50% for ass1, ass2 and ass3 respectively - 1M
average = ass1*.2 + ass2*.3 + ass3*50
# Find the grade – 4M
if(average>=91 and average<=100):
print("Your Grade is A+");
elif(average>=81 and average<=90):
print("Your Grade is A");
...
else:
print("RA");
11 a.ii) c=0;
print("Enter -1 to exit");
num=input("enter a number") 1M
while(num)
{ # prime number checking 4M
for i in range(2,num):
if (num % i) == 0:
break
else:
c=c+1 # counting 1M
num=input("enter a number") # repeat to get input
}
print(c) # print the count value – 1M
11b.i) Set - 2M
Set contain unique elements only
Immutable like tuples and strings
eg. {1,2,3}
11 b. i) Various operations on set – 5M
printme('kongu engineering')
Keyword arguments are related to the function calls. When you use keyword arguments in a
function call, the caller identifies the arguments by the parameter name.
def printinfo( name, age )
print "Name: ", name
print "Age ", age
return
printinfo( age=50, name="babu" )
A default argument is an argument that assumes a default value if a value is not provided in the
function call for that argument.
def printinfo( name, age = 35 ):
print "Name: ", name;
print "Age ", age;
return;
printinfo( age=50, name="babu" );
printinfo( name="devi" ); # age=35 default arg
Variable-length arguments
You may need to process a function for more arguments than you specified while defining the
function. These arguments are called variable-length arguments and are not named in the function
definition, unlike required and default arguments.
def printinfo( arg1, *vartuple ):
"This prints a variable passed arguments"
print "Output is: "
print arg1
for var in vartuple:
print var
return
printinfo( 10 )
printinfo( 70, 60, 50 )
12 b.ii)
2M
2M
2M
# client.py - 5M
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # create a socket object
host = socket.gethostname() # get local machine name
port = 4567
s.connect((host, port)) # connection to hostname on the port.
tm = s.recv(1024) # Receive no more than 1024 bytes
s.send(“hai”) # send the message
s.close() # closes the connection
print(tm)
#Server side – 4M
import cgi, os
import cgitb; cgitb.enable()
form = cgi.FieldStorage()
# Get filename
fileitem = form['filename']
# Test if the file was uploaded
if fileitem.filename:
# strip leading path from file name to avoid
# directory traversal attacks
fn = os.path.basename(fileitem.filename)
open(fn, 'wb').write(fileitem.file.read())
message = 'The file "' + fn + '" was uploaded successfully'
else:
message = 'No file was uploaded'
15bii) Various UDP socket methods
(Any six from the following and each carry 1 M)
# socket creation socket_type=SOCK_DGRAM for UDP
S = socket.socket(socket_family, socket_type, protocol=0)
connect( ) - To connect to a remote socket at an address.
bind( )- binds the socket to an address
listen(backlog)- listens for the connection made to the socket.
The backlog is the maximum number of queued connections
that must be listened before rejecting the connection.
accept( ) -accept a connection.
s.recvfrom()- Receives UDP messages
s.sendto()-Transmits UDP messages
General methods
close() - close the socket connection.
gethostname()- hostname of the machine where the python interpreter is currently executing. .
gethostbyname() - If you want to know the current machine's IP address, you may use
gethostbyname(gethostname()).
16a) import MySQLdb; #import necessary module to establish data base connection – 1 M
#insert records - 5M
sql = "INSERT INTO book_details (...)"
cursor1.execute(sql)
#str1=input("Enter name")
sql=("INSERT INTO book_details (...))
cursor1.execute(sql)
db.commit()
Design a separate web page for performing a particular operation and corresponding server
program to support the same.
Avail.html # Web page for ticket availability (front end)
<form action = "Avail.py" method = "post">
Source name: <input type = "text" name = "source_name"><br
Destination Name: <input type = "text" name = "destination_name" />
...
<input type = "submit" value = "Submit" />
</form>