Wednesday, November 16, 2022

Tuples - Python Programming for Beginners Practice Exercise 10

Exercise 10:

Create a list of airports that includes a series of tuples containing an airport's name and its code. Loop through the list and utilize tuple assignment. Use one variable to hold the airport name and another variable to hold the airport code. Display the airport's name and code to the screen.

Sample Output:

The code for O’Hare International Airport is ORD.

The code for Los Angeles International Airport is LAX.

The code for Dallas/Fort Worth International Airport is DFW.

The code for Denver International Airport is DEN.

Solution:

#!/usr/bin/env python3

airports = [
    ("O’Hare International Airport", 'ORD'),
    ('Los Angeles International Airport', 'LAX'),
    ('Dallas/Fort Worth International Airport', 'DFW'),
    ('Denver International Airport', 'DEN')
]

for (airport, code) in airports:
    print('The code for {} is {}.'.format(airport, code))

No comments:

Post a Comment