Python Programs
Python Programs
DAILY PROGRAMS
PROGRAM 1
Aim: Write a python program to take a two digit number as an input from user and
Source code:
"""
@author: it291
"""
a=x//10
b=x%10
Output:
1
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
PROGRAM 2
Aim: Write a program to prompt the user for hours and rate per hour to compute
gross pay using python
Source code:
"""
@author: it291
"""
hours=int(input('Enter hours'))
pay=hours*rate
print(pay)
Output:
2
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
PROGRAM 3
Aim: Write a python program to take a string as an input from the user and do the
following
Source code:
@author: it291
"""
name =input('enter:')
print (name[1:])
print (name[:-1])
print (name[::-1])
print (name[::2])
Output:
3
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
PROGRAM 4
Aim: Write a python program to covert the given string in upper case and lower case.
Source code:
"""
@author: it291
"""
nnn=greet.upper()
print(nnn)
Output:
4
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
PROGRAM 5
Aim: write a python program to ask the user to enter an integer(use an empty prompt)
Source code:-
"""
@author: it291
"""
x_str =input()
x=int(x_str)
print (x%2==1)
Output:
5
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
PROGRAM 6
Aim: write a python program to ask the user to enter a name (use an empty prompt)
Source code:-
@author: it291
"""
name =input()
print (name.upper())
Output:
6
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
PROGRAM 7
Source code:
"""
@author: it291
"""
x =int (input( ))
if x<0:
elif x>0:
print ("positive")
else:
print ("zero")
print ("done")
Output:-
7
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
PROGRAM 8
Aim: write a python program to rewrite your pay computation to give the employee 1.5
times the hourly rate for hours worked above 40 hours
Source code:
"""
@author: it291
"""
if hours >40:
ot=hours-40;
pay=(40*rate)+(ot*(1.5*rate))
else:
pay=hours*rate
print(pay)
Output:
8
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
PROGRAM 9
Aim: Write a python program to find the greatest of two numbers and print the square
of the greatest number if the numbers are same print square of either number.
Source code:
@author: it291
"""
x=int(input("Enter the first number"))
if x>y:
elif y>x:
else:
Output:
9
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
PROGRAM 10
Aim: Write a python program to find the largest number of the given numbers.
Source code:
@author: it291
"""
Output:
10
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Program 11
Aim: write a python program to find greatest among three given numbers.
Source code:
"""
@author: it291
"""
a=int(input("enter a:"))
b=int(input("enter b:"))
c=int(input("enter c:"))
else:
Output:
11
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
PROGRAM 12
Aim: Write a python program to find the largest number of the given numbers.
Source code:
@author: it291
"""
Output:
PROGRAM 13
Aim: write a python program to count the elements in the list and print count with the
number (or) element
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 25 08:05:48 2023
@author: it291
"""
12
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
PROGRAM 14
Source code:
@author: it291
"""
13
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
PROGRAM 15
Aim: write a python program to use a while loop to generate the numbers from 1 to 10
if a number divisible by 3 is found print “***” .if a number is divisible by 5 is found
print “*****” . other wise just print the number
@author: it291
"""
n =1
while n<11:
if n%3==0:
print (n,'***')
elif n%5==0:
print (n,'*****')
else:
print (n)
n=n+1
print ('done')
Output:-
14
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Program 16
Aim: write a python program to ask the user to enter an integer ,n(z)
Source code:-
"""
@author: it291
"""
n=int(input())
a, b =0,1
result = [0]
result.append (b)
a, b = b, a+b
print (result)
Output:
15
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
PROGRAM 17
Source code:-
"""
@author: it291
"""
s='Monthly Python'
print(s[0:4])
print(s[6:7])
print(s[6:20])
Output:
16
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
PROGRAM 18
@author: it291
"""
PROGRAM 19
@author: it291
"""
fruit ='banana'
pos = fruit.find ('aa')
print (pos)
aa = fruit.find('a')
print (aa)
Output:-
17
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
PROGRAM 20
@author: it291
"""
18
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
PROGRAM 21
Source code:-
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 25 08:19:01 2023
@author: it291
"""
Output:
PROGRAM 22
@author: it291
"""
friends=['siva','scout','mavi']
friends.sort()
print(friends)
Output:-
19
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
PROGRAM 23
Source code:
"""
Created on Sat Mar 25 08:44:55 2023
@author: it291
"""
stuff=list()
stuff.append('book')
stuff.append(99)
print (stuff)
stuff.append('cookie')
print (stuff)
Output:
20
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
PROGRAM 24
Source code:-
"""
@author: it291
"""
print(t[1:3])
print(t[:4])
print(t[3:])
print(t[:])
Output:
21
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
PROGRAM 25
Source code:
"""
@author: it291
"""
greet='Hello Bob'
nstr=greet.replace('bob','jane')
print(nstr)
nstr=greet.replace('o','x')
print(nstr)
Output:
22
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Program 26
Source code:
"""
@author: it291
"""
name="siva venkat"
dd=name.rstrip()
print(dd)
ss=name.lstrip()
print(ss)
Output:
23
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Program 27
Source code:-
"""
@author: it291
"""
n = int(input())
x = list (range(n))
print(x)
Output:
24
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Program 28
Store the square of all the add numbers less than n in a list
Source code:-
"""
@author: it291
"""
n =int(input())
result =[]
result.append (i+i)
print(result)
Output:
25
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Program 29
Source code:
"""
@author: it291
"""
purse=dict()
purse ['candy'] =3
print (purse)
print (purse['candy'])
print (purse)
Output:
26
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Program 30
Source code:
"""
@author: it291
"""
lst= list()
lst.append(21)
lst.append(183)
print (lst)
lst[0]=23
print (lst)
Output:
27
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Program 31
Source code:-
"""
@author: it291
"""
ddd=dict()
ddd ['age']=21
ddd ['course']=182
print(ddd)
ddd ['age']=23
print(ddd)
Output:
28
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Program 32
Source code:-
"""
@author: it291
"""
etc=line.split()
print (etc)
line ='first;second;third'
thing =line.split()
print (thing)
print(len(thing))
thing =line.split(';')
print (thing)
print(len(thing))
Output:
29
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Program 33
Aim: write a python program ask the user to enter a list of integer separated by spaces
convert this into a list of integer but square each element
Source code :
"""
@author: it291
"""
text = input()
result = []
x = int(item)
result.append(x*x)
print (result)
Output:
30
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Program 34
Source code:-
"""
@author: it291
"""
x=input().lower()
Output:
31
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Program 35
Aim: write a python program for when we encounter a new name , we need to add a
new entry in the dictionary
Source code:
"""
@author: it291
"""
counts =dict()
names =['csev','cwen','csev','zgian','cwen']
counts[name]=1
else:
counts[name] =counts[name]+1
print(counts)
Output:
32
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Program 36
Source code:
"""
@author: it291
"""
counts =dict()
line = input('')
words =line.split()
print('words:',words)
print('counting...')
counts[word]=counts.get(word, 0)+1
print('counts',counts)
Output:
33
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Program 37
Source code:
"""
@author: it291
"""
print(jjj)
ooo={}
print(ooo)
Output:
34
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
PROGRAM 38
Source code:
"""
@author: it291
"""
counts={}
for i in str.lower():
if i not in counts:
counts [i]=1
else:
counts[i]=counts[i]+1
print(counts)
Output:
35
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
PROGRAM 39
Aim: write a python program to read list of students details (registration number
marks) and display it in dictionary
Source code:
"""
@author: it291
"""
s={}
for i in range(n):
reg = input()
m=input()
if reg not in s:
s[reg]=m
Output:
36
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
37
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
PROGRAM 40
Aim: write a python program ask the user for a list of integer separated by spaces each
number of occurrence of each in the string version as the key and the square of the
integers print the result with square of the given number
Source code:
"""
@author: it291
"""
text=input()
d={}
x=int(item)
d[item]=x*x
print(d)
Output:
38
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
PROGRAM 41
Ask the user for a 3 character month code lower /upper mixed
Source code:
"""
@author: it291
"""
months=('jan feb mar apr may jun jul aug sep oct nov dec').split()
month2mm={}
for i in range(len(months)):
month2mm[months[i]]=i+1
text=input()
mon=text[:3].lower()
print(month2mm[mon])
39
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Output:
40
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Program 42
Source code:
"""
@author: it291
"""
a=(5,6,7)
n1,n2,n3=a
print(n1,n2,n3)
Output:
Program 43
41
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Source code:
"""
@author: it291
"""
a=(5,6,7)
n1,n2,n3=a
print(n1,n2,n3)
print(a+(8,))
Output:
Program 44
42
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Aim: write a python program to read list of integers from keyboard and display in
tuple.
Source code:
"""
@author: it291
"""
a=()
for i in range(n):
print("enter {} integer".format(i+1))
s=input()
a=(a+(s,))
print(a)
Output:
Program 45
Aim:Write a python program to reverse the tuple of strings.
43
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Source code:
Program 46
44
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Source code:
"""
@author: it291
"""
def add(a,b):
c=a+b
return c
a=15
b=20
Output:
Program 47
45
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Source code:
"""
@author: it291
"""
def add_numbers(a=18,b=87):
sum=a+b
print("sum=",sum)
add_numbers(30,40)
Output:
Program 48
46
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Source code:
"""
@author: it291
"""
def find_sum(*numbers):
result=0
result=result+num
print("sum=",result)
find_sum(10,20,30)
find_sum(40,90)
Output:
Program 49
47
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Aim: write a python program define a function called power2() which takes a single
argument x but returns 2pow(x).
Source code:
"""
@author: it291
"""
def power2():
def f(x):
print(2**x)
f(x)
x=4
power2()
Output:
Program 50
48
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Aim: write a python program to add two numbers using positional arguments.
Source code:
"""
@author: it291
"""
def add(a,b):
sum=a+b
print("sum=",sum)
j=43
k=39
add(j,k)
Output:
Program 51
49
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Aim: write a python program to define a function call message which contain 2
positional
Source code:
"""
@author: it291
"""
def message(a,b,c,d=29):
print(message(84,124,c=93,d=99))
Output:
Program 52
50
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Source code:
Program-53
51
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Aim :Write a python program to calculate the number of days between two given dates.
Source Code:
"""
@author: it291
"""
d1=datetime.strptime(d1,"%d/%m/%Y")
d2=datetime.strptime(d2,"%d/%m/%Y")
if d1>d2:
else:
Output:
Program-54
52
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Source Code:
"""
@author: it291
"""
d=date.today()
yd=d-timedelta(days=1)
td=d+timedelta(days=1)
Output:
Program 55
53
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Aim: Write a python program to convert given string into daytime object.
Source code:
Program 56
54
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Aim: write a python program to generate 5 random integers from given list of integers.
Source code:
"""
@author: it291
"""
l=[2,4,9,12,16,18]
import random
print(random.sample(l,5))
Output:
Program 57
55
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
5 to 50 excluding 50
a. -50 to 50
b. 5 to 50 with step of 3
Source code:
"""
@author: it291
"""
import random
print(random.randint(-50,50))
print(random.randrange(5,50))
print(random.randrange(5,50,3))
Output:
Program 58
56
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Source code:
"""
@author: it291
"""
import random
print(random.randrange(100000,1000000))
Output:
Program 59
57
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Aim: write a python program to generate a random password which means the
following conditions.
Source code:
"""
@author: it291
"""
import random
import string
randomsource=string.ascii_letters+string.digits+string.punctuation
password=random.sample(randomsource,6)
password+=random.sample(string.ascii_uppercase,2)
password+=random.choice(string.digits)
password+=random.choice(string.punctuation)
random.shuffle(password)
print(password)
Output:
Program 60
Aim:Write a python program to shuffle the given elements in a list.
58
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Source code:
Output:
Program 61
Source code:
"""
@author: it291
"""
import re
l=re.findall("a",str)
print(l)
Output:
Program 62
59
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Aim: write a python program to replace all occurrences of 5 with five for given string.
Source code:
"""
@author: it291
"""
import re
print(re.sub(r'(5)',"five",n))
Output:
Program 63
60
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Aim: write a python program to print all the elements that contain either a or w.
Source code:
"""
@author: it291
"""
import re
l=n.split(",")
m=[]
for i in l:
if re.search(r'a',i) or re.search(r'w',i):
m.append(i)
print(m)
Output:
Program 64
61
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Aim: write a python program that matches the string has a followed by zero or more
b’s.
Source code:
"""
@author: it291
"""
import re
if re.search(r'ab*',n):
print("match found")
else:
print("not found")
Output:
Program 65
62
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Aim: write a python program to replace all occurrences of spaces commas with
underscore.
Source code:
"""
@author: it291
"""
import re
n=input("enter ay string")
print(re.sub("[, ]","_",n))
Output:
Program 66
63
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Aim: write a python program to find all five character words in a given string.
Source code:
"""
@author: it291
"""
import re
l=re.findall("\w{5}",n)
print(l)
Output:
Program 67
64
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Aim: write a python program that matches the string with any no of occurrences of
characters followed by s in a given string.
Source code:
"""
@author: it291
"""
import re
l=re.findall(".*s",str)
print(l)
Output:
Program 68
65
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Aim: python program that matches the string with any characters by s in a given string.
Source code:
"""
@author: it291
"""
import re
s=re.findall(".+s",str)
print(s)
Output:
Program 69
66
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Aim: write a python program that matches the string any no of i’s before s in a given
string.
Source code:
"""
@author: it291
"""
import re
l=re.findall("i*s",str)
print(l)
Output:
Program 70
67
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Aim: write a python program to create a file named bec.txt and write some content into
the file and display the content on the screen.
Source code:
"""
@author: it291
"""
a=open("bec.txt","a")
a.write(n)
a.close()
a=open("bec.txt","r")
s=a.read()
a.close()
Output:
Program 71
68
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Aim: write a python program to read the contents of file and display it on the screen.
Source code:
"""
@author: it291
"""
a=open("bec.txt","r")
s=a.read()
a.close()
Output:
Program 72
69
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Aim: write a python program to create a file named bapatala.txt and write some
content into to it then display no of words in a given file.
Source code:
"""
@author: it291
"""
a=open("bapatala.txt","a")
a.write(n)
a.close()
a=open("bapatala.txt","r")
s=a.read()
a.close()
m=s.split(" ")
cnt=0
for i in s:
cnt+=1
print(cnt)
Output:
Program 73
70
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Aim: write a python program to write some content on file bec.txt and count no of
words start with s.
Source code:
"""
@author: it291
"""
a=open("bec.txt","a")
a.write(n)
a.close()
b=open("bec.txt","r")
s=b.read()
b.close()
m=s.split(" ")
cnt=0
import re
for i in s:
if re.search(r'^s',i):
cnt+=1
print(cnt)
Output:
Program 74
71
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Aim: write a python program to read and write from a text file bec.txt your function
should find and display the occurrences of the word ‘the’.
Source code:
"""
@author: it291
"""
f=open("bec.txt","w")
f.write(n)
f.close()
f=open("bec.txt","r")
s=f.read()
f.close()
s=s.split(" ")
cnt=0
for w in s:
if w=='the' or w=='The':
cnt+=1
print("count is",cnt)
Output:
Program 75
72
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Aim: write a python program to count the number of lines from text file named bec.txt
which is not starting with an alphabet ‘t’ or ‘T’.
Source code:
"""
@author: it291
"""
def count():
f=open("bec.txt","r")
cnt=0
for line in f:
cnt+=1
print("count=",cnt)
count()
Output:
Program 76
73
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Source code:
"""
@author: it291
"""
def push():
list.append(ele)
print("inserted successfully")
def pop():
if len(list)==0:
print("stack is empty")
else:
print("deleted successfully")
def display():
n=len(list)
if n==0:
print("stack is empty")
else:
for i in list[::-1]:
74
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
print(i)
list=[]
while True:
print("1.push\n2.pop\n3.display")
if ch==1:
push()
elif ch==2:
pop()
elif ch==3:
display()
else:
printf("invalid choice")
if c=='y' or c=='Y':
continue
else:
break
Output:
75
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
76
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
Program 77
Source code:
77
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
"""
@author: it291
"""
def enqueue():
list.append(ele)
print("inserted successfully")
def dequeue():
if len(list)==0:
print("queue is empty")
else:
print("deleted successfully")
def display():
n=len(list)
if n==0:
print("queue is empty")
else:
for i in range(n):
print(list[i])
list=[]
78
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
while True:
print("1.enqueue\n2.dequeue\n3.display")
if ch==1:
enqueue()
elif ch==2:
dequeue()
elif ch==3:
display()
else:
print("invalid choice")
if c=='y' or c=='Y':
continue
else:
break
Output:
79
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
80
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB
81
DEPARTMENT OF INFORMATION TECHNOLOGY