Online visitors

People learning around the world

Online

—

Visitors exploring W3Schools

Online visitors by country Circles show approximate country locations. The country list provides the visitor counts.

Circles represent countries, not exact visitor locations.

Country

Online

Loading country activity…

    Python Program to Create a Contact Book

    Python Programming Examples


    This Python contact book program saves user input as a contact book description and retrieves it as search results. It is a fundamental program that shows how simple logic works using arrays, loops, and conditional statements.

    Example:

    An example of a Contact book in Python:

    names = []
    contact_numbers = []
    num = int(input("Enter the total number of contacts you want to save: "))
    for i in range(num):
        name = input("Name: ")
        contact_number = int(input("Contact Number: ")) 
        names.append(name)
        contact_numbers.append(contact_number)
    print("\nName\t\t\tContact Number\n")
    for i in range(num):
        print("{}\t\t\t{}".format(names[i], contact_numbers[i]))
    search_term = input("\nEnter search term: ")
    print("Search result:")
    if search_term in names:
        index = names.index(search_term)
        contact_number = contact_numbers[index]
        print("Name: {}, Phone Number: {}".format(search_term, contact_number))
    else:
        print("No records")

    Program Output:

    Enter the total number of contacts you want to save: 2
    Name: Alex
    Contact Number: 123456789
    Name: Amara
    Contact Number: 984321
    
    Name                    Contact Number
    
    Alex                    123456789
    Amara                   987654321
    
    Enter search term: Alex
    Search result:
    Name: Alex, Phone Number: 123456789


    Found This Page Useful? Share It!
    Get the Latest Tutorials and Updates
    Join us on Telegram

    Notifications

    Updates are enabled for this browser.

    Receive new tutorials, articles and learning resources. You can turn updates off anytime.