Python 140
Python 140
LAB MANUAL
PARUL UNIVERSITY
FACULTY OF ENGINEERING & TECHNOLOGY PARUL INSTITUTE OF ENGINEERING &
TECHNOLOGY DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING /
INFORMATION TECHNOLOGY
STUDENT DETAILS
Marks
Sr. Experiment Title Page Date Sign
(out of
No No
10)
WAP to read and display the following
1 information. Name, Address, Phone no.
PRACTICAL: 1
Aim: - WAP to read and display the following information. Name, Address,
Phone no.
Program:
name = input("Please enter your name: ")
address = input("Please enter your address: ")
phone = input("Please enter your phone number: ")
print("Name:", name)
print("Address:", address)
print("Phone number:", phone)
Output:
EEnrnrool l m
e ennt N o: :190303108140 Page | 1
Faculty Of Engineering & Technology
Subject Name: Python Programming
Subject Code: 203105452
B. Tech. IT Year 4rd Semester 8
PRACTICAL: 2
Aim: - WAP to read two numbers from the keyboard and display the larger
one on the screen.
Program:
Output:
PRACTICAL: 3
Program:
Output:
Program:
x=1
y=2
x, y = swap(x, y)
print("x =", x)
print("y =", y)
Output:
Program:
def factorial(n):
# Initialize the result to 1
result = 1
# Iterate over the range of integers from 1 to n
for i in range(1, n+1):
# Multiply the result by the current number
result *= i
return result
print(factorial(5))
print(factorial(10))
Output:
Aim: - WAP to print Fibonacci series of ‘n’ numbers, where n is given by the
programmer.
Program:
def fibonacci(n):
# Initialize the first two numbers in the series
a, b = 0, 1
# Iterate over the range of integers from 1 to n
for i in range(1, n+1):
print(b)
# Calculate the next number in the series
a, b = b, a+b
n = 10
print("Fibonacci series of", n, "numbers:")
fibonacci(n)
Output:
Aim: - WAP to read a set of numbers in an array & to find the largest of them.
Program:
# Read the number of elements in the array
n = int(input("Please enter the number of elements in the array: "))
numbers = []
for i in range(n):
numbers.append(int(input("Please enter element {}: ".format(i+1))))
Output:
Program:
n = int(input("Please enter the number of names: "))
names = []
for i in range(n):
names.append(input("Please enter name {}: ".format(i+1)))
names.sort()
print("Sorted names:")
for name in names:
print(name)
Output:
Aim: - WAP to read a set of numbers from keyboard & to find the sum of all
elements of the given array using a function.
Program:
def sum_array(numbers):
result = 0
for number in numbers:
result += number
return result
Output:
Program:
import random
PRACTICAL: 11
Program:
import random
class Employee:
def __init__(self, name, id, designation, salary):
self.name = name
self.id = id
self.designation = designation
self.salary = salary
def inc_sal(self):
if self.designation == "Manager":
self.salary += 5000
elif self.designation == "General Manager":
self.salary += 10000
elif self.designation == "CEO":
self.salary += 20000
elif self.designation == "Worker":
self.salary += 2000
Output:
Aim: - WAP to read data from keyboard & write it to the file. After writing is
completed, the file is closed. The program again opens the same file and reads
it.
Program:
Output:
Program:
def bubble_sort(arr):
for i in range(len(arr)-1):
# Flag to indicate whether any swaps were made
swapped = False
for j in range(len(arr)-1-i):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
swapped = True
# If no swaps were made, the array is sorted
if not swapped:
break
return arr
arr = [5, 3, 2, 4, 1]
sorted_arr = bubble_sort(arr)
print(sorted_arr)
Output:
PRACTICAL: 14
Program:
Code:
import sqlite3
# create a connection to the database
conn = sqlite3.connect('bank.db')
# create the Bank table with columns AccountNo, CustomerName, Balance, Phone, and
Address
c.execute('''CREATE TABLE IF NOT EXISTS Bank (
AccountNo INTEGER PRIMARY KEY,
CustomerName TEXT NOT NULL,
Balance REAL NOT NULL,
Phone TEXT NOT NULL,
Address TEXT NOT NULL)''')
# print the details of all customers with a balance greater than 20,000
def print_records_with_balance_gt_20000():
c.execute("SELECT * FROM Bank WHERE Balance > 20000")
records = c.fetchall()
for record in records:
print(record)
# print the details of all customers with a balance greater than 20,000
print_records_with_balance_gt_20000()
Output:
Conclusion:
In this implementation of practical I have learned how to import sqlite3 and how to work
with database. And I have learned different types of operation on SQLite3.and leaned how to
apply conditions on table data
PRACTICAL NO: 16
AIM: Create two classes namely Employee and Qualification. Using multiple
inheritance derive two classes Scientist and Manager. Take suitableattributes &
operations. WAP to implement this class hierarchy.
Code:
class Employee:
def __init__ (self,name,age):
self.name = name
self.age = age
def employee_details(self):
print('Name is ' ,self.name)
print('Age is ',self.age)
class Qualification:
def __init__ (self,degree,college):
self.degree=degree
self.college=college
def qualification_details(self):
print('Name of degree',self.degree)
print('Name of college',self.college)
class Scientist(Employee,Qualification):
def __init__ (self,name,age,degree,college):
Employee. __init__(self,name,age)
Qualification. __init__ (self,degree,college)
print("Scientist's Details:-")
class Manager(Employee,Qualification):
def __init__ (self,name,age,degree,college):
Employee. __init__(self,name,age)
Qualification. __init__ (self,degree,college)
print('Manager\'s deatils:-')
s1=Scientist('ABC',11,'B.tech- IT','XYZ')
s1.employee_details()
s1.qualification_details()
m1=Manager('ABC',11,'B.tech - IT','XYZ')
m1.employee_details()
m1.qualification_details()
Output:
Conclusion:
In this implementation of practical I have learned how to use of oop concept in python. And I
have learned how to implement inheritance in python with different classes. And how to
create objects in python and how to call class using of this object
PRACTICAL NO: 17
Code:
import turtle
def form_tri(side):
for i in range(3):
my_pen.fd(side)
my_pen.left(120)
side -= 10
# window screen
tut = turtle.Screen()
tut.bgcolor("orange")
tut.title("Turtle")
my_pen = turtle.Turtle()
my_pen.color("white")
tut = turtle.Screen()
# different shapes
side = 300
for i in range(10):
form_tri(side)
side -= 30
Output:
Conclusion:
In this implementation of practical I have learned how import turtle libraries and how to
work with this library and how to draw different types of shapes using of turtle.
PRACTICAL NO: 18
Code:
v0=IntVar()
v0.set(1)
r1=Radiobutton(window, text="Male", variable=v0,value=1)
r2=Radiobutton(window, text="Female", variable=v0,value=2)
r1.place(x=100,y=50)
r2.place(x=180, y=50)
v1 = IntVar()
v2 = IntVar()
C1 = Checkbutton(window, text = "Boy", variable = v1)
C2 = Checkbutton(window, text = "Girl", variable = v2)
C1.place(x=100, y=100)
C2.place(x=180, y=100)
window.title('Hello Students')
window.geometry("400x300+10+10")
window.mainloop()
Output:
Conclusion:
In this implementation of practical I have learned how to use tkinter library in Python. Using
of this library I have learned how to implement a radio button, how to implement a Check
box, and How to implement a dropdown list in python.
PRACTICAL NO: 19
AIM: Assume that a list of Students “height in inches is given. Create a Numpy
array from this list and then multiply that array with 0.0254 to convert all height
measurements into meters and print the Numpy array Height_list =
[180,215,176,150,181,209]
Code:
class Student(object):
def __init__ (self, name, height):
self.name = name
self.height = height
def getheight(self):
return self.height
def str (self):
return self.name + ' : ' + str(self.getheight())
def HeightRecord(rec, name, height):
rec.append(Student(name, height))
return rec
Record = [x] = 'y'
while x == 'y':
name = input('Enter the name of the student: ')
height = input('Enter the height recorded: ')
Record = HeightRecord(Record, name, height)
x= input('another student? y/n ')
n=1
for el in Record:
print(n,'. ', el)
n=n+1
Output:
PRACTICAL NO: 20
Code:
Server:
import socket
def binary_search(arr, val, start, end):
if start == end:
if arr[start] > val:
return start
else:
return start + 1
if start > end:
return start
def binary_insertion_sort(arr):
for i in range(1, len(arr)):
val = arr[i]
j = binary_search(arr, val, 0, i - 1)
arr = arr[:j] + [val] + arr[j:i] + arr[i+1:]
return arr
HOST = 'localhost'
PORT = 65432
while True:
conn, addr = s.accept()
with conn:
print('Connected by', addr)
data = conn.recv(1024)
arr = list(map(int, data.decode().split(',')))
sorted_arr = binary_insertion_sort(arr)
sorted_data = ','.join(map(str, sorted_arr)).encode()
conn.sendall(sorted_data)
Client:
import socket
HOST = 'localhost'
PORT = 65432
def get_input():
while True:
try:
n = int(input("Enter the number of elements to be sorted: "))
if n <= 0:
print("Please enter a positive integer.")
else:
break
except ValueError:
print("Please enter a valid integer.")
arr = []
for i in range(n):
while True:
try:
element = int(input(f"Enter element {i+1}: "))
break
except ValueError:
print("Please enter a valid integer.")
arr.append(element)
return arr
Output: Server:
Output: Client:
Conclusion:
In this implementation of practical I have learned how to use of socket library and how to
connect with server and how client and server will work in request - response method. And
how to get and put data from client side.