A list is a collection of items in a particular order.Â
You can make a list that includes the letters of the alphabet, the digits from 0 to 9, or the names of all the people in your family.Â
You can put anything you want into a list, and the items in your list donât have to be related in any particular way.Â
Think of a list as a big filing cabinet with lots of folders inside it!
Because a list usually contains more than one element, itâs a good idea to make the name of your list plural, such as letters, digits, or names.
In Python, square brackets [ ] indicate a list, and individual elements in the list are separated by commas.
Let's Look at an Example of how we can define a list...
HARD-CODED LISTS:
For this type of list, we literally write what we want in the list:
USER INPUT LISTS:
It would be a lot less fun if we could only hard code lists. What if we want the use to input items?
Is there a way we can get input from a user??
YES! The input() function!!
Step 1: Get the user to input the 4 items and save them to a variable
Step 2: Assign those VALUES into our list
How??:
Are There Other Ways?
Yes. But let's focus on these for now!!
The big day is finally here! Mr. Krabs is hosting the Grand Re-Opening of the Krusty Krab, and the whole crew is scrambling to make the restaurant look perfect.
SpongeBobâs job: keep everything organized. He needs to write down three lists before the doors open â one for the kitchen setup, one for the VIP guest names, and one for todayâs patty batch counts.
This will be your very first adventure with Python lists â collections of items stored inside square brackets.
Youâll create a few lists, fill them with data, and print them out proudly for Mr. Krabs to approve!
SpongeBob has already set up the restaurant. These stations are permanent parts of the Krusty Krab kitchen.
Create a list named kitchen_stations with four items.
Print out the entire list.
Patrick is guarding the door, but his handwriting looks like jellyfish trails. SpongeBob decides to take over and type each guestâs name neatly.
Ask the user for three VIP names (one at a time).
Store them in a list called vip_names.
Print the full list once.
Before the ribbon-cutting, SpongeBob cooked a few practice batches of Krabby Patties. He needs to record how many patties came out perfectly golden in each run.
Ask the user for three integers (number of patties per batch).
Store them in a list called patty_batches.
Print the list.
We need to use the list's INDEX
The Index is just a position in the list.Â
It refers to a specific item (folder in the cabinet)
LIST NUMBERING STARTS AT ZERO!!! THIS IS IMPORTANT!!!
To Access An Element in a list:
Write the name of the list followed by the index of the item enclosed in square brackets
Try printing out the 7th movie. See what error you get (if in fact you get one)
Remember how I said that you need to make sure the list element exists?
Try this for fun...
What Does This Do?? Can We Use Other Numbers?
We've gone over string functions (upper, lower, title, strip, lstrip, rstrip, center, etc..)Â
We've gone over colors and f-strings so you can display everything pretty...
USE THIS STUFF!!! DON'T LET YOUR BRAIN FORGET!!! MUSCLE MEMORY!!!
The Bikini Bottom Bijou is prepping for a massive movie premiere, and youâve been promoted to Concessions Curator. Your job is to make sure the snack lineup looks as good as the trailer reel. Mr. Krabs wants to see the entire inventory list first, then the classics that always sell out halfway through the previews.
But before the doors open, SpongeBob grabs the mic and asks, âWhatâs your favorite snack, pal?â â so youâll let the user choose an index number to spotlight their pick from the list.
1ď¸âŁ Create a list named snacks with 7 items that a user inputs...
2ď¸âŁ Print the entire list first.
3ď¸âŁ Print the 1st, 3rd, 5th, and 7th snacks using their indexes.
4ď¸âŁ Ask the user which snack number they want (1â7) and display it in uppercase as their choice of the night.
The projectors are rolling! Youâre in charge of the glowing letter tiles for tonightâs âNow Showingâ marquee. SpongeBobâs chosen five titles, but the Bijou has two screens â left (even) and right (odd). Youâll display both line-ups, then give visitors a chance to type a movie number to see whatâs playing at their favorite screen.
1ď¸âŁ Ask for five movie titles (user input).
2ď¸âŁ Store them in movies.
3ď¸âŁ Print the entire list.
4ď¸âŁ Print even-screen titles (indexes 0, 2, 4).
5ď¸âŁ Print odd-screen titles (indexes 1, 3).
6ď¸âŁ Ask the user which movie number (1â5) they want to preview and print that title in uppercase.
Flashbulbs popping! Youâre manning the velvet rope at the Sea-Cinema Premiere. Five VIPs are arriving, and each gets a personalized shout-out. Afterwards, the critic board releases three numeric ratings, and youâll let the user peek at whichever one they want â no scrolling, just direct index access.
1ď¸âŁ Define a list guests with five names (hard-coded or input).
2ď¸âŁ Print the full list.
3ď¸âŁ Print three personalized lines using individual indices.
4ď¸âŁ Ask the user to pick a guest number and greet that person by name.
5ď¸âŁ Then gather three ratings into ratings, print the full list, and let the user choose which rating to display.
Itâs Friday night under the sea, and SpongeBob is hosting the first-ever Gala at the Krusty Krab! The bubble lights are glowing, the fryers are sizzling, and the guest list is⌠chaos. Mr. Krabs wants a perfect seating chart before the VIPs arrive â but everyone keeps yelling their favorite foods over the loudspeakers. SpongeBob needs a programmerâs touch to save the evening. Thatâs you!Â
Your mission: build the official Guest & Food Pairing System for the gala.
Prompt SpongeBob (the user) to enter 4 guest names for the party, one at a time.
Make sure to strip any leading or trailing whitespace from the inputs.
Store the guest names in a list called guest_list.
Print a message that lists all the guests using print().
Now, for each guest, SpongeBob records their favorite food.
Store all four food items in a list called favorite_foods.
Print the entire list of foods once.
Print each guest next to their favorite food using string formatting and .center() for perfect alignment.
Make each guestâs name Title Case and each food UPPERCASE.
Format it to look like a digital event screen.
Access the first and last guests using their indices (0 and -1).
Print a special message for each using f-strings.
Make the message sound like SpongeBob announcing it at the microphone.
Example Included Below:
Allow the user to pick the guest that is the star. Once they enter which guest, display their name and the food they love!