Lists Basics
Lists Basics
print(bicycles)
print(bicycles[0])
print(bicycles[1])
print(bicycles[2])
print(bicycles[3])
print(bicycles[4])
print(bicycles[4])
##can index returns the items from the last, for that you need to use
"-2", "-3"
#index -2 returns the second item from the end of the list, the
#index -3 returns the third item from the end, and so forth.
print(bicycles[-1])
print(bicycles[-2])
print(bicycles[-3])
print(bicycles[-4])
print(bicycles[-5])
BMX
specialized
redline
cannondale
trek
print(message)
####Exercise####
###Store the names of a few of your friends in a list called
###names. Print each person’s name by accessing each element in the
list,
###one at a time.
###Exercise 2####
teambravo=['haseeb', 'jamal', 'haider', 'mustafa']
message=f"Greetings {teambravo [-2].title()}"
print(message)
Greetings Haider
##But this is only returning me one team member name, I want to send
the greeting message to all.
Hi Haseeb, I hope you're having a great day! Keep up the good work.
Hi Jamal, I hope you're having a great day! Keep up the good work.
Hi Haider, I hope you're having a great day! Keep up the good work.
Hi Mustafa, I hope you're having a great day! Keep up the good work.
###Exercise 3##
#Think of your favorite mode of transportation, such as a car, and
make a list that stores several examples.
# Use your list to print a series of statements about these items,
such as “I would like to own a .”
cars = ['Tesla', 'BMW', 'Audi', 'Mercedes']
for car in cars:
print(f"I would like to own a {car}.")
###I need a specific car out of all list###you can access Index
cars = ['Tesla', 'BMW', 'Audi', 'Mercedes']
# Suppose you want the second car in the list (BMW)
specific_car = cars[1]
print(f"I would like to own a {specific_car}.")
#Searching for a specific car
#If you don't know the index but you know the car's name, you can
search for it in the list.
cars = ['Tesla', 'BMW', 'Audi', 'Mercedes']
# Suppose you want to find 'Audi' in the list
specific_car = 'Audi'
if specific_car in cars:
print(f"I would like to own a {specific_car}.")
else:
print(f"{specific_car} is not in the list.")
##How might this pop() method be useful? Imagine that the motorcycles
in the list are stored in chronological order,
#according to when we owned them. If this is the case, we can
#use the pop() method to print a statement about the last motorcycle
we bought:
motorcycles = ['honda', 'yamaha', 'suzuki']
last_owned = motorcycles.pop()
print(f"The last motorcycle I owned was a {last_owned.title()}.")
#output: The output is a simple sentence about the most recent
motorcycle we owned:
##Here the remove() method tells Python to figure out where 'ducati'
appears in the list and remove that element
#You can also use the remove() method to work with a value that’s
being removed from a list. Let’s remove the value
#'ducati' and print a reason for removing it from the list:
#NOTE:
#The remove() method deletes only the first occurrence
#of the value you specify. If there’s a possibility the value
#appears more than once in the list, you’ll need to use a
#loop to make sure all occurrences of the value are
#removed.
# Example : Guest List
# Guest List: If you could invite anyone, living or deceased, to
dinner, who would you invite?
# Make a list that includes at least three people you’d like to
invite to dinner.
# Then use your list to print a message to each person, inviting them
to dinner.
# List of people you'd like to invite to dinner
#Now, You just heard that one of your guests can’t make the dinner, so
you need to send out a new set of invitations.
# You’ll have to think of someone else to invite.
#Start with your program from prevous exercise. Add a print() call at
the end
# of your program, stating the name of the guest who can’t make it.
# Modify your list, replacing the name of the guest who can’t make it
with the name of the new person you are inviting.
#Print a second set of invitation messages, one for each person who is
still in your list.
# Original list of people you'd like to invite to dinner
#Exercise 3
#More Guests: You just found a bigger dinner table, so now more
#space is available. Think of three more guests to invite to dinner.
#Start with your program from previous exercises. Add a print() call
to
#the end of your program, informing people that you found a bigger
table.
#Use insert() to add one new guest to the beginning of your list.
#Use insert() to add one new guest to the middle of your list.
#Use append() to add one new guest to the end of your list.
#Print a new set of invitation messages, one for each person in your
list.
print(guest_list)
#Exercise
#Shrinking Guest List: You just found out that your new dinner table
won’t arrive in time for the dinner,
# and now you have space for only two guests.
#Start with your program from Previous exercise. Add a new line that
prints a
#message saying that you can invite only two people for dinner.
#Use pop() to remove guests from your list one at a time until only
two
#names remain in your list. Each time you pop a name from your list,
#print a message to that person letting them know you’re sorry you
can’t
#invite them to dinner.
#Print a message to each of the two people still on your list, letting
them know they’re still invited.
#Use del to remove the last two names from your list, so you have
anempty list.
#Print your list to make sure you actually have an empty list at the
end of your program.