Urban Database - Python Programming Exercise

In this exercise, you will develop a Python program to create a database for storing information about urban areas. In the first approach, you will store only the name of each urban area and the number of inhabitants, and allocate space for up to 500 entries. The menu should include the following options: add a new urban area (at the end of the existing data), view all urban areas (name and inhabitants), modify a record (rename and/or change the number of inhabitants), insert a new record (in a specified position, moving the following ones to the right), delete a record (moving the following ones to the left so that no empty spaces are left), search in the records (display the ones which contain a certain text in their name, whether in upper or lower case, using partial search), correct the capitalization of the names (turn into uppercase the first letter and the ones after a space, and make the rest lowercase), and exit the program. This exercise is perfect for practicing the use of arrays and data manipulation in Python, as well as enhancing your skills in user interaction and database management. By implementing these functionalities, you will gain hands-on experience in handling user input and output in Python. This exercise not only reinforces your understanding of arrays and data manipulation but also helps you develop efficient coding practices for managing large datasets.

 Category

Arrays, Lists, and Strings

 Exercise

Urban Database

 Objective

Develop a Python program to create a database for storing information about urban areas.

In the first approach, we will store only the name of each urban area and the number of inhabitants, and allocate space for up to 500 entries.

The menu should include the following options:
1 .- Add a new urban area (at the end of the existing data)
2 .- View all urban areas (name and inhabitants)
3 .- Modify a record (rename and/or change the number of inhabitants)
4 .- Insert a new record (in a specified position, moving the following ones to the right)
5 .- Delete a record (moving the following ones to the left so that no empty spaces are left)
6 .- Search in the records (display the ones which contain a certain text in their name, whether in upper or lower case, using partial search)
7 .- Correct the capitalization of the names (turn into uppercase the first letter and the ones after a space, and make the rest lowercase)
0 .- Exit

 Example Python Exercise

 Copy Python Code
# Initialize empty list to store urban areas data
urban_areas = []

# Start the program
while True:
    # Display the menu
    print("\nMenu:")
    print("1. Add a new urban area")
    print("2. View all urban areas")
    print("3. Modify a record")
    print("4. Insert a new record")
    print("5. Delete a record")
    print("6. Search in the records")
    print("7. Correct the capitalization of the names")
    print("0. Exit")
    
    choice = input("Enter your choice: ")
    
    if choice == '1':
        # Add a new urban area
        name = input("Enter the name of the urban area: ")
        inhabitants = int(input("Enter the number of inhabitants: "))
        urban_areas.append((name, inhabitants))
        
    elif choice == '2':
        # View all urban areas
        if urban_areas:
            for i, (name, inhabitants) in enumerate(urban_areas, start=1):
                print(f"{i}. {name} - {inhabitants} inhabitants")
        else:
            print("No data available.")
    
    elif choice == '3':
        # Modify a record
        index = int(input("Enter the record number to modify: ")) - 1
        if 0 <= index < len(urban_areas):
            name = input("Enter the new name of the urban area: ")
            inhabitants = int(input("Enter the new number of inhabitants: "))
            urban_areas[index] = (name, inhabitants)
        else:
            print("Invalid record number.")
    
    elif choice == '4':
        # Insert a new record at a specified position
        index = int(input("Enter the position to insert at (1-based index): ")) - 1
        if 0 <= index < len(urban_areas):
            name = input("Enter the name of the urban area: ")
            inhabitants = int(input("Enter the number of inhabitants: "))
            urban_areas.insert(index, (name, inhabitants))
        else:
            print("Invalid position.")
    
    elif choice == '5':
        # Delete a record
        index = int(input("Enter the record number to delete: ")) - 1
        if 0 <= index < len(urban_areas):
            urban_areas.pop(index)
            print("Record deleted.")
        else:
            print("Invalid record number.")
    
    elif choice == '6':
        # Search in the records
        search_text = input("Enter the text to search for: ").lower()
        found = False
        for name, inhabitants in urban_areas:
            if search_text in name.lower():
                print(f"{name} - {inhabitants} inhabitants")
                found = True
        if not found:
            print("No matching records found.")
    
    elif choice == '7':
        # Correct the capitalization of the names
        for i in range(len(urban_areas)):
            name, inhabitants = urban_areas[i]
            corrected_name = ' '.join([word.capitalize() for word in name.split()])
            urban_areas[i] = (corrected_name, inhabitants)
        print("Capitalization corrected.")
    
    elif choice == '0':
        # Exit the program
        print("Exiting the program.")
        break
    
    else:
        print("Invalid choice. Please try again.")

 Output

Menu:
1. Add a new urban area
2. View all urban areas
3. Modify a record
4. Insert a new record
5. Delete a record
6. Search in the records
7. Correct the capitalization of the names
0. Exit
Enter your choice: 1
Enter the name of the urban area: New York
Enter the number of inhabitants: 8419600

Menu:
1. Add a new urban area
2. View all urban areas
3. Modify a record
4. Insert a new record
5. Delete a record
6. Search in the records
7. Correct the capitalization of the names
0. Exit
Enter your choice: 1
Enter the name of the urban area: Los Angeles
Enter the number of inhabitants: 3980400

Menu:
1. Add a new urban area
2. View all urban areas
3. Modify a record
4. Insert a new record
5. Delete a record
6. Search in the records
7. Correct the capitalization of the names
0. Exit
Enter your choice: 2
1. New York - 8419600 inhabitants
2. Los Angeles - 3980400 inhabitants

Menu:
1. Add a new urban area
2. View all urban areas
3. Modify a record
4. Insert a new record
5. Delete a record
6. Search in the records
7. Correct the capitalization of the names
0. Exit
Enter your choice: 4
Enter the position to insert at (1-based index): 1
Enter the name of the urban area: Chicago
Enter the number of inhabitants: 2716000

Menu:
1. Add a new urban area
2. View all urban areas
3. Modify a record
4. Insert a new record
5. Delete a record
6. Search in the records
7. Correct the capitalization of the names
0. Exit
Enter your choice: 2
1. Chicago - 2716000 inhabitants
2. New York - 8419600 inhabitants
3. Los Angeles - 3980400 inhabitants

Menu:
1. Add a new urban area
2. View all urban areas
3. Modify a record
4. Insert a new record
5. Delete a record
6. Search in the records
7. Correct the capitalization of the names
0. Exit
Enter your choice: 6
Enter the text to search for: york
New York - 8419600 inhabitants

Menu:
1. Add a new urban area
2. View all urban areas
3. Modify a record
4. Insert a new record
5. Delete a record
6. Search in the records
7. Correct the capitalization of the names
0. Exit
Enter your choice: 7
Capitalization corrected.

Menu:
1. Add a new urban area
2. View all urban areas
3. Modify a record
4. Insert a new record
5. Delete a record
6. Search in the records
7. Correct the capitalization of the names
0. Exit
Enter your choice: 2
1. Chicago - 2716000 inhabitants
2. New York - 8419600 inhabitants
3. Los Angeles - 3980400 inhabitants

Menu:
1. Add a new urban area
2. View all urban areas
3. Modify a record
4. Insert a new record
5. Delete a record
6. Search in the records
7. Correct the capitalization of the names
0. Exit
Enter your choice: 0
Exiting the program.

 Share this Python Exercise

 More Python Programming Exercises of Arrays, Lists, and Strings

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Display Banner

    In this exercise, you will develop a Python program to mimic the basic Unix SysV "banner" utility, capable of displaying large texts. This exercise is perfect ...

  •  Right-Aligned Triangle

    In this exercise, you will develop a Python program that prompts the user for a string and displays a right-aligned triangle. This exercise is perfect for prac...

  •  Text Processing

    In this exercise, you will develop a Python program that prompts the user for a string and performs several transformations. The program will replace all lowercase 'a...

  •  Hierarchical Structures

    In this exercise, you will develop a Python program to store two pieces of data for a person: Name and Date of birth. The Date of birth must be a...

  •  Data Organization

    In this exercise, you will develop a Python program that prompts the user for 10 integer numbers (ranging from -1000 to 1000), sorts them, and displays them in sorted...

  •  Screen Buffer Using 2D Array

    In this exercise, you will develop a Python program that declares a 70x20 two-dimensional array of characters, "draws" 80 letters (X, for example) in random positions...