Insertion and bubble sort
def bubble(lst):
n = len(lst)
print("The list before sorting: ", lst)
for i in range (0,n):
for j in range (0, n-i-1):
if lst[j]>lst[j+1]:
lst[j],lst[j+1]=lst[j+1],lst[j]
print("litterary list: ", lst)
print("Sorted list: ", lst)
def insertion(lst):
n = len(lst)
print("The list before sorting: ", lst)
for i in range (1, n):
key = lst[i]
j = i-1
while (j >= 0 and key < lst[j]):
lst[j+1] = lst [j]
j -= 1
else:
lst[j+1] = key
print("litterary list: ", lst)
print("The final sorted list: ", lst)
ch='y'
while [Link]()=='y':
l=eval(input('Enter your list to be sorted: '))
sort_type=int(input('\nChoose the type of sorting you want:\n1. Bubble Sort \n2. Insertion Sort\nYour choice :'))
if sort_type ==1:
bubble(l)
elif sort_type ==2:
insertion(l)
ch=input("\nDo you want to continue? y/n: ")
Output:
Enter your list to be sorted: [12,1,3,23,431,0]
Choose the type of sorting you want:
1. Bubble Sort
2. Insertion Sort
Your choice :1
The list before sorting: [12, 1, 3, 23, 431, 0]
litterary list: [1, 12, 3, 23, 431, 0]
litterary list: [1, 3, 12, 23, 431, 0]
litterary list: [1, 3, 12, 23, 431, 0]
litterary list: [1, 3, 12, 23, 431, 0]
litterary list: [1, 3, 12, 23, 0, 431]
litterary list: [1, 3, 12, 23, 0, 431]
litterary list: [1, 3, 12, 23, 0, 431]
litterary list: [1, 3, 12, 23, 0, 431]
litterary list: [1, 3, 12, 0, 23, 431]
litterary list: [1, 3, 12, 0, 23, 431]
litterary list: [1, 3, 12, 0, 23, 431]
litterary list: [1, 3, 0, 12, 23, 431]
litterary list: [1, 3, 0, 12, 23, 431]
litterary list: [1, 0, 3, 12, 23, 431]
litterary list: [0, 1, 3, 12, 23, 431]
Sorted list: [0, 1, 3, 12, 23, 431]
Do you want to continue? y/n: y
Enter your list to be sorted: [12,1,3,23,431,0]
Choose the type of sorting you want:
1. Bubble Sort
2. Insertion Sort
Your choice :1
The list before sorting: [12, 1, 3, 23, 431, 0]
litterary list: [1, 12, 3, 23, 431, 0]
litterary list: [1, 3, 12, 23, 431, 0]
litterary list: [1, 3, 12, 23, 431, 0]
litterary list: [1, 3, 12, 23, 431, 0]
litterary list: [1, 3, 12, 23, 0, 431]
litterary list: [1, 3, 12, 23, 0, 431]
litterary list: [1, 3, 12, 23, 0, 431]
litterary list: [1, 3, 12, 23, 0, 431]
litterary list: [1, 3, 12, 0, 23, 431]
litterary list: [1, 3, 12, 0, 23, 431]
litterary list: [1, 3, 12, 0, 23, 431]
litterary list: [1, 3, 0, 12, 23, 431]
litterary list: [1, 3, 0, 12, 23, 431]
litterary list: [1, 0, 3, 12, 23, 431]
litterary list: [0, 1, 3, 12, 23, 431]
Sorted list: [0, 1, 3, 12, 23, 431]
Do you want to continue? y/n: n
Linear and binary search
def linear(l, e):
for i in range(len(l)):
if l[i]==e:
print("element", e, "found in position", i+1)
break
else:
print("element", e, "not found")
def binary(l, e):
first = 0
last = len(l)-1
while first <= last:
mid = (first + last)//2
if l[mid] == e:
print("Element", e, "found in position:", mid+1)
break
elif l[mid] < e:
first = mid+1
else:
last = mid-1
else:
print("Element not found")
lst=eval(input("Enter your list: "))
ch='y'
while [Link]()=='y':
print("\nSearch Algorithm:\[Link] Search\[Link] Search")
s=int(input("Enter the search algorithm you want to use:"))
ele=int(input("Enter the element you are searching for:"))
if s==1:
linear(lst,ele)
elif s==2:
binary(lst,ele)
ch=input("\nDo you want to continue? y/n: ")
Output
Enter your list: [1,3,4,2,5]
Search Algorithm:
[Link] Search
[Link] Search
Enter the search algorithm you want to use:1
Enter the element you are searching for:4
element 4 found in position 3
Do you want to continue? y/n: y
Search Algorithm:
[Link] Search
[Link] Search
Enter the search algorithm you want to use:2
Enter the element you are searching for:5
Element 5 found in position: 5
Do you want to continue? y/n: y
Search Algorithm:
[Link] Search
[Link] Search
Enter the search algorithm you want to use:1
Enter the element you are searching for:6
element 6 not found
Do you want to continue? y/n: y
Search Algorithm:
[Link] Search
[Link] Search
Enter the search algorithm you want to use:2
Enter the element you are searching for:6
Element not found
Do you want to continue? y/n: n
Minimum and maximum, perfect number values in a tuple
def maxmin(t):
min=t[0]
max=t[0]
for i in t:
if min>i:
min=i
elif max<i:
max=i
print("maximum value in the tuple is: ",max)
print("minimum value in the tuple is: ",min)
def perfect_number(t):
t1=()
for i in t:
f=0
for j in range(1,i):
if i%j==0:
f+=j
if f==i:
t1+=(i,)
if len(t1)!=0:
print(t1)
else:
print("no perfect number in the tuple")
ch='y'
while [Link]()=='y':
print("\n1. Find Maximum and Mminimum NUmber in the tuple\n2. Find perfect numbers in the tuple\n3. Exit")
ans = int(input("Enter your choice: "))
if ans==1:
t = eval(input("Enter the tuple: "))
maxmin(t)
elif ans==2:
t = eval(input("Enter the tuple: "))
perfect_number(t)
elif ans==3:
break
ch=input("\nDo you want to continue? y/n: ")
Output
1. Find Maximum and Mminimum NUmber in the tuple
2. Find perfect numbers in the tuple
3. Exit
Enter your choice: 1
Enter the tuple: (1,0,1000)
maximum value in the tuple is: 1000
minimum value in the tuple is: 0
Do you want to continue? y/n: y
1. Find Maximum and Mminimum NUmber in the tuple
2. Find perfect numbers in the tuple
3. Exit
Enter your choice: 2
Enter the tuple: (1,6,28,100)
(6, 28)
Do you want to continue? y/n: n
Sum of prime numbers In the list and second biggest element in the list
def prime_sum(lst):
l=[]
for i in lst:
if i>1:
for j in range(2,i):
if i%j==0:
break
else:
[Link](i)
return sum(l)
def second_biggest(lst):
[Link]()
return lst[-2]
ch='y'
while [Link]()=='y':
print("\[Link] Of All Prime Numbers\n2. Second Biggest Number\n3. Exit")
ans = int(input("Enter your choice: "))
if ans==1:
lst = eval(input("Enter list: "))
print(prime_sum(lst))
elif ans==2:
lst = eval(input("Enter list: "))
print(second_biggest(lst))
elif ans==3:
break
ch=input("\nDo you want to continue? y/n: ")
Output
[Link] Of All Prime Numbers
2. Second Biggest Number
3. Exit
Enter your choice: 1
Enter list: [2,4,6,8,9,3,5]
10
Do you want to continue? y/n: y
[Link] Of All Prime Numbers
2. Second Biggest Number
3. Exit
Enter your choice: 2
Enter list: [2,4,6,8,9,3,5]
8
Do you want to continue? y/n: n
Replace a character in string and string palindrome:
def replace(s):
i=int(input("enter the index you want to replace: "))
rw=input("enter the character: ")
s=s[:i]+rw+s[i+1:]
print(s)
def stringpalindrome(s):
for i in range(len(s)//2):
bi = len(s)-(i-1)
if s[i] != s[bi]:
print("not a palindrome")
break
else:
print("palindrome")
ch='y'
while [Link]()=='y':
print("\n1. Replace\n2. String Palindrome\n3. Exit")
ans = int(input("Enter your choice: "))
if ans==1:
s = input("Enter the string: ")
replace(s)
elif ans==2:
s = input("Enter the string: ")
stringpalindrome(s)
elif ans==3:
break
ch=input("\nDo you want to continue? y/n: ")
Output:
1. Replace
2. String Palindrome
3. Exit
Enter your choice: 1
Enter the string: naman
enter the index you want to replace: 2
enter the character: a
naaan
Do you want to continue? y/n: y
1. Replace
2. String Palindrome
3. Exit
Enter your choice: 2
Enter the string: naman
palindrome
Do you want to continue? y/n: y
1. Replace
2. String Palindrome
3. Exit
Enter your choice: 2
Enter the string: jai
not a palindrome
Do you want to continue? y/n: n
Counting characters in a string and longest word in a string
def counting():
s=input("Enter a string:")
uv=uc=lc=lv=dig=spl=0
for ch in s:
if [Link]():
if [Link]():
if [Link]():
if ch in "AEIOU":
uv+=1
else:
uc+=1
else:
if ch in "aeiou":
lv+=1
else:
lc+=1
else:
dig+=1
else:
spl+=1
print("[Link] Uppercase Vowels:",uv)
print("[Link] Uppercase Consonants:",uc)
print("[Link] Lowercase Vowels:",lv)
print("[Link] Lowercase Consonants:",lc)
print("[Link] Digits:",dig)
print("[Link] Special Characters:",spl)
def longest(s):
s=[Link]()
L=[]
for i in s:
l=len(i)
[Link](l)
m=max(L)
for j in s:
if len(j)==m:
print(j)
ans="y"
while [Link]()=="y":
print("[Link] Counting of Characters")
print("[Link] Longest Word In A String")
choice=int(input("Enter your choice:"))
if choice==1:
counting()
ans=input("Would you like to continue:(y/n)")
if choice==2:
s=input("Enter a string:")
longest(s)
ans=input("Would you like to continue:(y/n)")
output
[Link] Counting of Characters
[Link] Longest Word In A String
Enter your choice:1
Enter a string:Naman@123
[Link] Uppercase Vowels: 0
[Link] Uppercase Consonants: 1
[Link] Lowercase Vowels: 2
[Link] Lowercase Consonants: 2
[Link] Digits: 3
[Link] Special Characters: 1
Would you like to continue:(y/n)y
[Link] Counting of Characters
[Link] Longest Word In A String
Enter your choice:2
Enter a string:Naman is A good boyyyy
boyyyy
Would you like to continue:(y/n)n
Frequency of characters in a string to dict and checking the first letter words in a list
def frequency(s):
d={}
for i in s:
if i not in d:
d[i]=1
else:
d[i]+=1
print(d)
def firstch(l):
d={}
for i in l:
n=i[0]
if n in d:
d[n].append(i)
else:
d[n]=[i]
print(d)
ans="y"
while [Link]()=="y":
print("[Link] Frequency Of Characters In A String ")
print("[Link] First Letter Of Words In A String In A Dictionary")
choice=int(input("Enter your choice:"))
if choice==1:
s=input("Enter a string:")
frequency(s)
ans=input("Would you like to continue:(y/n)")
if choice==2:
l=eval(input("Enter your list:"))
firstch(l)
ans=input("Would you like to continue:(y/n)")
Output
[Link] Frequency Of Characters In A String
[Link] First Letter Of Words In A String In A Dictionary
Enter your choice:1
Enter a string:naman
{'n': 2, 'a': 2, 'm': 1}
Would you like to continue:(y/n)y
[Link] Frequency Of Characters In A String
[Link] First Letter Of Words In A String In A Dictionary
Enter your choice:2
Enter your list:["naman", "jain"]
{'n': ['naman'], 'j': ['jain']}
Would you like to continue:(y/n)n
Text file 1
def create():
f=open("[Link]","w")
n=int(input("Enter how many lines to be created:"))
for i in range(n):
t=input("Enter the line:")
[Link](t+"\n")
if (f):
print("File Created Successfully")
else:
print("Error")
[Link]()
def read():
try:
f=open("[Link]","r")
except:
print("no file found")
create()
f=open("[Link]","r")
data=[Link]()
wds=[Link]()
lines=[Link]('\n')
print("[Link] characters:",len(data))
print("[Link] Words:",len(wds))
print("[Link] Lines:",len(lines)-1)
[Link]()
def add():
try:
f=open("[Link]","r")
except:
print("no file found")
create()
f=open("[Link]","a")
n=int(input("Enter how many lines to be added:"))
l=[]
for i in range(n):
t=input("Enter the line:")
[Link](t+"\n")
[Link](l)
ans="y"
while [Link]()=="y":
print("[Link] Create A Text File")
print("[Link] Read characters,Words And Lines in the Text File")
print("3. Add Data to the Text File")
choice=int(input("Enter your choice:"))
if choice==1:
create()
ans=input("Would you like to continue:(y/n)")
if choice==2:
read()
ans=input("Would you like to continue:(y/n)")
if choice==3:
add()
ans=input("Would you like to continue:(y/n)")
output:
[Link] Create A Text File
[Link] Read characters,Words And Lines in the Text File
3. Add Data to the Text File
Enter your choice:1
Enter how many lines to be created:3
Enter the line:hi
Enter the line:this is program for
Enter the line:text file 001
File Created Successfully
Would you like to continue:(y/n)y
[Link] Create A Text File
[Link] Read characters,Words And Lines in the Text File
3. Add Data to the Text File
Enter your choice:2
[Link] characters: 38
[Link] Words: 8
[Link] Lines: 3
Would you like to continue:(y/n)y
[Link] Create A Text File
[Link] Read characters,Words And Lines in the Text File
3. Add Data to the Text File
Enter your choice:3
Enter how many lines to be added:1
Enter the line:hope you like it
Would you like to continue:(y/n)y
[Link] Create A Text File
[Link] Read characters,Words And Lines in the Text File
3. Add Data to the Text File
Enter your choice:2
[Link] characters: 55
[Link] Words: 12
[Link] Lines: 4
Would you like to continue:(y/n)n
text file 2
def createf1():
f=open("[Link]","w")
n=int(input("Enter how many lines to be created:"))
for i in range(n):
t=input("Enter the line:")
[Link](t+"\n")
if (f):
print("File Created Successfully")
else:
print("Error")
[Link]()
def fcopy():
f=open("[Link]","r")
l=open("[Link]","w")
ch=input("The Letter Who's Corresponding Line To Be Printed:")
for i in [Link]():
if ch==i[0]:
[Link](i)
print("copied succesfuly")
[Link]()
[Link]()
def capcopy():
f=open("[Link]","r")
l=open("[Link]","w")
for i in [Link]():
i=[Link]()
[Link](i)
print("copied succesfuly")
[Link]()
[Link]()
def revcopy():
f=open("[Link]","r")
l=open("[Link]","w")
for i in [Link]():
[Link](i[::-1])
print("copied succesfuly")
[Link]()
[Link]()
ans="y"
while [Link]()=="y":
print("[Link] A Text File")
print("[Link] from file 1 to file 2 on basis of the first character")
print("[Link] from file 1 to file 2 by capitalizing first letter of every word")
print("[Link] from file 1 to file 2 by reversing every word")
choice=int(input("Enter your choice:"))
if choice==1:
createf1()
if choice==2:
fcopy()
if choice==3:
capcopy()
if choice==4:
revcopy()
ans=input('Do you want to continue y/n: ')
output
[Link] A Text File
[Link] from file 1 to file 2 on basis of the first character
[Link] from file 1 to file 2 by capitalizing first letter of every word
[Link] from file 1 to file 2 by reversing every word
Enter your choice:1
Enter how many lines to be created:3
Enter the line:hi
Enter the line:this is second program
Enter the line:for text file
File Created Successfully
Do you want to continue y/n: y
[Link] A Text File
[Link] from file 1 to file 2 on basis of the first character
[Link] from file 1 to file 2 by capitalizing first letter of every word
[Link] from file 1 to file 2 by reversing every word
Enter your choice:2
The Letter Who's Corresponding Line To Be Printed:t
copied succesfuly
Do you want to continue y/n: y
[Link] A Text File
[Link] from file 1 to file 2 on basis of the first character
[Link] from file 1 to file 2 by capitalizing first letter of every word
[Link] from file 1 to file 2 by reversing every word
Enter your choice:3
copied succesfuly
Do you want to continue y/n: y
[Link] A Text File
[Link] from file 1 to file 2 on basis of the first character
[Link] from file 1 to file 2 by capitalizing first letter of every word
[Link] from file 1 to file 2 by reversing every word
Enter your choice:4
copied succesfuly
Do you want to continue y/n: n
import pickle
def create():
f=open("[Link]","wb")
n=int(input("Enter how many travel tickets you want:"))
for i in range(n):
travel_id=int(input("Enter Travel ID:"))
start=input("Enter Start Journey:")
end=input("Enter End Journey:")
price=int(input("Enter Ticket Price:"))
L=[travel_id,start,end,price]
[Link](L,f)
if (f):
print("File Created Successfully")
else:
print("Error Occured")
[Link]()
def display():
f=open("[Link]","rb")
try:
print(" ID " , "Start " , "End " , "Price " )
while True:
det=[Link](f)
print(" ",det[0]," ",det[1]," ",det[2]," ",det[3]) Binary file 1
except:
[Link]()
def update(travel_id,price):
f=open("[Link]","rb+")
flag=False
try:
while True:
recpos=[Link]()
det=[Link](f)
if det[0]==travel_id:
det[-1]=price
[Link](recpos)
[Link](det,f)
flag=True
except:
if flag==False:
print("Error Occured")
else:
print("File Updated Successfully")
[Link]()
ans="y"
while [Link]()=="y":
print("[Link] To Create A Binary File")
print("[Link] To Display The File")
print("[Link] Updating The File")
choice=int(input("Enter your choice:"))
if choice==1:
create()
if choice==2:
display()
if choice==3:
travel_id=int(input("Enter Travel ID To Be Updated:"))
price=int(input("Enter Ticket Price:"))
update(travel_id,price)
ans=input("Would you like to continue:(y/n)")
Output
[Link] To Create A Binary File
[Link] To Display The File
[Link] Updating The File
Enter your choice:1
Enter how many travel tickets you want:3
Enter Travel ID:0001
Enter Start Journey:mas
Enter End Journey:del
Enter Ticket Price:6000
Enter Travel ID:0002
Enter Start Journey:del
Enter End Journey:mas
Enter Ticket Price:8000
Enter Travel ID:0003
Enter Start Journey:nyc
Enter End Journey:mas
Enter Ticket Price:12000
File Created Successfully
Would you like to continue:(y/n)y
[Link] To Create A Binary File
[Link] To Display The File
[Link] Updating The File
Enter your choice:2
ID Start End Price
1 mas del 6000
2 del mas 8000
3 nyc mas 12000
Would you like to continue:(y/n)y
[Link] To Create A Binary File
[Link] To Display The File
[Link] Updating The File
Enter your choice:3
Enter Travel ID To Be Updated:0001
Enter Ticket Price:9000
File Updated Successfully
Would you like to continue:(y/n)y
[Link] To Create A Binary File
[Link] To Display The File
[Link] Updating The File
Enter your choice:2
ID Start End Price
1 mas del 9000
2 del mas 8000
3 nyc mas 12000
Would you like to continue:(y/n)n
Binary file 2
import pickle
def create():
f=open("[Link]","wb")
n=int(input("Enter how many words and their meanings you want:"))
for i in range(n):
word=input("Enter A Word:")
meaning=input("Enter Meaning Of The Word:")
d={word:meaning}
[Link](d,f)
if (f):
print("File Created Successfully")
else:
print("Error Occured")
[Link]()
def display():
f=open("[Link]","rb")
try:
while True:
det=[Link](f)
print(det)
except:
[Link]()
def search(word):
f=open("[Link]","rb+")
try:
while True:
det=[Link](f)
for i in det:
if i==word:
print("Meaning of",word,":",det[i])
except:
[Link]()
ans="y"
while [Link]()=="y":
print("[Link] To Create A Binary File")
print("[Link] To Display The File")
print("[Link] To Search The Meaning Of The Word In The File")
choice=int(input("Enter your choice:"))
if choice==1:
create()
if choice==2:
display()
if choice==3:
word=input("Enter The Word Whose Meaning Is To Be Searched:")
search(word)
ans=input("Would you like to continue:(y/n)")
Output
[Link] To Create A Binary File
[Link] To Display The File
[Link] To Search The Meaning Of The Word In The File
Enter your choice:1
Enter how many words and their meanings you want:2
Enter A Word:Abandon
Enter Meaning Of The Word:cease to support or look after someone
Enter A Word:Abscond
Enter Meaning Of The Word:leave hurriedly and secretly, typically to avoid detection of or arrest for an unlawful
action such as theft
File Created Successfully
Would you like to continue:(y/n)y
[Link] To Create A Binary File
[Link] To Display The File
[Link] To Search The Meaning Of The Word In The File
Enter your choice:2
{'Abandon': 'cease to support or look after someone'}
{'Abscond': 'leave hurriedly and secretly, typically to avoid detection of or arrest for an unlawful action such as theft'}
Would you like to continue:(y/n)y
[Link] To Create A Binary File
[Link] To Display The File
[Link] To Search The Meaning Of The Word In The File
Enter your choice:3
Enter The Word Whose Meaning Is To Be Searched:Abscond
Meaning of Abscond : leave hurriedly and secretly, typically to avoid detection of or arrest for an unlawful action such
as theft
Would you like to continue:(y/n)n
CSV file 1
import csv
def create():
with open('[Link]','w', newline='') as f:
w = [Link](f)
a = 'y'
while [Link]()=='y':
empId = int(input('enter the id: '))
sal = int(input('enter salary: '))
deseg = input('enter desegnation: ')
l=[empId, sal, deseg]
[Link](l)
a = input('do you want to add more records: ')
def display():
with open('[Link]','r') as f:
r = [Link](f)
print('id' ,'salary', 'designation', sep='\t')
for i in r:
print(i[0], i[1], i[2], sep='\t')
def search(empid):
with open('[Link]','r') as f:
r = [Link](f)
for i in r:
if int(i[0])==empid:
print(i[0], i[1], i[2], sep='\t')
ans="y"
while [Link]()=="y":
print("[Link] A CSV File")
print("[Link] The Content File")
print("[Link] On Basis Of Employee Id")
choice=int(input("Enter your choice:"))
if choice==1:
create()
if choice==2:
display()
if choice==3:
empid=int(input('enter the employee Id: '))
search(empid)
ans=input("Would you like to continue:(y/n)")
Output
[Link] A CSV File
[Link] The Content File
[Link] On Basis Of Employee Id
Enter your choice:1
enter the id: 001
enter salary: 100000
enter desegnation: sales
do you want to add more records: y
enter the id: 0112
enter salary: 1000000000000
enter desegnation: tech
do you want to add more records: n
Would you like to continue:(y/n)y
[Link] A CSV File
[Link] The Content File
[Link] On Basis Of Employee Id
Enter your choice:2
id salary designation
1 100000 sales
112 1000000000000 tech
Would you like to continue:(y/n)y
[Link] A CSV File
[Link] The Content File
[Link] On Basis Of Employee Id
Enter your choice:3
enter the employee Id: 112
112 1000000000000 tech
Would you like to continue:(y/n)n
CSV file 2
import csv
def create():
with open('[Link]','w', newline='') as f:
w = [Link](f)
a = 'y'
while [Link]()=='y':
stuId = int(input('enter the id: '))
name = input('enter name: ')
s1 = int(input('enter subject 1 mark: '))
s2 = int(input('enter subject 2 mark: '))
s3 = int(input('enter subject 3 mark: '))
s4 = int(input('enter subject 4 mark: '))
s5 = int(input('enter subject 5 mark: '))
msum = s1+s2+s3+s4+s5
avg=msum/5
l=[stuId, name, msum, avg]
[Link](l)
a = input('do you want to add more records: ')
def display():
with open('[Link]','r') as f:
r = [Link](f)
print('id' ,'name', 'Sum of all the marks','avg. marks', sep='\t')
for i in r:
print(i[0], i[1], i[2], i[3], sep='\t')
def search(sid):
with open('[Link]','r') as f:
r = [Link](f)
for i in r:
if int(i[0])==sid:
print('Total marks of',i[1],'is',i[-2])
print('avrage marks of',i[1],'is',i[-1])
ans="y"
while [Link]()=="y":
print("[Link] A CSV File")
print("[Link] The Content File")
print("[Link] Based On StudentID")
choice=int(input("Enter your choice:"))
if choice==1:
create()
if choice==2:
display()
if choice==3:
sid=int(input('enter student Id: '))
search(sid)
ans=input("Would you like to continue:(y/n)")
Output
[Link] A CSV File
[Link] The Content File
[Link] Based On StudentID
Enter your choice:1
enter the id: 1
enter name: naman
enter subject 1 mark: 90
enter subject 2 mark: 89
enter subject 3 mark: 69
enter subject 4 mark: 100
enter subject 5 mark: 92
do you want to add more records: y
enter the id: 2
enter name: chavi
enter subject 1 mark: 60
enter subject 2 mark: 70
enter subject 3 mark: 72
enter subject 4 mark: 54
enter subject 5 mark: 93
do you want to add more records: n
Would you like to continue:(y/n)y
[Link] A CSV File
[Link] The Content File
[Link] Based On StudentID
Enter your choice:2
id name Sum of all the marks avg. marks
1 naman 440 88.0
2 chavi 349 69.8
Would you like to continue:(y/n)y
[Link] A CSV File
[Link] The Content File
[Link] Based On StudentID
Enter your choice:3
enter student Id: 1
Total marks of naman is 440
avrage marks of naman is 88.0
Would you like to continue:(y/n)n
Random program 1
import random
import string
def generate_password(length):
characters = string.ascii_letters + [Link] + [Link]
password = ''.join([Link](characters) for _ in range(length))
return password
def password_generator():
print("Welcome to the Password Generator!")
while True:
try:
length = int(input("Enter the desired length of the password: "))
if length <= 0:
print("Please enter a positive integer.")
continue
password = generate_password(length)
print("Generated Password:", password)
break
except ValueError:
print("Invalid input. Please enter a valid integer.")
password_generator()
Output:
Welcome to the Password Generator!
Enter the desired length of the password: 10
Generated Password: 2MS2Iy8=b$
def isEmpty(stk):
if len(stk)==0:
return True
else:
return False
def PUSH(stk,ele):
[Link](ele)
def POP(stk):
if isEmpty(stk):
return None
else:
item=[Link]()
return item
def PEEK(stk):
if isEmpty(stk):
print("Stack is empty! Underflow!")
else:
top=len(stk)-1
print("The topmost element is:",stk[top])
def DISPLAY(stk):
if isEmpty(stk):
print("Stack is empty!")
else:
top=len(stk)-1
print(stk[top],"<----TOP")
for i in range(top-1,-1,-1):
print(stk[i])
print("End of Stack")
stack=[]
while True:
print("[Link]-Enter elements into a stack")
print("[Link]-Delete element of the stack")
print("[Link]-See the topmost element")
print("[Link]-Display the stack")
print("[Link]")
ch=int(input("Enter your choice:"))
if ch==1:
n=int(input("Enter the number of elements you want to enter:"))
for i in range(n):
element=eval(input("Enter the element:"))
PUSH(stack,element)
elif ch==2:
delele=POP(stack)
if delele==None:
print("Stack is empty!! Underflow!")
else:
print("Deleted Item is:",delele)
elif ch==3:
PEEK(stack)
elif ch==4:
DISPLAY(stack)
elif ch==5:
print("Exited!")
break
else:print("Invalid choice:")
OUTPUT:
[Link]-Enter elements into a stack
[Link]-Delete element of the stack
[Link]-See the topmost element
[Link]-Display the stack
[Link]
Enter your choice:1
Enter the number of elements you want to enter:5
Enter the element:100
Enter the element:200
Enter the element:300
Enter the element:400
Enter the element:500
[Link]-Enter elements into a stack
[Link]-Delete element of the stack
[Link]-See the topmost element
[Link]-Display the stack
[Link]
Enter your choice:4
500 <----TOP
400
300
200
100
End of Stack
[Link]-Enter elements into a stack
[Link]-Delete element of the stack
[Link]-See the topmost element
[Link]-Display the stack
[Link]
Enter your choice:2
Deleted Item is: 500
[Link]-Enter elements into a stack
[Link]-Delete element of the stack
[Link]-See the topmost element
[Link]-Display the stack
[Link]
Enter your choice:4
400 <----TOP
300
200
100
End of Stack
[Link]-Enter elements into a stack
[Link]-Delete element of the stack
[Link]-See the topmost element
[Link]-Display the stack
[Link]
Enter your choice:3
The topmost element is: 400
[Link]-Enter elements into a stack
[Link]-Delete element of the stack
[Link]-See the topmost element
[Link]-Display the stack
[Link]
Enter your choice:4
400 <----TOP
300
200
100
End of Stack
[Link]-Enter elements into a stack
[Link]-Delete element of the stack
[Link]-See the topmost element
[Link]-Display the stack
[Link]
Enter your choice:5
Exited!
Book=[]
Message=["Stack is Empty!","Book Added","Book Deleted!","Underflow!"]
def Pushbook():
bkname=input("Enter the book title:")
price=int(input("Enter the price of the book:"))
[Link]([bkname,price])
print(Message[1])
def Popbook():
if Book==[]:
print(Message[3])
else:
[Link]()
print(Message[2]) 17
def dispbook():
if Book==[]:
print(Message[0])
else:
top=len(Book)-1
print(Book[top],"<----TOP")
for i in range(top-1,-1,-1):
print(Book[i])
while True:
print("[Link] new book(s)")
print("[Link] a book")
print("[Link] the books")
print("[Link]")
ch=int(input("Enter your choice:"))
if ch==1:
n=int(input("Enter the number of books you want to enter:"))
for i in range(n):
Pushbook()
elif ch==2:
Popbook()
elif ch==3:
dispbook()
elif ch==4:
print("Exited!")
break
else:
print("Invalid choice:")
OUTPUT:
[Link] new book(s)
[Link] a book
[Link] the books
[Link]
Enter your choice:1
Enter the number of books you want to enter:3
Enter the book title:Famous Five
Enter the price of the book:500
Book Added
Enter the book title:Secret Seven
Enter the price of the book:700
Book Added
Enter the book title:Gernimo Stilton
Enter the price of the book:600
Book Added
[Link] new book(s)
[Link] a book
[Link] the books
[Link]
Enter your choice:3
['Gernimo Stilton', 600] <----TOP
['Secret Seven', 700]
['Famous Five', 500]
[Link] new book(s)
[Link] a book
[Link] the books
[Link]
Enter your choice:2
Book Deleted!
[Link] new book(s)
[Link] a book
[Link] the books
[Link]
Enter your choice:3
['Secret Seven', 700] <----TOP
['Famous Five', 500]
[Link] new book(s)
[Link] a book
[Link] the books
[Link]
Enter your choice:2
Book Deleted!
[Link] new book(s)
[Link] a book
[Link] the books
[Link]
Enter your choice:2
Book Deleted!
[Link] new book(s)
[Link] a book
[Link] the books
[Link]
Enter your choice:3
Stack is Empty!
[Link] new book(s)
[Link] a book
[Link] the books
[Link]
Enter your choice:4
Exited!