You are on page 1of 25

Shri Gulab Rai Montessori Senior

Secondary School

Computer Science
Project Work
Session 2023-24

“ Programmes in Python ”

Submitted By Submitted To
Navya Gangwar Mr.Manish Saxena
XI-A1 PGT Computer
Science
Acknowledgement
I am extremely indebted to express my heartfelt gratitude
and vote of thanks to my Computer Science teacher and
mentor Mr. Manish Saxena Sir in tandem with my principal
Mr. Ranveer Singh Rawat Sir, for assisting me in the due
course of completion of this project. Their words of
encouragement have galvanized me to inculcate a spirit of
enquiry and research and develop a lasting interest in the
discipline of Computer Science. In addition to the
aforementioned people, I am obliged to thank my classmates
and friends for their camaraderie was crucial in the
completion of the project work. Lastly, I am beholden to my
parents and brother, whose support was vital for having
finished this assignment in due time.
Certificate
This is to certify that Pratham Gupta of Class XI - A1 has
proficiently completed the project- work undertaken on the
topic “Programmes in Python” during the academic session
2023-24. He has prepared the project under my guidance
and as per the norms and guidelines prescribed by the
Central board of secondary Education (CBSE).

PGT Computer Science Principal


Mr. Manish Saxena Mr. RS Rawat
Index
• Programmes Based on Sorting and Searching in
Lists
• Programmes Based on Strings
• Programmes Based on Tuple
• Programmes Based on Dictionary
• Programmes Based on List
Programmes Based on Searching &
Sorting

1. Program to enter “N” numbers in a list and sort them


in ascending order using Selection sort technique.

n=int(input('enter size'))
l1=[]
for i in range(n):
e=int(input('enter element:'))
l1.append(e)
print(l1)
for i in range(0,n-1):
min=l1[i]
p=i
for j in range (i+1,n):
if l1[j]<min:
min=l1[j]
p=j
l1[i],l1[j]=l1[j],l1[i]
print(l1)
Output:

2. Program to enter “N” numbers in a list and sort them


in descending order using Bubble sort technique.
n=int(input('enter size'))
l1=[]
for i in range(n):
e=int(input('enter element:'))
l1.append(e)
print(l1)
for i in range(0,n-1):
for j in range (0,n-1-i):
if l1[j]<l1[j+1]:
l1[j],l1[j+1]=l1[j+1],l1[j]
print(l1)
Output:
3. Program to enter “N” numbers in a list and sort them
in ascending order using Insertion sort technique.
n=int(input('enter size'))
l1=[]
for i in range(n):
e=int(input('enter element:'))
l1.append(e)
print(l1)
for i in range(0,n):
num=l1[i]
j=i-1
while j>=0:
if l1[j]>num:
l1[j+1]=l1[j]
else:
break
j-=1
l1[j+1]=num
print(l1)
Output:

4. Program to enter “N” numbers in a List and display


existence of an element in the list using Linear Search.
n=int(input('enter size'))
l1=[]
for i in range(n):
e=int(input('enter element:'))
l1.append(e)
print(l1)
num=int(input('enter elemnet'))
f=0
for i in l1:
if num==i:
f=1
if f==1:
print('present')
else:
print('not')
Output:

5. Program to accept “N” numbers in a List in descending


order. Search for the existence of an element in a List
using Binary search.
n=int(input('enter size'))
l1=[]
for i in range(n):
e=int(input('enter element:'))
l1.append(e)
print(l1)
num=int(input('enter element'))
beg=0
end=n-1
while beg<=end:
mid=(beg+end)//2
if num==l1[mid]:
print('present at',mid)
break
elif num>=l1[mid]:
end=mid-1
else:
beg=mid+1
else:
print('not')
Output:
Programmes Based on Strings
1. Write a Python program to count the number of
characters (character frequency) in a string.
n=input('enter string:')
l1={}
for i in n:
if i in l1:
l1[i]=l1[i]+1
else:
l1[i]=1
print(l1)
Output:

2. Write a Python program to get a string made of the


first 2 and last 2 characters of a given string. If the string
length is less than 2, return the empty string instead.
n=input('enter string')
if len(n)>1:
print(n[0]+n[1]+n[-2]+n[-1])
else:
print(“’’”)
Output:

3. Write a Python program to get a string from a given


string where all occurrences of its first char have been
changed to '$', except the first char itself.
n=input('enter string:')
a=''
l=[]
for i in range(len(n)):
if n[i] not in l:
a=a+n[i]
l.append(n[i])
else:
a=a+'$'
print(a)
Output:
4. Write a Python program to get a single string from two
given strings, separated by a space and swap the first
two characters of each string.
s1=input('enter first string: ')
s2=input('enter second string: ')
a=s2[0]+s2[1]
b=s1[0]+s1[1]
for i in range(2,len(s1)):
a+=s1[i]
for i in range(2,len(s2)):
b+=s2[i]
print(a,b)
Output:

5. Write a Python program to add 'ing' at the end of a


given string (length should be at least 3). If the given
string already ends with 'ing', add 'ly' instead. If the
string length of the given string is less than 3, leave it
unchanged
s1=input('enter string:')
a=s1[-1]+s1[-2]+s1[-3]
if len(s1) >=3 and a=='gni' or a=='GNI':
s1+='ly'
print(s1)
elif len(s1)>=3:
s1+='ing'
print(s1)
else:
print(s1)
Output:
Programmes Based on Tuples
1. Write a Python program to test if a variable is a list or
tuple or a set.
a=eval(input('enter element'))
b=str(a)
if b[0]=='[' and b[-1]==']':
print('list')
elif b[0]=='(' and b[-1]==')':
print('tuple')
else:
print('neither list nor tuple')
Output:

2. Write a Python program to sort a list of tuples by the


second Item.
a=eval(input('enter element'))
for i in range(len(a)-1):
for j in range(i,len(a)):
if a[i][1]>a[j][1]:
a[i],a[j]=a[j],a[i]
print(a)
Output:

3. Write a Python program which accepts a sequence of


comma-separated numbers from user and generate a
list and a tuple with those numbers.
n=eval(input('enter sequence'))
l=[]
for i in n:
l.append(i)
print(l,tuple(l))
Output:

4. Write a Python program to sort a list of tuples


alphabetically.
b=eval(input('enter element'))
a=list(b)
for i in range(len(a)-1):
for j in range(i,len(a)):
if a[i]>a[j]:
a[i],a[j]=a[j],a[i]
print(tuple(a))
Output:

5. Write a python program to Check if the given element


is present in tuple or not.
l1=eval(input('enter tuple'))
num=int(input('enter elemnet'))
f=0
for i in l1:
if num==i:
f=1
if f==1:
print('present')
else:
print('not')
Output:
Programmes Based on Dictionary

1. Write a Python script to sort (ascending and


descending) a dictionary by value.
d1=eval(input('enter dictionary'))
l1=[]
d3={}
d4={}
for i in d1:
l1.append(d1[i])
l2=l1.copy()
for i in range(len(l1)-1):
for j in range(0,len(l1)-1-i):
if l1[j]>l1[j+1]:
l1[j],l1[j+1]=l1[j+1],l1[j]
for j in range(len(l2)):
for i in range(0,len(l2)-1-j):
if l2[i]<l2[i+1]:
l2[i],l2[i+1]=l2[i+1],l2[i]
i=0
for i in l1:
for j in d1:
if d1[j]==i:
d3[j]=d1[j]
print(d3)
for i in l2:
for j in d1:
if d1[j]==i:
d4[j]=d1[j]
print(d4)
Output:

2. Write a Python script to concatenate the following


dictionaries to create a new one.
n=int(input('enter no. of dict:'))
D={}
for i in range(n):
d=eval(input('enter dict:'))
for r in d:
D[r]=d[r]
print(D)
Output:

3. Write a Python script to check whether a given key


already exists in a dictionary.
d=eval(input('enter dict:'))
k=eval(input('enter key:'))
f=0
for r in d:
if r==k:
f=1
if f==1:
print('already exist')
else:
print('do not exist')
Output:

4. Write a Python script to print a dictionary where the


keys are numbers between 1 and 15 (both included) and
the values are the square of the keys.
d={}
for i in range(1,16):
d[i]=i**2
print(d)
Output:

5. Write a Python program to get the maximum and


minimum values of a dictionary.
d=eval(input('enter dict:'))
l=[]
for i in d:
l.append(d[i])
for j in range(len(l)-1):
for i in range(len(l)-1-j):
if l[i]>l[i+1]:
l[i],l[i+1]=l[i+1],l[i]
print('max:',l[-1],'min:',l[0])
Output:
Programmes Based on List
1. Program to remove first occurrence of a given
element in the list
l=eval(input('enter list'))
n=eval(input('enter element'))
l2=[]
for i in range (len(l)):
if l[i]!=n:
l2.append(l[i])
else:
break
for r in range(i+1,len(l)):
l2.append(l[r])
print(l2)
Output:

2. Program to remove duplicate elements from the list


l1=eval(input('enter list'))
l2=[]
for i in l1:
if i not in l2:
l2.append(i)
print(l2)
Output:

3. Create two lists with first half and second half


elements of a list
l1=eval(input('enter list'))
l3,l2=[],[]
for i in range(len(l1)):
if i<(len(l1)//2):
l2.append(l1[i])
else:
l3.append(l1[i])
print(l2,l3)
Output:
4. Python program to swap any two elements in the list
l1=eval(input('enter list'))
r,m=eval(input('enter element'))
a,b='',''
for i in range(len(l1)):
if l1[i]==r:
a=i
if l1[i]==m:
b=i
l1[int(a)],l1[int(b)]=l1[int(b)],l1[int(a)]
print(l1)
Output:

5. Python program to check if elements of a list are in


ascending order, descending order or in random order.
l1=eval(input('enter list'))
l2,l3=[],[]
for i in l1:
l2.append(i)
l3.append(i)
for i in range(len(l2)-1):
for j in range(i+1,len(l2 )):
if l2[i]>l2[j]:
l2[i],l2[j]=l2[j],l2[i]
if l3[i]<l3[j]:
l3[i],l3[j]=l3[j],l3[i]
if l1==l2:
print('accending')
elif l1==l3:
print('descending')
else:
print('random')
Output:

You might also like