-
Notifications
You must be signed in to change notification settings - Fork 0
Chapter 3
Jarlin Almanzar edited this page Oct 25, 2019
·
2 revisions
A list is a collection of items in a particular order
They can include a list that includes letters of the alphabet, digits from 0-9, names, dates, and so on.
#bicycle brands
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles)
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles[0])
# Will print trek, the first value of the list
list[-1] would return the value in the last index of a list
In this case bicycles[-1] would return specialized
#EX: Using Index in a list
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
msg = f"My first bike was a {bicycles[0].title()}"
print(msg)
motorcycles = ['honda', 'yamaha', 'suzuki']
motorcycles[0] = 'ducati'
print(motorcycles)
# it will print ['ducati', 'yamaha', 'suzuki']
motorcycles.append('ducati')
print(motorcycles)
motorcycles = []
motorcycles.append('honda')
motorcycles.append('yamaha')
motorcycles.append('suzuki')
print(motorcycles)
motorcycles.insert(0, 'ducati')
motorcycles = ['honda', 'yamaha', 'suzuki']
print(motorcycles)
del motorcycles[0]
print(motorcycles)
# pop() removes the last item in a list, but it lets you work an item after removing it
motorcycles = ['honda', 'yamaha', 'suzuki']
pop_mtr = motorcycles.pop()
print(motorcycles)
print(pop_mtr)
# remove()
motorcycles = ['honda', 'yamaha', 'suzuki']
motorcycles.remove('honda')
print(motorcycles)
# sort()
cars= ['bmw', 'audi', 'toyota', 'subaru']
cars.sort()
print(cars)
# sort()
cars= ['bmw', 'audi', 'toyota', 'subaru']
cars.sort(reverse=True)
# or cars.reverse() but not sorted
print(cars)
# sorted() returns the list in order without affecting the original list
cars= ['bmw', 'audi', 'toyota', 'subaru']
print(sorted(cars))
Check for list index out of range
Calling values where they don't exist