0% found this document useful (0 votes)
72 views

Laboratory #1 IT System Security Lab University of Petroleum and Energy Studies

This document provides instructions for students to complete Python language basics laboratory exercises. It outlines several tutorials including using lists, dictionaries, and nested structures to represent student records, file handling functions like reading and writing to files, and basic socket programming functions. Students are instructed to work individually and that late submissions will be penalized. The exercises demonstrate fundamental Python concepts like indexing, slicing, and manipulating list and dictionary data structures.

Uploaded by

Mansi Bisht
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Laboratory #1 IT System Security Lab University of Petroleum and Energy Studies

This document provides instructions for students to complete Python language basics laboratory exercises. It outlines several tutorials including using lists, dictionaries, and nested structures to represent student records, file handling functions like reading and writing to files, and basic socket programming functions. Students are instructed to work individually and that late submissions will be penalized. The exercises demonstrate fundamental Python concepts like indexing, slicing, and manipulating list and dictionary data structures.

Uploaded by

Mansi Bisht
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

LABORATORY #1

IT System Security Lab


University of Petroleum and Energy Studies
Lab : Basics of Python Language Date (Assign) :
Last Date (Checking) : Practice Lab Last Date (Late Checking) :
Date of Solution to be posted : Total Time : 2 hrs.
Week :
_________________________________________________________________________________________________
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:
Python, Either on window platform or Linux platform. Start working on linux platform by the time lab instructor install python on all
systems.
____________________________________________________________________________________________________________________

Tutorial #1:

Open StartProgramPythonIDLE (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) //ljust,rjust,center are used for text alignment
(xiv) >>> student[0].rjust(100)
(xv) >>> student[0].center(100)
(xvi) >>> student[0].zfill(100)
(xvii) >>> student[0].replace('ajay','vijay')
(xviii) >>> student
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
(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
c. Field Labels
(i) >>> NAME, AGE, PAY = range(3) # [0, 1, 2]
(ii) >>> bob = ['Bob Smith', 42, 10000]
(iii) >>> bob[NAME]
(iv) >>> PAY, bob[PAY]

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)}
(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
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. 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

________________________________

You might also like