Lists
List Basics
Lists in Python are ordered collections of things. Lists and list elements are designated by square brackets, []
.
>>> pycon_feelings = ['Hooray', 'Nervous', 'Amazing', 'Lacking bees']
>>> pycon_feelings
['Hooray', 'Nervous', 'Amazing', 'Lacking bees']
Lists can contain of all types of things; they don't have to have all strings, integers, etc.
>>> things = [5, True, "hello"]
>>> things
[5, True, 'hello']
You can even put lists of things in other lists!
>>> more_things = [things, pycon_feelings]
>>> more_things
[[5, True, 'hello'], ['Hooray', 'Nervous', 'Amazing', 'Lacking bees']]
Just like every other object, we can use type
to confirm that we're working with a list:
>>> type(things)
<class 'list'>
We can also use conditionals on lists:
>>> 5 in things
True
>>> False in things
False
>>> "hello" in things
True
List Exercises
Magic 8 Ball
Make a program magic_eight_ball.py
that takes a yes or no question as and gives a yes, no, or maybe response at random.
Hint
You can import the choice
function from Python's random
module and use it to choose an item at random, like this:
import random
colors = ["purple", "green", "yellow"]
print(random.choice(colors)) # This will randomly print a color
Guest List
Make a program event.py
that asks the user for their name, checks a list of names and either tells them "Welcome THEIR_NAME!" or "Sorry, but you're not on the guest list".
Interactive Rock, Paper, Scissors
Update your rock, paper, scissors program to play against the computer. It should work like this:
Prompt the user for a move
Choose a move at random for the computer
Print what the computer chose and who won
Hint
random.choice()
will help here. See the documentation for more information.
Virtual Friend
Create a program my_friend.py
that does the following:
Asks how you are
Compares the input to a list of good emotions and a list of bad emotions, and prints an appropriate response depending on which list the input is in
Prints "I truly don't know what that is like." if the input isn't in either list
Chooses a random response if the input "How are you" is received (with any capitalization)
Lazy Chef Sandwich Generator
You've just opened a restaurant but you're too lazy to come up with a sandwich menu. Make a program to do it instead!
Create a program lazy_chef.py
that does the following:
Create a list of options for each element of the sandwich (proteins, spreads, bread, etc.).
Print one random item from each of the sandwich elements lists when the program is run.
Example usage:
$ python3 lazy_chef.py
olives
mustard
beef
white bread
Hint
You can use random.choice()
to choose an item at random.
Acquiring Orange, Inc.
You've acquired the famous tech company Orange, Inc.! Time to combine your assets.
Make a list
my_assets
containing your assets, and a listorange_assets
containing tech giant Orange, Inc.'s assetsMake a new list
my_orange_assets
that contains all of the combined assets of the previous lists
Hint
This shouldn't be a list of two lists, but rather a list containing all of the items from the first two lists. Do you remember how we combined strings? See if something like that will work here.