Sie sind auf Seite 1von 4

LABORATORY #1

10B17CI673: SYSTEM AND NETWORK PROGRAMMING LAB


JAYPEE INSTITUTE OF INFORMATION TECHNOLOGY, NOIDA
Lab

: Basics of Python Language

Date (Assign)

: Practice Lab

Last Date (Checking)

: Practice Lab

Last Date (Late Checking)

Date of Solution to be posted

Total Time

: 2 hrs.

Week

: First

Lab (A or B)

:A

(14 Jan to 21 Jan)

_________________________________________________________________________________________________
INSTRUCTIONS:
1. Unprofessional conduct, such as an abuse of USF computer privileges (unauthorized access), or a Violation of academic integrity
(plagiarism or fraud), will result in the student receiving a failing grade.
2. Work in singles.
3. Late submission will be evaluated from half of total marks.
5. Platform:
Use Python, Either on window or Linux platform.
____________________________________________________________________________________________________________________
Tutorial #1:
Open StartProgramPythonIDLE (GUI) and practice on following commands:
1.

>>> import this

2.

Record representation
Let us take an example; a student can have an enrollment number, name, branch, batch etc. What are the different ways to manage
record of a student and how can we iterate the records of a student. There are different ways of record management and some of them
are:
a.

Using Lists

b.

Database List

c.

Field Labels

d.

Using Dictionaries

e.

Nested Structures

f.

Dictionaries of dictionaries

Let us discuss one by one


a.

Using Lists:
(i)

>>> a=['text',23,'alpha25']

(ii)

>>> a

(iii)

>>> a[0]

(iv)

>>> a[1]

(v)

>>> a[2]

(vi)

>>> a[3]

(vii)

>>> student=["ajay kumar",21,"BTECH","CSE"]

(viii)

>>> student[0].split()[-1]

(ix)

>>> student[0].split()[0]

(x)

>>> len(student)

(xi)

>>> student[0].upper()

(xii)

>>> student[0].swapcase() //convert lower to upper and vice versa

(xiii)

>>> student[0].ljust(100)

(xiv)

>>> student[0].rjust(100)

(xv)

>>> student[0].center(100)

(xvi)

>>> student[0].zfill(100)

(xvii)

>>> student[0].replace('ajay','vijay')

(xviii)

>>> student

//ljust,rjust,center are used for text alignment

b. Database List:
Database is group of lists.
(i)

>>> list1=['student1',21,'batch1']

(ii)

>>> list2=['student2',22,'batch2']

(iii)

>>> list1

(iv)

>>> list2

(v)

>>> database=[list1,list2]

(vi)

>>> for data in database:


print data

c.

(vii)

>>> database[1][0]

(viii)

>>> database[1][2]

(ix)

>>> database[0][1]

(x)

>>> age=[data[1] for data in database]

(xi)

>>>age

(xii)

>>> pays=map((lambda x:x[0]),database)

(xiii)

>>> pays

(xiv)

>>> database.append(['student3',23,'batch3'])

(xv)

>>> database

(i)

>>> NAME, AGE, PAY = range(3)

(ii)

>>> bob = ['Bob Smith', 42, 10000]

(iii)

>>> bob[NAME]

(iv)

>>> PAY, bob[PAY]

Field Labels
# [0, 1, 2]

d. Using Dictionaries
Using list based dictionaries; you can attach values to the field names.
(i)

>>> list1={'name': 'student1','age': 21,'batch': 'batch1'}

(ii)

>>> list2={'name': 'student2','age': 22,'batch': 'batch2'}

(iii)

>>> list1['name']

(iv)

>>> list1['age']+=1

(v)

>>> list1['age']

e. Nested Structures
(i)

>>> list1={'name':{'first':'ADARSH','Last':'Kumar'},
'age':27,
'job':['senior','lecturer'],
'pay':(400,500)}

3.

>>> a=['hello']

4.

>>> a[0]

5.

>>> a[0][1]

6.

>>> a[0][0]

7.

>>> a[0][-1]

8.

>>> a[0][-3]

9.

File Handling
a.

(ii)

>>> list1['name']

(iii)

>>> list1['name']['Last']

(iv)

>>> db={}

(v)

>>> db['list1']=list1

(vi)

>>> db['list2']=list2

(vii)

>>> db['list1']['list2']

(viii)

>>> db['list1']

(ix)

>>> db

Writing data to a file


i. >>> file=open('data.txt','w')
ii. >>> file.write('Hello file world!\n')
iii. >>> file.write('Bye file world.\n')
iv. >>> file.close()

b.

Reading data from a file


i. >>> file=open('data.txt','r')
ii. >>> for line in file.readlines():
iii.

print line

iv. >>> file.seek(0)


v. >>> file.read()
vi. >>> file.seek(0)
vii. >>> file.readlines()
viii. >>> file.readline()
ix. >>> file.seek(0)
x. >>> file.readline()
xi. >>> file.seek(0)
xii. >>> file.read(1),file.read(8)
xiii. >>> file.next()
xiv. >>> file.next()
xv. >>> file.next()

c.

Introduction to Socket programming functions


Basic functions that are used for socket programming are defined here. For these functions, you need to open python help
manual and study the functionality. At this stage functionality of these functions might not be very cleared to you but as you
will proceed and study, things will be clear.
i. >>> import socket
ii. >>> sock=socket.socket()
iii. >>> sock.bind(('localhost',8080))
iv. >>> sock.listen(3)
v. >>> sock1=socket.socket()
vi. >>> sock1.connect(('localhost',8080))
vii. >>> sock.close()
viii. >>> socket.gethostname()
ix. >>> socket.gethostbyname('localhost')
x. >>> socket.getfqdn()
xi. >>> socket.gethostbyaddr('127.0.0.1')
xii. >>> socket.gethostbyname_ex('localhost')
xiii. >>> socket.getprotobyname('TCP')
xiv. >>> socket.getservbyname('FTP')
xv. >>> socket.getservbyport(80)
xvi. >>> socket.has_ipv6

Exercise 1: Implement stack, queue, dqueue and tree in python.


Example:

stack = []
def pushit():
stack.append("A")
def popit():
if len(stack) == 0:
print 'Cannot pop from an empty stack!'
else:
print 'Removed [', 'stack.pop()', ']'
def viewstack():
print stack

# calls str() internally

pushit()
viewstack()
popit()
viewstack()
________________________________

Das könnte Ihnen auch gefallen