You are on page 1of 7

1.

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

print('1.Find the sum\n2.Reverse the number\n3.Check if it is a


palindrome\n4.Check if it is armstrong\n5.Check if it is a perfect
number\n6.Check if it is a prime number\n7.Check if single digit is
present\n8.Exit')
while State:
user_input=int(input("\nEnter your choice: "))
if user_input==1:
sum=0
while user_num>0:
digit=user_num%10
sum+=digit
user_num//=10
print(f"The sum of the digits is {sum}")
elif user_input==2:
original_num = user_num
reversed_num = 0
while original_num > 0:
digit = original_num % 10
reversed_num = (reversed_num * 10) + digit
original_num //= 10
print(f"The reversed number is {reversed_num}")
elif user_input==3:
rev=0
temp2=user_num
while user_num>0:
digit=user_num%10
rev=rev*10+digit
user_num//=10
if temp2==rev:
print("This is a Palindrome")
else:
print("This is not a Palindrome")
elif user_input==4:
sum=0
temp1=user_num
while temp1 > 0:
digit = temp1 % 10
sum += digit**length
temp1 //= 10
if user_num == sum:
print("It is Armstrong")
else:
print("It is not Armstrong")
elif user_input==5:
sum = 0
for i in range(1, user_num):
if user_num % i == 0:
sum = sum + i
if sum == user_num:
print("This is a Perfect Number")
else:
print("This is not a Perfect number")
elif user_input==6:
prime=True
if user_num == 1:
print(user_num, "is not a prime number")
elif user_num > 1:
for i in range(2, user_num):
if user_num % i == 0:
prime=False
break
if prime:
print("It is a prime number")
else:
print("It is not a prime number")
elif user_input==7:
choice=int(input("Enter the digit: "))
temp_num = user_num
while temp_num > 0:
digit = temp_num % 10
if digit == choice:
print("The digit is present")
break
temp_num //= 10
elif user_input==8:
break
integer_functions()

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

choice = int(input("\nEnter your choice: "))

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

You might also like