0% found this document useful (0 votes)
13 views7 pages

ANSWERS of Practical

Uploaded by

Rizheen Abbas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views7 pages

ANSWERS of Practical

Uploaded by

Rizheen Abbas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

L=[1,2,3,4,5]
while True:
print('''
===============================Menu========================================
Choose an option:
1. Append an element
2. Insert an element
3. Append a list to the given list
4 Modify an existing element
5 Delete an existing element from its position
6 Delete an existing element with a given value
7 Sort the list in ascending order
8 Sort the list in descending order
9 Display the list
10 Exit''')
ch=int(input('enter your choice: '))
if ch==1:
element=input('enter the element:')
L.append(element)
print(L)
elif ch == 2:
print(L)
element=input('enter the element:')
pos=int(input('enter the position:'))
L.insert(pos,element)
print(L)
elif ch==3:
print('given list:',L)
L2=[]
n=int(input('enter the no of elements in the new list:'))
for i in range (n):
element=input('enter an element:')
L2.append(element)
print('new list: ',L2)
L.append(L2)
print(L)
elif ch == 4 :
print(L)
element=int(input('enter the element you want to edit:'))
for i in range (len(L)):#here only len fn would be possible as for i in L wont
work
if L[i]==element:
newch=int(input('enter the new replacement:'))
L[i]=newch
elif ch == 5:
print(L)
position=int(input('enter the position of the element you want to delete: '))
position-=1
L.remove(L[position])
elif ch == 6:
print(L)
element=int(input('enter the value of the ele u want to delete'))
for i in L:#i was facing an error when i used the len method
if i == element:
L.remove(i)
elif ch == 7:
L.sort()#The sort function in lists arranges the elements in ascending order by
default. It modifies the original list, arranging numbers from smallest to largest and
strings in alphabetical order
elif ch == 8:
L.sort(reverse=True)#to reverse the list use te reverse = True statement
elif ch == 9:
print(L)
elif ch == 10:
break
print('exited')

2.
while True:
print('''
Write a menu driven program to count different characters in string (sentence):
1• Count the number of characters.
2• Count number of capital letters
3• Count number of small letters
4• Count number of blank spaces
5• Count number of digits
6• Count number of special or other characters
7• Number of alphabets
8• Number alphanumeric characters
9• Number of words
10• Exit''')
st=input('enter a string :')
ch=int(input('enter a choice: '))
if ch==1 :
print('no of characters:',len(st))
elif ch == 2 :
count=0
for i in st:
if i.isupper():
count+=1
print('no of uppercase characters: ',count)
elif ch == 3 :
count=0
for i in st:
if i.islower():
count+=1
print('no of lower case letters: ',count)
elif ch == 4 :
count=0
for i in st:
if i.isspace():
count+=1
print('no of spaces',count)
elif ch == 5 :
count=0
for i in st:
if i.isdigit():
count+=1
print('no of digits present: ',count)
elif ch == 6 :
count=0
for i in st:
if not i.isalnum() and not i.isspace():
count+=1
print('no of special characters:',count)
elif ch == 7:
count=0
for i in st:
if i.isalpha():
count+=1
print('no of alphabets:',count)
elif ch == 8:
count=0
for i in st:
if i.isalnum():
count+=1
print('no of alphanumeric characters: ',count)
elif ch == 9:
count=0
groups=st.split()
for i in groups:
if i.isalpha():
count+=1
print('no of words are: ',count)
elif ch == 10:
break
print('exited')

3.
while True:
print('''Write a menu driven program to perform the following operations:
•1 Find Sum of Digits
•2 Reverse the number
•3 Check if the number is a palindrome
•4 Check if the number is an Armstrong number
•5 Check if the number is a perfect number
•6 Check if the number is a prime number
•7 Check if a single digit number is present in a multi - digit number
•8 Exit''')
ch=int(input('enter a choice:'))
num=int(input('enter a number:'))
if ch==1:
sum=0
stored=num
while stored > 0:
r=stored%10
sum+=r
stored=stored//10
print(sum)
elif ch==2:
stored=num
rev=0
while stored >0:
r=stored%10
rev=rev*10+r
stored=stored//10
print(rev)
elif ch==3:
stored=num
rev=0
while stored > 0:
r=stored%10
rev=rev*10+r
stored=stored//10
if num == rev:
print('its pallindrome')
else:
print('its not pallindrome')
elif ch == 4:
sum=0
stored=num
while stored>0:
r=stored%10
sum=sum+r**(len(str(num)))
stored=stored//10
if sum == num :
print('its armstong ')
else:
print('its not armstong')
elif ch == 5:
sum=0
for i in range (1,num):
if num%i==0:#we find its factors
sum+=i
if sum == num:
print('its perfect number')
else:
print('its not perfect number')
elif ch == 6:
factors=0
for i in range (2,num):#begin from two so that 1 that is a factor of every number
is not counted
if num%i==0:
factors+=1
if factors==0:
print('its a prime number')
else:
print('its not a prime number')
elif ch == 7:#Revisit
n=int(input('enter a single digit number:'))
stored=str(num)
if str(n) in stored:
print('its present')
else:
print('its not present')
elif ch==8:
break
print('exited')

5.
while True:
print(
'''Write Menu-driven program to perform the following operations:
•1 Perfect number
•2 Armstrong number
•3 Palindrome number
•4 Reverse the number
•5 Check if a single digit number is present in a multi - digit number
•6 Exit''')
ch=int(input('enter option'))
num=int(input('enter a number'))
if ch == 1:
sum=0
for i in range (1,num):
if num%i==0:
sum+=i
if sum == num:
print('its a perfect number')
else:
print('its not a perfect number')
elif ch == 2:
sum=0
stored=num
while stored > 0 :
r=stored % 10
sum=sum + r**(len(str(num)))
stored=stored//10
if sum==num:
print('its an armstrong number')
else:
print('its not an armstrong number')
elif ch == 3 :
stored=num
sum=0
while stored > 0:
r=stored%10
sum=sum*10+r
stored=stored//10
if sum == num :
print('its a pallindrome')
else:
print('its not a pallindrome')
elif ch == 4 :
stored = num
rev=0
while stored >0 :
r=stored % 10
rev=rev*10+r
stored=stored//10
print('reversed number :' ,rev)
elif ch == 5 :
n=int(input('enter the single digit number:'))
stored = num
while stored > 0:
r=stored%10
stored=stored//10
if r == n :# Please learn this logic
break
if r==n :
print('its present')
else:
print('its not present')
elif ch == 6 :
break
print('exited')

4.
D = {}

while True:
# Display the menu options
print('''
Write Menu-driven program using dictionary:
1• Add New Contact
2• Modify Phone Number of Contact
3• Delete a Friend's Contact
4• Display All Entries
5• Check if a Friend is Present or Not
6• Display in Sorted Order of Names
7• Exit
''')

# Take user input for menu choice


ch = int(input("Enter an option (1-7): "))

# Option 1: Add New Contact


if ch == 1:
name = input("Enter the friend's name: ")
phone = input("Enter the phone number: ")
D[name] = phone # Add contact to the dictionary
print("Contact added successfully.")

# Option 2: Modify Phone Number of Contact


elif ch == 2:
name = input("Enter the name of the contact to modify: ")
if name in D:
phone = input("Enter the new phone number: ")
D[name] = phone # Update the contact's phone number
print("Phone number updated successfully.")
else:
print("Contact not found.")

# Option 3: Delete a Contact


elif ch == 3:
name = input("Enter the name of the contact to delete: ")
if name in D:
del D[name] # Remove the contact from the dictionary
print("Contact deleted successfully.")
else:
print("Contact not found.")

# Option 4: Display All Entries


elif ch == 4:
print(D)

# Option 5: Check if a Contact is Present


elif ch == 5:
name = input("Enter the name to check: ")
if name in D:
print(f"'{name}' is present in the contact list.")
else:
print(f"'{name}' is not present in the contact list.")

# Option 6: Display in Sorted Order of Names


elif ch == 6:
if D:
print("Contacts in Sorted Order:")
for name in sorted(D):
print(f"{name}: {D[name]}")
else:
print("No contacts to sort.")

# Option 7: Exit
elif ch == 7:
print("Exiting the program. Goodbye!")
break # Break out of the loop to end the program

# Handle invalid choice


else:
print("Invalid option! Please choose a number between 1 and 7.")

You might also like