Lab Manual
Lab Manual
PYTHON PROGRAMMING
LAB MANUAL
(18CS614)
PEO1: Graduates will have strong understanding in logical, mathematical and analytical reasoning
among peers, coupled with problem solving attitude.
PEO2: Graduates able to engage in the productive practice of Computer Science and Engineering to
identify and solve significant problems across broad range of application areas.
PEO3: Graduates able to engage in successful careers in industry, academics, and public service,
providing communication and management skills that generate entrepreneurship and/ or leadership
qualities.
PEO4: Graduates able to adapt to new technologies, tools and methodologies by life-long learning to
remain at the leading edge of computer engineering practice with the ability to respond to challenges of
changing environment with ethical approach.
(18CS614)
Course Objectives:
1. Able to introduce core programming basics and program design with functions using Python
programming language.
Course Outcomes:
Program:
Output:
Conclusion:
Program:
Output:
Conclusion:
Program:
Output:
Conclusion:
AIM: Write a python script to print the current date in the following format “Sun May 07
03:30:03 IST 2020”
Program:
import time
import datetime
print("Current date and time: " , datetime.datetime.now())
print("Current year: ", datetime.date.today().strftime("%Y"))
print("Month of year: ", datetime.date.today().strftime("%B"))
print("Week number of the year: ", datetime.date.today().strftime("%W"))
print("Weekday of the week: ", datetime.date.today().strftime("%w"))
print("Day of year: ", datetime.date.today().strftime("%j"))
print("Day of the month : ", datetime.date.today().strftime("%d"))
print("Day of week: ", datetime.date.today().strftime("%A"))
Output:
Conclusion:
Program:
class MyList:
def __init__(self):
self.n = []
def add(self, a):
return self.n.append(a)
def remove(self, b):
self.n.remove(b)
def display(self):
return (self.n)
obj = MyList()
choice = 1
while choice != 0:
print("0. Exit")
print("1. Add")
print("2. Delete")
print("3. Display")
choice = int(input("Enter choice: "))
if choice == 1:
n = int(input("Enter number to append: "))
obj.add(n)
print("List: ", obj.display())
elif choice == 2:
n = int(input("Enter number to remove: "))
obj.remove(n)
print("List: ", obj.display())
elif choice == 3:
print("List: ", obj.display())
elif choice == 0:
print("Exiting!")
else:
print("Invalid choice!!")
Conclusion:
Program:
Output:
Conclusion:
Program:
# Creating a Dictionary
# with Integer Keys
Dict = {1: 'Audisankara', 2: 'Engineering', 3: 'College'}
print("\nDictionary with the use of Integer Keys: ")
print(Dict)
# Creating a Dictionary
# with Mixed keys
Dict = {'Name': 'Audisankara', 1: [1, 2, 3, 4]}
print("\nDictionary with the use of Mixed Keys: ")
print(Dict)
Output:
Conclusion:
Program:
# Python program to find the largest number among the three input numbers
# change the values of num1, num2 and num3
# for a different result
num1 = 10
num2 = 14
num3 = 12
# uncomment following lines to take three numbers from user
#num1 = float(input("Enter first number: "))
#num2 = float(input("Enter second number: "))
#num3 = float(input("Enter third number: "))
if (num1 >= num2) and (num1 >= num3):
largest = num1
elif (num2 >= num1) and (num2 >= num3):
largest = num2
else:
largest = num3
print("The largest number is", largest)
Output:
Conclusion:
Program:
Output:
Conclusion:
AIM: Python program to display all the prime numbers within an interval
Program:
lower = 900
upper = 1000
print("Prime numbers between", lower, "and", upper, "are:")
for num in range(lower, upper + 1):
# all prime numbers are greater than 1
if num > 1:
for i in range(2, num):
if (num % i) == 0:
break
else:
print(num)
Output:
Conclusion:
Program:
Output:
Conclusion:
Program:
X = [ [1,2],[3,4],[4,5] ]
# 2x3 matrix
Y = [ [1,2,3],[4,5,6] ]
# resultant matrix
result = [ [0,0,0],[0,0,0],[0,0,0] ]
my_list = []
# iterating rows of X matrix
for i in range( len(X) ):
# iterating columns of Y matrix
for j in range(len(Y[0])):
# iterating rows of Y matrix
for k in range(len(Y)):
result[i][j] += X[i][k] * Y[k][j]
for r in result:
print(r)
Output:
Conclusion:
Program:
Output:
Conclusion:
AIM: Write a python program to define a module and import a specific function in that
module to another program.
Program:
# top_module.py
import module1
module1.print_parameters()
print(module1.combinations(5, 2))
# module1.py
from module2 import k, print_parameters
from math import factorial
n = 5.0
def combinations(n, k):
return factorial(n) / factorial(k) / factorial(n-k)
# module2.py
import module1
k = 2.0
def print_parameters():
print('k = %.f n = %.f' % (k, module1.n))
Output:
Conclusion:
AIM: Write a script named copyfile.py. This script should prompt the user for the names
of two text files. The contents of the first file should be input and written to the second file.
Program:
Output:
Conclusion:
AIM: Write a program that inputs a text file. The program should print all of the unique
words in the file in alphabetical order.
Program:
# Program to sort alphabetically the words form a string provided by the user
my_str = "Hello this Is an Example With cased letters"
# To take input from the user
#my_str = input("Enter a string: ")
# breakdown the string into a list of words
words = my_str.split()
# sort the list
words.sort()
# display the sorted words
print("The sorted words are:")
for word in words:
print(word)
Output:
Conclusion:
class py_solution:
def int_to_Roman(self, num):
val = [
1000, 900, 500, 400,
100, 90, 50, 40,
10, 9, 5, 4,
1
]
syb = [
"M", "CM", "D", "CD",
"C", "XC", "L", "XL",
"X", "IX", "V", "IV",
"I"
]
roman_num = ''
i=0
while num > 0:
for _ in range(num // val[i]):
roman_num += syb[i]
num -= val[i]
i += 1
return roman_num
print(py_solution().int_to_Roman(1))
print(py_solution().int_to_Roman(4000))
Output:
Conclusion:
Program:
class py_solution:
def pow(self, x, n):
if x==0 or x==1 or n==1:
return x
if x==-1:
if n%2 ==0:
return 1
else:
return -1
if n==0:
return 1
if n<0:
return 1/self.pow(x,-n)
val = self.pow(x,n//2)
if n%2 ==0:
return val*val
return val*val*x
print(py_solution().pow(2, -3));
print(py_solution().pow(3, 5));
print(py_solution().pow(100, 0));
Output:
Conclusion:
Program:
Output:
Conclusion:
AIM: Write a python program to define a module to find Fibonacci Numbers and import
the module to another program.
Program:
Output:
Conclusion: