Computer Science
Computer Science
def list_functions():
L=[]
State=True
print('1.Append an element\n2.Insert an element\n3.Modify an existing
element\n4.Delete an existing element from its position\n5.Delete an existing
element with given value\n6.Sort the list in ascending order\n7.Sort the list
in descending order\n8.Display the List\n9.Exit')
while State:
user_input=int(input("\nEnter your choice: "))
if user_input==1:
choice=input("Enter the element to be added: ")
L.append(choice)
elif user_input==2:
user_element=(input("Enter the element: "))
user_index=int(input("Enter the index: "))
L.insert(user_index,user_element)
elif user_input==3:
if len(L)==0:
print("The list has no elements, please choose another option
before picking this one")
continue
else:
user_element1=input("Enter the element to modify: ")
new_element=input("Enter the new element: ")
index = L.index(user_element1)
L[index]=new_element
elif user_input==4:
if len(L)==0:
print("This list is still empty, pick other options before
picking this one")
else:
user_index=int(input("Enter the index to delete: "))
L.pop(user_index)
elif user_input==5:
if len(L)==0:
print("This list is still empty, pick other options before
picking this one")
user_element2=input("Enter the element to delete: ")
L.remove(user_element2)
elif user_input==6:
if len(L)==0:
print("This list is still empty, pick other options before
picking this one")
L.sort()
elif user_input==7:
if len(L)==0:
print("This list is still empty, pick other options before
picking this one")
L.sort(reverse=True)
elif user_input==8:
if len(L)==0:
print("The list is empty, please choose other options before
picking this one")
else:
print(f"The list is {L}")
elif user_input==9:
break
list_functions()
2.
def string_chr():
string=input("Enter your string: ")
State=True
print('1.Count number of characters\n2.Count number of capital
letters\n3.Count number of small letters\n4.Count number of blank spaces\n5.Count
number of digits\n6.Count number of special or other characters\n7.Count number
of special or other characters\n8.Number alphanumeric characters\n9.Number of
words\n10.Exit')
upper=0
lower=0
blank=0
numeric=0
alpha=0
alphanumeric=0
while State:
user_input=int(input("\nEnter your choice: "))
if user_input==1:
print(f"The number of character is {len(string)}")
elif user_input==2:
for i in string:
if i.isupper():
upper+=1
print(f"The number of uppercase letters is {upper}")
elif user_input==3:
for i in string:
if i.islower():
lower+=1
print(f"The number of loweracse letters is {lower}")
elif user_input==4:
for i in string:
if i==' ':
blank+=1
print(f"The number of blank spaces letters is {blank}")
elif user_input==5:
for i in string:
if i.isnumeric():
numeric+=1
print(f"The number of digits letters is {numeric}")
elif user_input==6:
special=len(string)-(numeric+upper+lower+blank)
print(f"The number of special characters is {special}")
elif user_input==7:
for i in string:
if i.isalpha():
alpha+=1
print(f"The number of letters is {alpha}")
elif user_input==8:
for i in string:
if i.isalnum():
alphanumeric+=1
print(f"The number of alphanumeric letters are {alphanumeric}")
elif user_input==9:
length=len(string.split())
print(f"The number of words present in the string is {length}")
elif user_input==10:
break
string_chr()
3.
def integer_functions():
user_num=int(input("Enter your your number: "))
length=len(str(user_num))
State=True
4.
contacts = {}
def add_contact():
name = input("Enter the name: ")
phone = input("Enter the phone number: ")
contacts[name] = phone
print(f"{name}'s contact added successfully.")
def modify_contact():
name = input("Enter the name whose phone number you want to modify: ")
if name in contacts:
new_phone = input("Enter the new phone number: ")
contacts[name] = new_phone
print(f"{name}'s phone number modified successfully.")
else:
print(f"{name} not found in contacts.")
def delete_contact():
name = input("Enter the name whose contact you want to delete: ")
if name in contacts:
del contacts[name]
print(f"{name}'s contact deleted successfully.")
else:
print(f"{name} not found in contacts.")
def display_all():
print("Contacts:")
for name, phone in contacts.items():
print(f"{name}: {phone}")
def check_contact():
name = input("Enter the name to check if it's present: ")
if name in contacts:
print(f"{name} is present in contacts.")
else:
print(f"{name} is not found in contacts.")
def display_sorted():
print("Contacts (Sorted by Name):")
for name in sorted(contacts.keys()):
print(f"{name}: {contacts[name]}")
while True:
print("\n1. Add New Contact")
print("2. Modify Phone Number of Contact")
print("3. Delete a Friend's contact")
print("4. Display all entries")
print("5. Check if a friend is present or not")
print("6. Display in sorted order of names")
print("7. Exit")
if choice == 7:
break
if choice == 1:
add_contact()
elif choice == 2:
modify_contact()
elif choice == 3:
delete_contact()
elif choice == 4:
display_all()
elif choice == 5:
check_contact()
elif choice == 6:
display_sorted()
else:
print("Invalid choice. Please enter a number between 1 and 7.")