Expedia Code Academy
A list acts as a single variable to store lots of data.
Each of those values is called an element of the list and can be accessed by its position, or index.
In Python, you can define lists like this:
# a list of strings
expedia_brands = ['Hotels.com', 'EPS', 'Expedia']
# a list of numbers
exam_grades = [40, 55, 99, 73]
# a list containing mixed types
a_bit_of_everything = ['hello!', 2, True, 0.535]
# a list of lists
words = [['hello', 'yes', 'no'], ['hola', 'si', 'no']]
# a list of strings
expedia_brands = ['Hotels.com', 'EPS', 'Expedia', 'Egencia']
print(expedia_brands[0]) # 'Hotels.com'
print(expedia_brands[2]) # 'Expedia'
print(expedia_brands[-1]) # 'Egencia'
print(expedia_brands[-2]) # 'Expedia'
# Create a list
random_numbers = [31, 5.36, 0, 25, 535, 42.3]
length = len(sentence)
print(length) # 6
# Start with a list
sentence = ['Programming', 'is']
# Add an item to the end
sentence.append('fun')
print(sentence)
# To create an empty list
empty_list = []
# Create two lists
sentence_start = ['Programming', 'is']
sentence_end = ['really', 'fun']
# Join the two lists together using the + operator
sentence = sentence_start + sentence_end
print(sentence)
expedia_brands = ['Hotels.com', 'EAN', 'Expedia']
if 'Booking.com' in expedia_brands:
print('Something must be wrong!')
if 'Hotels.com' in expedia_brands:
print('Yay!')
(⭐️⭐️) 3d. Program Bob (version 2)!
random_numbers = [5, 3, 4, 6, 1, 2]
sorted_numbers = sorted(random_numbers)
print(sorted_numbers) # displays [1, 2, 3, 4, 5, 6]
letters = ['a', 'b', 'c', 'd', 'e', 'f']
letters[1:3] # results in ['b', 'c']
letters[2:3] # results in ['c']
numbers = [65, 4, 9, 23, 15, 875, 3.69, 33, 12, 79, 6]