Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 81

PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

DAILY PROGRAMS

PROGRAM 1

Aim: Write a python program to take a two digit number as an input from user and

specify each digit number separately.

Source code:

# -*- coding: utf-8 -*-

"""

Created on Sat Mar 18 07:54:38 2023

@author: it291

"""

x=int(input("Enter a two digit number:"))

a=x//10

b=x%10

print("Ten's digit is:",a)

print("One's digit is:",b)

Output:

1
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

PROGRAM 2

Aim: Write a program to prompt the user for hours and rate per hour to compute
gross pay using python

Source code:

# -*- coding: utf-8 -*-

"""

Created on Sat Mar 11 08:05:47 2023

@author: it291

"""

hours=int(input('Enter hours'))

rate=int(input('Enter rate per hour:'))

pay=hours*rate

print(pay)

Output:

2
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

PROGRAM 3

Aim: Write a python program to take a string as an input from the user and do the
following

1.Print the string without the first character.

2.Print the string without last character.

3.Print the string in reverse.

4.Print alternate characters of the following.

Source code:

# -*- coding: utf-8 -*-


"""
Created on Sat Mar 18 07:59:47 2023

@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

REGD NO: Y21AIT484

PROGRAM 4

Aim: Write a python program to covert the given string in upper case and lower case.

Source code:

# -*- coding: utf-8 -*-

"""

Created on Sat Mar 25 08:42:29 2023

@author: it291

"""

greet=' Good Morning'

nnn=greet.upper()

print(nnn)

Output:

4
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

PROGRAM 5

Aim: write a python program to ask the user to enter an integer(use an empty prompt)

Print true if the number is add, print false otherwise

Source code:-

# -*- coding: utf-8 -*-

"""

Created on Sat Mar 25 09:13:57 2023

@author: it291

"""

x_str =input()

x=int(x_str)

print (x%2==1)

Output:

5
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

PROGRAM 6

Aim: write a python program to ask the user to enter a name (use an empty prompt)

Print the name entered but in upper case

For example, if the user enters ‘abc’, print’ ABC’.

Source code:-

# -*- coding: utf-8 -*-


"""
Created on Sat Mar 25 09:23:08 2023

@author: it291
"""

name =input()
print (name.upper())
Output:

6
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

PROGRAM 7

Aim: Ask the user to enter an integer

If the number is positive print “positive”


If the number is negative print “negative”

Source code:

# -*- coding: utf-8 -*-

"""

Created on Sat Mar 18 08:26:21 2023

@author: it291

"""

x =int (input( ))

if x<0:

print ("negative of", abs (x) )

elif x>0:

print ("positive")

else:

print ("zero")

print ("done")

Output:-

7
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

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:

# -*- coding: utf-8 -*-

"""

Created on Sat Mar 11 08:13:47 2023

@author: it291

"""

hours=int(input('Enter hours of work:'))

rate=int(input('Enter rate per hour:'))

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

REGD NO: Y21AIT484

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:

# -*- coding: utf-8 -*-


"""
Created on Sat Mar 18 08:37:53 2023

@author: it291
"""
x=int(input("Enter the first number"))

y=int(input("Enter the second number"))

if x>y:

print("first number is greatest")

print("sqaure of the greatest is",x*x)

elif y>x:

print("second number is greatest")

print("sqaure of the greatest is",y*y)

else:

print("Numbers are same")

print("sqaure of the greatest is",y*y)

Output:

9
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

PROGRAM 10

Aim: Write a python program to find the largest number of the given numbers.

Source code:

# -*- coding: utf-8 -*-


"""
Created on Sat Mar 25 08:03:00 2023

@author: it291
"""

thing=[3, 41, 12, 9, 74, 15]


largest=0
for i in thing:
if(i>largest):
largest=i
print (largest)

Output:

10
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

Program 11

Aim: write a python program to find greatest among three given numbers.

Source code:

# -*- coding: utf-8 -*-

"""

Created on Wed Apr 1 07:46:42 2023

@author: it291

"""

a=int(input("enter a:"))

b=int(input("enter b:"))

c=int(input("enter c:"))

if a>b and a>c:

print("a is the greatest number")

elif b>c and b>a:

print("b is the greatest number")

else:

print("c is the greatest number")

Output:

11
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

PROGRAM 12

Aim: Write a python program to find the largest number of the given numbers.

Source code:

# -*- coding: utf-8 -*-


"""
Created on Sat Mar 25 08:03:00 2023

@author: it291
"""

thing=[3, 41, 12, 9, 74, 15]


largest=0
for i in thing:
if(i>largest):
largest=i
print (largest)

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
"""

thing=[3, 41, 12, 9, 74, 15]


count=0
for i in thing:
print(count+1,i)
count=count+1
print('done')
Output:-

12
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

PROGRAM 14

Aim: Write a python to calculate the average of the given numbers.

Source code:

# -*- coding: utf-8 -*-


"""
Created on Sat Mar 25 08:07:40 2023

@author: it291
"""

thing=[10, 20, 30, 40, 50]


count=0
sum=0
for i in thing:
sum=sum+i
count=count+1
avg=sum/count
print('sum=',sum)
print('avg=',avg)
Output:-

13
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

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

# -*- coding: utf-8 -*-


"""
Created on Sat Mar 18 09:04:59 2023

@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

REGD NO: Y21AIT484

Program 16

Aim: write a python program to ask the user to enter an integer ,n(z)

Store the first n numbers of the fibonnaci series in a list

Print the list

Source code:-

# -*- coding: utf-8 -*-

"""

Created on Sat Apr 1 08:03:19 2023

@author: it291

"""

n=int(input())

a, b =0,1

result = [0]

for i in range (n-1):

result.append (b)

a, b = b, a+b

print (result)

Output:

15
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

PROGRAM 17

Aim: write a python program using string slicing

Source code:-

# -*- coding: utf-8 -*-

"""

Created on Sat Mar 11 08:20:47 2023

@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

REGD NO: Y21AIT484

PROGRAM 18

Aim: write a python program using with range function

# -*- coding: utf-8 -*-


"""Created on Sat Mar 25 08:17:39 2023

@author: it291
"""

friends = ['scout', 'mavi', 'ultron']


for i in range(len(friends)):
friend =friends[i]
print ('happy ugadi', friend)
Output:-

PROGRAM 19

Aim: write a python program to demonstrate searching a string

# -*- coding: utf-8 -*-


"""
Created on Sat Apr 1 09:34:46 2023

@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

REGD NO: Y21AIT484

PROGRAM 20

Aim: write a python program to demonstrate built in function and lists

# -*- coding: utf-8 -*-


"""
Created on Sat Mar 25 08:11:39 2023

@author: it291
"""

nums =[3, 41, 12, 9, 74, 15]


print(len(nums))
print(max(nums))
print(min(nums))
print(sum (nums))
print(sum(nums)/len(nums))
Output:

18
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

PROGRAM 21

Aim: write a python program using string and lists

Source code:-
# -*- coding: utf-8 -*-
"""
Created on Sat Mar 25 08:19:01 2023

@author: it291
"""

abc='with three words'


stuff=abc.split()
print(stuff)
print(len(stuff))
print(stuff[0])

Output:

PROGRAM 22

Aim: write a python program to demonstrate lists in order

# -*- coding: utf-8 -*-


"""
Created on Sat Mar 25 08:31:12 2023

@author: it291
"""
friends=['siva','scout','mavi']
friends.sort()
print(friends)
Output:-

19
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

PROGRAM 23

Aim: Write a python program to add elements in a list using append.

Source code:

# -*- coding: utf-8 -*-

"""
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

REGD NO: Y21AIT484

PROGRAM 24

Aim: write a python program on list can be sliced using string

Source code:-

# -*- coding: utf-8 -*-

"""

Created on Sat Mar 25 08:52:45 2023

@author: it291

"""

t=[9, 41, 12, 3, 74, 15]

print(t[1:3])

print(t[:4])

print(t[3:])

print(t[:])

Output:

21
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

PROGRAM 25

Aim: Write a python program to replace a word or letters in a particular string.

Source code:

# -*- coding: utf-8 -*-

"""

Created on Sat Mar 25 09:05:52 2023

@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

REGD NO: Y21AIT484

Program 26

Aim: write a python program on stripping with spaces

Source code:

# -*- coding: utf-8 -*-

"""

Created on Fri Jul 7 13:59:49 2023

@author: it291

"""

name="siva venkat"

dd=name.rstrip()

print(dd)

ss=name.lstrip()

print(ss)

Output:

23
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

Program 27

Aim: write a python program to enter an integer n

Create a list of integer from 0 to n-1

Print the list

Source code:-

# -*- coding: utf-8 -*-

"""

Created on Sat Apr 1 07:54:28 2023

@author: it291

"""

n = int(input())

x = list (range(n))

print(x)

Output:

24
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

Program 28

Aim: write a python program to enter an integer n

Store the square of all the add numbers less than n in a list

Print the list

Source code:-

# -*- coding: utf-8 -*-

"""

Created on Sat Apr 1 08:00:29 2023

@author: it291

"""

n =int(input())

result =[]

for i in range (1, n, 2):

result.append (i+i)

print(result)

Output:

25
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

Program 29

Aim: write a python program to demonstrate the dictionaries

Source code:

# -*- coding: utf-8 -*-

"""

Created on Sat Apr 1 08:11:55 2023

@author: it291

"""

purse=dict()

purse ['money'] =12

purse ['candy'] =3

purse ['tissues'] =75

print (purse)

print (purse['candy'])

purse ['candy'] =purse ['candy']+2

print (purse)

Output:

26
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

Program 30

Aim: write a python program to comparing lists and dictionaries

Source code:

# -*- coding: utf-8 -*-

"""

Created on Sat Apr 1 08:23:26 2023

@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

REGD NO: Y21AIT484

Program 31

Aim: write a python program using dictionaries

Source code:-

# -*- coding: utf-8 -*-

"""

Created on Sat Apr 1 08:26:49 2023

@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

REGD NO: Y21AIT484

Program 32

Aim: write a python program to demonstrate string with spaces

Source code:-

# -*- coding: utf-8 -*-

"""

Created on Sat Apr 1 08:35:32 2023

@author: it291

"""

line = 'a lot of space'

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

REGD NO: Y21AIT484

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

Print the list

Source code :

# -*- coding: utf-8 -*-

"""

Created on Sat Apr 1 08:09:26 2023

@author: it291

"""

text = input()

result = []

for item in text.split():

x = int(item)

result.append(x*x)

print (result)

Output:

30
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

Program 34

Aim: write a python program to ask the user to enter a string

Check if the substring ‘abc’ or ‘def’ exist in the string

If it does print True else false

Source code:-

# -*- coding: utf-8 -*-

"""

Created on Sat Apr 1 09:19:20 2023

@author: it291

"""

x=input().lower()

print ('abc' in x or 'def' in x)

Output:

31
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

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:

# -*- coding: utf-8 -*-

"""

Created on Sat Apr 8 07:44:50 2023

@author: it291

"""

counts =dict()

names =['csev','cwen','csev','zgian','cwen']

for name in names:

if name not in counts:

counts[name]=1

else:

counts[name] =counts[name]+1

print(counts)

Output:

32
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

Program 36

Aim: write a python program to demonstrate counting pattern

Source code:

# -*- coding: utf-8 -*-

"""

Created on Sat Apr 8 07:59:01 2023

@author: it291

"""

counts =dict()

print ('enter a line of text :')

line = input('')

words =line.split()

print('words:',words)

print('counting...')

for word in words:

counts[word]=counts.get(word, 0)+1

print('counts',counts)

Output:

33
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

Program 37

Aim: write a python program to demonstrate dictionary literals(constants)

Source code:

# -*- coding: utf-8 -*-

"""

Created on Sat Apr 8 08:49:16 2023

@author: it291

"""

jjj={'roshan':1, 'shiva':6, 'prakash':9}

print(jjj)

ooo={}

print(ooo)

Output:

34
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

PROGRAM 38

Aim: write a python program to ask the user to enter a string

Covert this to lower case

Count the no.of occurrences of each character in the string

Print the result in sorted order of character

Source code:

# -*- coding: utf-8 -*-

"""

Created on Sat Apr 8 08:53:00 2023

@author: it291

"""

counts={}

str = input("enter a string:")

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

REGD NO: Y21AIT484

PROGRAM 39

Aim: write a python program to read list of students details (registration number
marks) and display it in dictionary

Source code:

# -*- coding: utf-8 -*-

"""

Created on Sat Apr 8 08:59:40 2023

@author: it291

"""

n=int(input("Enter how many students details you want:"))

s={}

for i in range(n):

print("enter {} student regd no:",format(i+1))

reg = input()

print("enter {} student marks:",format(i+1))

m=input()

if reg not in s:

s[reg]=m

print("list of students details",s)

Output:

36
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

37
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

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:

# -*- coding: utf-8 -*-

"""

Created on Sat Apr 8 09:15:33 2023

@author: it291

"""

text=input()

d={}

for item in text.split():

x=int(item)

d[item]=x*x

print(d)

Output:

38
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

PROGRAM 41

Aim: write a python program on mapping using dictionaries

Create a mapping from 3 character month name to month number

Hint for example {‘jan’:1, ‘dec’:12}

Ask the user for a 3 character month code lower /upper mixed

Print the month number corresponding to month the user entered

For example if the user enters ‘jul’, print 7

Source code:

# -*- coding: utf-8 -*-

"""

Created on Sat Apr 8 09:21:45 2023

@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

REGD NO: Y21AIT484

Output:

40
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

Program 42

Aim: write a python program to un pair a tuple into several variables.

Source code:

# -*- coding: utf-8 -*-

"""

Created on Wed Jul 1 09:15:00 2023

@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

REGD NO: Y21AIT484

Aim: write a python program to add an item to a tuple.

Source code:

# -*- coding: utf-8 -*-

"""

Created on Wed Jul 1 09:20:04 2023

@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

REGD NO: Y21AIT484

Aim: write a python program to read list of integers from keyboard and display in
tuple.

Source code:

# -*- coding: utf-8 -*-

"""

Created on Wed Jul 1 09:25:04 2023

@author: it291

"""

n=int(input("how any integers do you want to enter"))

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

REGD NO: Y21AIT484

Source code:

# -*- coding: utf-8 -*-


"""
Created on Fri Jul 7 14:57:44 2023
@author: it291
"""
while True:
n=input("Enter how many strings you want")
a=()
for i in range(int(n)):
print("Enter {} strings".format(i+1))
s=input()
a=a+(s,)
print(a)
print(tuple(reversed(a)))
ch=input("Do you want to continue Y|N")
if ch=='y' or ch=='Y':
continue
else:
Break
Output:

Program 46

44
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

Aim: write a python program to add two numbers using recursion.

Source code:

# -*- coding: utf-8 -*-

"""

Created on Wed Jul 1 09:34:04 2023

@author: it291

"""

def add(a,b):

c=a+b

return c

a=15

b=20

print("addition of two numbers is",add(a,b))

Output:

Program 47

45
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

Aim: write a python program of the default arguments.

Source code:

# -*- coding: utf-8 -*-

"""

Created on Wed Jul 1 09:39:04 2023

@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

REGD NO: Y21AIT484

Aim: write a python program of the arbitrary arguments.

Source code:

# -*- coding: utf-8 -*-

"""

Created on Wed Jul 1 09:45:04 2023

@author: it291

"""

def find_sum(*numbers):

result=0

for num in numbers:

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

REGD NO: Y21AIT484

Aim: write a python program define a function called power2() which takes a single
argument x but returns 2pow(x).

Source code:

# -*- coding: utf-8 -*-

"""

Created on Fri Jul 7 14:43:20 2023

@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

REGD NO: Y21AIT484

Aim: write a python program to add two numbers using positional arguments.

Source code:

# -*- coding: utf-8 -*-

"""

Created on Fri Jul 7 15:28:21 2023

@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

REGD NO: Y21AIT484

Aim: write a python program to define a function call message which contain 2
positional
Source code:

# -*- coding: utf-8 -*-

"""

Created on Sat Jul 8 07:32:32 2023

@author: it291

"""

def message(a,b,c,d=29):

print("two positional argument is ",a,b,)

print("one keyword argument is",c)

print("one keyword argument is",d)

print(message(84,124,c=93,d=99))

Output:

Program 52

50
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

Aim: write a python program to print yesterday today tomorrow.

Source code:

## -*- coding: utf-8 -*-


"""
Created on Fri Jul 7 15:20:21 2023
@author: it291
"""

from datetime import date,timedelta


d=date.today()
a=d+timedelta(days=1)
b=d-timedelta(days=1)
print(a,b)
Output:

Program-53

51
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

Aim :Write a python program to calculate the number of days between two given dates.

Source Code:

# -*- coding: utf-8 -*-

"""

Created on Tue Jul 6 15:28:08 2023

@author: it291

"""

from datetime import datetime

d1=input("enter first date(dd/mm/yyyy)")

d2=input("enter second date(dd/mm/yyyy)")

d1=datetime.strptime(d1,"%d/%m/%Y")

d2=datetime.strptime(d2,"%d/%m/%Y")

if d1>d2:

print("the difference between two given dates",d1-d2)

else:

print("the difference between two given dates",d2-d1)

Output:

Program-54

52
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

Aim: Write a python program to print yesterday, today and tomorrow.

Source Code:

# -*- coding: utf-8 -*-

"""

Created on Tue Jul 7 13:48:08 2023

@author: it291

"""

from datetime import timedelta,date

d=date.today()

print("Today's date is",d)

yd=d-timedelta(days=1)

td=d+timedelta(days=1)

print("Yesterday's date is",yd)

print("Tomorrow's date is",td)

Output:

Program 55

53
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

Aim: Write a python program to convert given string into daytime object.

Source code:

# -*- coding: utf-8 -*-


"""
Created on Fri Jul 7 15:10:21 2023
@author: it291
"""
from datetime import datetime
d=input("Enter any date :")
a=datetime.strptime(d,"%Y%m%d")
print(a)
Output:

Program 56

54
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

Aim: write a python program to generate 5 random integers from given list of integers.

Source code:

# -*- coding: utf-8 -*-

"""

Created on Wed Jul 1 09:53:04 2023

@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

REGD NO: Y21AIT484

Aim: write a python program to generate random integer between

5 to 50 excluding 50
a. -50 to 50
b. 5 to 50 with step of 3

Source code:

# -*- coding: utf-8 -*-

"""

Created on Tue Jul 6 13:20:08 2023

@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

REGD NO: Y21AIT484

Aim: write a python program to generate 6 digit random integers.

Source code:

# -*- coding: utf-8 -*-

"""

Created on Tue Jul 6 13:26:08 2023

@author: it291

"""

import random

print(random.randrange(100000,1000000))

Output:

Program 59

57
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

Aim: write a python program to generate a random password which means the
following conditions.

1. Password length must be 10 characters long


2. It must contain 2 uppercase letters and one digit and one special symbol

Source code:

# -*- coding: utf-8 -*-

"""

Created on Fri Jul 7 14:25:49 2023

@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

REGD NO: Y21AIT484

Source code:

# -*- coding: utf-8 -*-


"""
Created on Fri Jul 7 14:50:02 2023
@author: it291
"""
import random
n=[12,23,45,67,65,43]
random.shuffle(n)
print(n)

Output:

Program 61

Aim: write a python program to find all occurrence of a given string.

Source code:

# -*- coding: utf-8 -*-

"""

Created on Tue Jul 6 14:30:08 2023

@author: it291

"""

import re

str=input("enter any string")

l=re.findall("a",str)

print(l)

Output:

Program 62

59
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

Aim: write a python program to replace all occurrences of 5 with five for given string.

Source code:

# -*- coding: utf-8 -*-

"""

Created on Tue Jul 6 14:40:08 2023

@author: it291

"""

import re

n=input("enter any string")

print(re.sub(r'(5)',"five",n))

Output:

Program 63

60
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

Aim: write a python program to print all the elements that contain either a or w.

Source code:

# -*- coding: utf-8 -*-

"""

Created on Tue Jul 6 14:55:08 2023

@author: it291

"""

import re

n=input("enter string separated by comma")

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

REGD NO: Y21AIT484

Aim: write a python program that matches the string has a followed by zero or more
b’s.

Source code:

# -*- coding: utf-8 -*-

"""

Created on Tue Jul 6 15:02:08 2023

@author: it291

"""

import re

n=input("enter any string")

if re.search(r'ab*',n):

print("match found")

else:

print("not found")

Output:

Program 65

62
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

Aim: write a python program to replace all occurrences of spaces commas with
underscore.

Source code:

# -*- coding: utf-8 -*-

"""

Created on Tue Jul 6 15:10:08 2023

@author: it291

"""

import re

n=input("enter ay string")

print(re.sub("[, ]","_",n))

Output:

Program 66

63
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

Aim: write a python program to find all five character words in a given string.

Source code:

# -*- coding: utf-8 -*-

"""

Created on Tue Jul 6 15:16:08 2023

@author: it291

"""

import re

n=input("enter any string")

l=re.findall("\w{5}",n)

print(l)

Output:

Program 67

64
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

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:

# -*- coding: utf-8 -*-

"""

Created on Tue Jul 6 15:22:08 2023

@author: it291

"""

import re

str="siva is a father of vinayaka"

l=re.findall(".*s",str)

print(l)

Output:

Program 68

65
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

Aim: python program that matches the string with any characters by s in a given string.

Source code:

# -*- coding: utf-8 -*-

"""

Created on Tue Jul 6 15:39:08 2023

@author: it291

"""

import re

str="siva is a smart and intelligent boy"

s=re.findall(".+s",str)

print(s)

Output:

Program 69

66
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

Aim: write a python program that matches the string any no of i’s before s in a given
string.

Source code:

# -*- coding: utf-8 -*-

"""

Created on Fri Jul 7 14:10:49 2023

@author: it291

"""

import re

str="this iis a thrusday and iiit is a festiival"

l=re.findall("i*s",str)

print(l)

Output:

Program 70

67
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

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:

# -*- coding: utf-8 -*-

"""

Created on Fri Jul 7 14:35:49 2023

@author: it291

"""

a=open("bec.txt","a")

n=input("enter any string")

a.write(n)

a.close()

a=open("bec.txt","r")

s=a.read()

a.close()

print("\ncontents of given file is",s)

Output:

Program 71

68
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

Aim: write a python program to read the contents of file and display it on the screen.

Source code:

# -*- coding: utf-8 -*-

"""

Created on Wed Jun 21 15:23:02 2023

@author: it291

"""

a=open("bec.txt","r")

s=a.read()

a.close()

print("content of given file is",s)

Output:

Program 72

69
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

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:

# -*- coding: utf-8 -*-

"""

Created on Wed Jun 21 15:30:43 2023

@author: it291

"""

a=open("bapatala.txt","a")

n=input("enter any string")

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

REGD NO: Y21AIT484

Aim: write a python program to write some content on file bec.txt and count no of
words start with s.

Source code:

# -*- coding: utf-8 -*-

"""

Created on Wed Jun 21 15:38:32 2023

@author: it291

"""

a=open("bec.txt","a")

n=input("enter any string")

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

REGD NO: Y21AIT484

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:

# -*- coding: utf-8 -*-

"""

Created on Wed Jun 21 15:46:12 2023

@author: it291

"""

f=open("bec.txt","w")

n=input("enter any string")

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

REGD NO: Y21AIT484

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:

# -*- coding: utf-8 -*-

"""

Created on Wed Jun 21 15:51:19 2023

@author: it291

"""

def count():

f=open("bec.txt","r")

cnt=0

for line in f:

if line[0]!='t' and line[0]!='T':

cnt+=1

print("count=",cnt)

count()

Output:

Program 76

73
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

Aim: write a python program to implement the stack using list

Source code:

# -*- coding: utf-8 -*-

"""

Created on Sat Jul 8 08:01:50 2023

@author: it291

"""

def push():

ele=int(input("which element do you want to insert"))

list.append(ele)

print("inserted successfully")

def pop():

if len(list)==0:

print("stack is empty")

else:

print("deleted element is{}".format(list.pop()))

print("deleted successfully")

def display():

n=len(list)

if n==0:

print("stack is empty")

else:

print("stack element are")

for i in list[::-1]:

74
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

print(i)

list=[]

while True:

print("stack using list")

print("1.push\n2.pop\n3.display")

ch=int(input("Enter your choice:"))

if ch==1:

push()

elif ch==2:

pop()

elif ch==3:

display()

else:

printf("invalid choice")

c=input("do you want to continue Y/N")

if c=='y' or c=='Y':

continue

else:

break

Output:

75
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

76
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

Program 77

Aim: write a python program to implement the Queue using list

Source code:

77
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

# -*- coding: utf-8 -*-

"""

Created on Sat Jul 8 08:31:44 2023

@author: it291

"""

def enqueue():

ele=int(input("Which element do you want to insert"))

list.append(ele)

print("inserted successfully")

def dequeue():

if len(list)==0:

print("queue is empty")

else:

print("deleted element is{}".format (list.pop()))

print("deleted successfully")

def display():

n=len(list)

if n==0:

print("queue is empty")

else:

print("queue elements are")

for i in range(n):

print(list[i])

list=[]

78
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

while True:

print("queue using list")

print("1.enqueue\n2.dequeue\n3.display")

ch=int(input("Enter your choice:"))

if ch==1:

enqueue()

elif ch==2:

dequeue()

elif ch==3:

display()

else:

print("invalid choice")

c=input("do you want to continue Y/N")

if c=='y' or c=='Y':

continue

else:

break

Output:

79
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

80
DEPARTMENT OF INFORMATION TECHNOLOGY
PYTHON PROGRAMMING LAB

REGD NO: Y21AIT484

81
DEPARTMENT OF INFORMATION TECHNOLOGY

You might also like