2.
Take 10 country name & their capital name, store country into one list and their capitals into
another one and make sure there is no duplicate entry
def get_unique_entries():
countries = []
capitals = []
while len(countries) < 10:
country = input("Enter country name: ").strip()
capital = input("Enter capital name: ").strip()
if country in countries:
print("Country already entered. Please enter a different one.")
continue
[Link](country)
[Link](capital)
return countries, capitals
def main():
print("Enter 10 unique country names and their capitals:")
countries, capitals = get_unique_entries()
print("\nCountries List:", countries)
print("Capitals List:", capitals)
if __name__ == "__main__":
main()
4. Read the employee details (id, name, age, mobile no) and store this data into dictionary,
employee id be the key for the dictionary and make sure there is no duplicate key.
def get_employee_details():
employees = {}
n = int(input("Enter the number of employees: "))
for _ in range(n):
while True:
emp_id = input("Enter Employee ID: ").strip()
if emp_id in employees:
print("Employee ID already exists. Please enter a unique ID.")
else:
break
name = input("Enter Employee Name: ").strip()
age = input("Enter Employee Age: ").strip()
mobile = input("Enter Employee Mobile No: ").strip()
employees[emp_id] = {"Name": name, "Age": age, "Mobile": mobile}
return employees
def main():
print("Enter Employee Details:")
employee_data = get_employee_details()
print("\nEmployee Records:")
for emp_id, details in employee_data.items():
print(f"ID: {emp_id}, Name: {details['Name']}, Age: {details['Age']}, Mobile:
{details['Mobile']}")
if __name__ == "__main__":
main()
5. Take 10 country name and filter out the country name based on the no of words in the
country name
def get_countries():
countries = []
print("Enter 10 country names:")
while len(countries) < 10:
country = input(f"Enter country {len(countries) + 1}: ").strip()
[Link](country)
return countries
def filter_countries_by_word_count(countries, word_count):
return [country for country in countries if len([Link]()) == word_count]
def main():
countries = get_countries()
word_count = int(input("Enter the number of words to filter country names by: "))
filtered_countries = filter_countries_by_word_count(countries, word_count)
print(f"\nCountries with {word_count} words:", filtered_countries)
if __name__ == "__main__":
main()
6. Read 10 employee data and convert all the data in unique format (employee id as number,
employee name as first name last name and camel case, date of birth as yyyy-mm-dd, mobile
no starts with +91 and contains 10 digit)
import re
def format_name(name):
return ' '.join([[Link]() for word in [Link]()])
def format_dob(dob):
try:
parts = [Link](r'[-/]', dob)
if len(parts) == 3:
year, month, day = (parts if len(parts[0]) == 4 else parts[::-1])
return f"{year}-{[Link](2)}-{[Link](2)}"
except:
pass
return "Invalid Date Format"
def format_mobile(mobile):
mobile = [Link](r'\D', '', mobile) # Remove non-digit characters
return f"+91{mobile[-10:]}" if len(mobile[-10:]) == 10 else "Invalid Mobile Number"
def get_employee_data():
employees = {}
for i in range(10):
while True:
emp_id = input("Enter Employee ID (Numeric): ").strip()
if not emp_id.isdigit() or emp_id in employees:
print("Invalid or duplicate Employee ID. Please enter a unique numeric ID.")
else:
emp_id = int(emp_id)
break
name = format_name(input("Enter Employee Name (First Last): ").strip())
dob = format_dob(input("Enter Date of Birth (YYYY-MM-DD or DD-MM-YYYY): ").strip())
mobile = format_mobile(input("Enter Mobile No (10 digits): ").strip())
employees[emp_id] = {"Name": name, "DOB": dob, "Mobile": mobile}
return employees
def main():
print("Enter Employee Details:")
employee_data = get_employee_data()
print("\nFormatted Employee Records:")
for emp_id, details in employee_data.items():
print(f"ID: {emp_id}, Name: {details['Name']}, DOB: {details['DOB']}, Mobile:
{details['Mobile']}")
if __name__ == "__main__":
main()
7. Read 20 product name and sort those data as per the length of the product name then put
this data into dictionary and key should be the length of the product name.
def get_products():
products = []
print("Enter 20 product names:")
while len(products) < 20:
product = input(f"Enter product {len(products) + 1}: ").strip()
[Link](product)
return products
def sort_products_by_length(products):
sorted_products = sorted(products, key=len)
product_dict = {}
for product in sorted_products:
length = len(product)
if length not in product_dict:
product_dict[length] = []
product_dict[length].append(product)
return product_dict
def main():
products = get_products()
sorted_product_dict = sort_products_by_length(products)
print("\nProducts sorted by length:")
for length, items in sorted_product_dict.items():
print(f"Length {length}: {items}")
if __name__ == "__main__":
main()