Today’s post returns to Sololearn’s Introduction to Python course with the next module. Module four is called “Working with Lists” and it covers everything new developers need to know about lists. Before you can explore everything you can do with lists, let’s start by basics you need to know about lists and how to create a list in Python.
Lists are very important in programming and you’ll be making a lot of lists regardless of what programming language you are using. Lists are an important programming concept because it enables developers to store multiple values to one variable instead of having multiple variables with different variables. Developers can just create one variable and have multiple values saved to that list.
When I was working at coding with kids, the students would use lists to store sprites and other items in as they were building their games. As you continue using lists, the items in your lists will not always be the same data type and can have as many values as you need to save to one variable.
Create a List in Python
The way you create a list is very similar to how developers create variables. When you create a variable in Python, you start by creating a variable. The same variable naming tips apply here so make sure you revisit those from previous modules. After the variable name, put an equal sign.
After the equal sign, you can start assigning values to a list. First, put an opening square bracket. This punctuation tells the computer this will be a list.
Next, put your first value. Before you create the next value, put a comma after the first value. Punctuation is important in programming and even more so in text based langauges like Python. If you don’t put a comma down, the computer will assume everything is one value instead of multiple values. Put a closing square bracket at the end of your list when you are done.
Here’s an example of a list of strings. I named this variable grocery_list using the variable naming conventions. After the equal sign, I have an opening square bracket followed by all the values I want in my list. When my list is done, it put a closing square bracket.
grocery_list = ["milk", "juice", "eggs", "cheese", "bread"]
Access items from a list
You won’t just be creating lists in Python. You’ll also need to access items from them. This is something you’ll do often in the projects you so you’ll be doing this often throughout your code.
To get items from a list, you will need the index number. Every item in a list has an index number. That index number is what the computer uses to locate it.
Computers count differently than we do and this impacts the way they assign index numbers in lists. This can get confusing for new developers because we have to learn to count by the computer. So make sure you count the way the computer counts when you are trying to get items from a list.
We count at the beginning of the list start at 1 then going got 2, 3, etc. Computers start counting at 0. So the first index for a computer will always be 0 then 1, 2, 3, etc. Accessing items with an index is similar to creating a list.
We start with a variable name then an opening square bracket. Put the index number of the list item you want. Put a closing square bracket at the end.
The example below is accessing items from the grocery_list list from the example above. If I want to access just the “milk” value from the list, I use the variable name and put the index in parentheses. If I print this to the console, milk will be the only value displayed.
print(grocery_list[0]) # print milk
Top comments (0)