You are on page 1of 7

Q.

1) WAP to understand the order of execution of methods in


several base classes according to MRO
Code:
class A:
    def rk(self):
        print(" In class A")
class B(A):
     def rk(self):
        print(" In class B")
class C(A):
     def rk(self):
      print("In class C")
  
# classes ordering
class D(B, C):
     pass
     
r = D()
r.rk()

Output :
MRO:

Q.2) WAP to pickle and unpickle EMP Class Object.


Ans:

import pickle

class employee:

def __init__(self,id=0,name="a",salary=0):

self.dic1={}

self.dic1["id"]= id

self.dic1["Name"]= name

self.dic1["Salary"]= salary

outfile = open("battle.txt","wb")

pickle.dump(self.dic1,outfile)

outfile.close()

def display(self):

infile=open("battle.txt","rb")

dic2=pickle.load(infile)

infile.close()

print("ID:{}\nName:{}\nSalary:{}".format(dic2["id"],dic2["Name"],dic2["Salary"]))
n=input("Enter ID,name and salary").split()

ob=employee(int(n[0]),n[1],n[2])

Q.3) WAP to create and use deque.


Ans:

import collections

DoubleEnded = collections.deque(["Mon","Tue","Wed"])

print (DoubleEnded)

print("Adding to the right: ")

DoubleEnded.append("Thu")

print (DoubleEnded)

print("Adding to the left: ")

DoubleEnded.appendleft("Sun")

print (DoubleEnded)

print("Removing from the right: ")

DoubleEnded.pop()

print (DoubleEnded)

print("Removing from the left: ")

DoubleEnded.popleft()
print (DoubleEnded)

print("Reversing the deque: ")

DoubleEnded.reverse()

print (DoubleEnded)

Output:

4.WAP to implement chat between Server and client 


Server:
import socket
def server_program():
 host = socket.gethostname()
 port = 5000
 server_socket = socket.socket()
 server_socket.bind((host, port))
 server_socket.listen(2)
 conn, address = server_socket.accept()
 print("Connection from: " + str(address))
 while (True):
     data = conn.recv(1024).decode()
     if not data:
         break
     print("from connected user(bot): " + str(data))
     data = input('qwerty-> ')
     conn.send(data.encode())
 conn.close()
if __name__ == '__main__':
 server_program()

Client:
import socket
def client_program():
 host = socket.gethostname()
 port = 5000
 client_socket = socket.socket()
 client_socket.connect((host, port))
 message = input("bot-> ")
 while message.lower().strip() != 'bye':
     client_socket.send(message.encode())
     data = client_socket.recv(1024).decode() 
     print('Received from server(Sanket): ' + data)
     message = input("bot-> ")
 client_socket.close()
if __name__ == '__main__':
 client_program()

Output:
Client

Server :

5.Difference between Pearl and Python


FEATURE PERL PYTHON

Perl is a general purpose high Python is a widely used general-


Introduction
level language popular for CGI purpose, high level programming

scripts. Some of the popular language. Due to its rich library and
support, it has wide applications in
projects in Perl are CPanel and
Web Development, Machine
Bugzilla. It was initially designed
Learning, Desktop Applications,
to replace complex shell scripts.
etc.

Python deals with whitespaces and

Perl does not care about a syntax error generates if


Whitespaces
whitespaces. whitespaces are not according to

Python.

Python accentuates support for


Perl accentuates support for
common methodologies such as
Focus common tasks such as report
object-oriented programming and
generation and file scanning.
data structure design.

The .pl file extension is used to The .py file extension is used to

File Extension save Perl Scripts. For example save Python Scripts. Example:

myDocument.pl myFile.py

It is not necessary to end the

End of All statements should end with statements with a semi colon in

Statement a semi colon in Perl. Python as it deals with

whitespaces.

Comments and For Inline comments, we use # Python also uses # for Inline

Documentation in Perl. comments.


e.g. #Inline-Comment in Perl

e.g. #Inline-Comment in Python


whereas for documentation we

use but for documentation we use

= and =cut “”” i.e. Three inverted commas

e.g. =Documentation in Perl e.g. “””Documentation in Python

starts from here and ends here. starts from here and ends here.”””

=cut

Statement Perl uses braces to mark the Python use indentations to mark

Blocks statement blocks. the statement blocks.

Some data types contained by Some data types contained by

Datatypes Perl are numeric, string, Scalars, Python are numeric, strings, lists,

Arrays, Hashes. dictionaries, tuples.

You might also like