DEV Community

Cover image for Python Lists + Lists Quiz!
Lambda Technology Inc for p-pl.com

Posted on • Updated on

Python Lists + Lists Quiz!

Ahhh, Lists!

Nearly every single functional programming language contains lists in one form or another. Lists are an important part of any code slinger's repertoire because they allow us to deal with data in a convenient and comprehensive way. After all... how many times have you found yourself writing a program that didn't, deal with data? Today we'll be focusing on the use of lists in Python.

Let's take a look at some basic principles of python lists and what they might mean for your code.

*If you're here to take the quiz and want to skip ahead, the quiz is linked at the bottom of this article.


First, let's define what a list truly is.
In essence, a list is a single variable that can store multiple items or, as we call them in python, elements.

Lists are one of the 4 Collection Data-Types (Arrays) in Python:
-Lists
-Dictionaries
-Sets
-Tuples

Let's look at an example list, and break it down.

foods = ["cherries", "apples", "grapes"]

First, we must give our list a name. In the above code example, we call our list "foods."

Next, we define our list using the '=', '[', and ']' symbols. An important thing to remember here: lists always use brackets ( '[' and ']') to contain their elements.

If the elements in our list are strings (text based items), we wrap each element in "double" quotation marks, followed by a comma to separate each element.
*If our list contains integers, we do not need quotation marks and would simply write the list as follows:

numbers = [0, 2, 4, 6]

Pretty simple so far, right?


Next, lets look at some of the properties lists have, and explain those properties in a bit more detail.

1). Lists are Ordered
What this means is that the data (elements) contained within a list have a set order. Generally, that order will not change (except in some more rare cases using some particular list methods, but we won't dive into that here... topic for another time).
If you were to add an element to an existing list, that newest element would be added to the end of the list.

2). Lists are Mutable
Building off our last property, we know that lists can in fact be changed (mutable). We can destroy, add or even change the existing elements in the lists that we create. 'nuff said.

3). Lists are Indexed and Allow for Duplicates
Basically, we can have multiple elements of the same exact value inside of a list. For example, a list may contain the following elements:
numbers = [0, 0, 2, 2, 4, 4,]

4). Lists Allow for Multiple Data Types
So far in this introduction to lists we've seen two lists with two different data-types: strings (fancy word for text) and integers (fancy word for whole numbers). Lists can also contain other data-types, like Boolean values. (Boolean data is a fancy way of saying: data that is either True or False... think of how computers operate with binary numbers: 0 and 1... True or False).
*One important thing to remember: Boolean values (True or False) are not wrapped in quotes, similarly to integer values.

For example:

boolean_list = [True, False, False]

The above code is very different than the below code (even though the lists look very similar).

not_boolean_list = ["True", "False", "False"]

The above code (not_boolean_list) represents a list that contains elements made up of string data, not boolean type data.

On a final note, it is also possible to have a list that combines all of these different data types, such as the following:

multi_data_type_list = [0, 2, "foo", 4, True, "bar"]


Let's now talk about how the Python Interpreter recognizes the positions of elements stored in a list. After all, we'll want to be able to call or grab the data stored in our lists as needed, so it's important to know what position that data is sitting in within a given list.

First, lets create a new list:

new_list = ["string", 4, 5, True]

Next, open a new tab here in your browser and enter the following in the address bar: https://www.online-python.com/

Clear the code example that loads and input the following:

new_list = ["string", 4, 5, True]
print(len(new_list))

*Here, we first define out list. In line 2, we invoke the print function. This tells python to print something. Next we invoke the len function, which tell python to count the length of the parameter contained inside the parentheses (in this case, it is our list: new_list). A helpful thing to remember here is that when we call a function in python, it is followed by parentheses, which contain the parameters that the function will need to operate and complete the job).

Run the code.

You'll notice that the output is 4. What this means is that the length (len) of our list is 4, meaning we have 4 elements of data stored in that list.

Now, here's what you want to remember about all of this: Python begins counting list elements starting at 0.

To illustrate this, clear the code in the IDE and enter the following:

new_list = ["string", 4, 5, True]
print(new_list[0])

*Here we invoke the print function, and pass the parameter new_list to the function. The '[0]' tells the print function that we wish to print our the first (zero) element of the data stored within the brackets.

Run the code.

The new output is now: string.

That's because the first element (element zero) in the list is a string data-type value called "string."

If you've made it this far, Great Job! You've now interacted with your list data in a couple of meaningful ways. This is only the beginning to your journey in working with lists, but we can assure you, this is a great jump into the Collection Data-Types that Python utilizes.

PRO-TIP: In tutorials like this, verbiage can sometimes be confusing. Different people write different tutorials in different languages. So I'm here to tell you this:
'Invoke a function' means the same this as 'Call a function'
'Calling our data' usually means the same thing as 'Grabbing our data'
'Arguments' mean the same thing as 'Parameters'


CHALLENGE TIME!

BROUGHT TO YOU BY P-PL.COM

Now, we'd like to offer you a challenge. If you're new to lists in python, the following quiz will test your abilities to understand the program flow in a tiny python script that interacts with two separate lists, and outputs a 3rd based on the results. This quiz may actually challenge a few intermediary python coders who aren't careful.

Native Link:
https://www.p-pl.com/resource/quiz/6192e4e956873400097e519a

Shortened and Shareable Link:
https://bit.ly/32jKcC8

Be sure to follow us on Instagram and Twitter: @p_pl_talk

Top comments (0)