Write a program to search the names and addresses of persons having age more than 30 in the data list of persons.

Python program that searches for names and addresses of persons with an age greater than 30 in a data list of persons:

def search_persons(data_list):
    result = []
    for person in data_list:
        if person['age'] > 30:
            result.append((person['name'], person['address']))
    return result


# Example data list of persons
persons = [
    {'name': 'John Doe', 'age': 25, 'address': '123 Main St'},
    {'name': 'Jane Smith', 'age': 35, 'address': '456 Elm St'},
    {'name': 'Mike Johnson', 'age': 40, 'address': '789 Oak St'},
    {'name': 'Sarah Williams', 'age': 28, 'address': '987 Pine St'},
    {'name': 'David Brown', 'age': 32, 'address': '654 Maple St'}
]

result = search_persons(persons)

if result:
    print("Persons with age greater than 30:")
    for name, address in result:
        print(f"Name: {name}, Address: {address}")
else:
    print("No persons found with age greater than 30.")

 

In this program, we define a search_persons function that takes a data list of persons as input. It iterates over each person in the list and checks if their age is greater than 30. If the condition is met, the person’s name and address are added to the result list. The function returns the result list.

We provide an example data list of persons and call the search_persons function on it. If there are persons with an age greater than 30, their names and addresses are printed. Otherwise, a message indicating that no persons were found is displayed.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *