Lists and Loops

Objectives ⟶

  • Review if, elif, else statements.

  • Introduce the list data type.

  • Learn how to create, access, edit a list.

  • Use a for loop to iterate over a list.

  • Use a while loop to iterate until a condition is satisfied.

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Introduction to Lists

Overview

The code you wrote above is great. But with a degree from the U of I, you will likely get more than one job offer. Assume you get three offers. Can you reuse the logic you've already written? Yes! But we'll first need to cover a new data type (list) and a new syntax (for loops).

Last time, we talked about bool, int, float, and str types. Variables with these three types can only contain a single value. That's why they are called basic types. The list type is the first non-basic data type we've seen. Square brackets ([]) are used to denote a list type.

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Accessing list elements by index

Zero-based Index

What does that offers[0] do? The square brackets used in offers[0] denote the index of the element you're trying to retrieve. offers[0] retrieves the first value in the array, which is 55000 in our code. But why does the index start at zero, not one?

In many programming languages including Python, you start counting indices at zero, not one. To retrieve the second item from my_list, the correct code would be my_list[1], not my_list[2].

Multiple Choice Question

Loading

Question
Loading
Loading Options
Loading
Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Negative list indices

From the end of the list

You can also access elements from the end of the array using a negative index. Negative indices begin at -1. my_list[-1] will select the last element in my_list.

Similarly, my_list[-2] will select the second to the last element in my_list.

Multiple Choice Question

Loading

Question
Loading
Loading Options
Loading
Multiple Choice Question

Loading

Question
Loading
Loading Options
Loading
Python Challenge

Loading

Python Challenge

Loading

How many elements are in a list?

Length of a list

You can retrieve the number of items in a list by using len(my_list) syntax.

Multiple Choice Question

Loading

Question
Loading
Loading Options
Loading
Python Challenge

Loading

Python Challenge

Loading

Working with lists

List methods

Python provides multiple list methods (if you're not familiar with what a "method" is, think of it as some operation on a list - like adding or removing an item).

A common operation is to add an item to an existing list. You can append an item to a list using my_list.append(new_value) syntax.

If you'd like to take a look at all available methods, refer to https://docs.python.org/3/tutorial/datastructures.html .

Python Challenge

Loading

Can a list have elements with non-uniform data types?

Mixed Types

From the previous coding question, we're able to see that a list can have elements with mixed data types. A list can hold any data type. In fact, a list can hold another list, which is called a nested list. For now, let's keep it simple and not worry about nested lists.

A list can contain a list that contains another list that contains another list... and so on.

Python Challenge

Loading

Python Challenge

Loading

Do we really want to keep copy-pasting stuff?

Repetition is never good

Well that's a little inefficient, isn't it? You may be okay with copy-pasting the print(roster[n]) code a couple of times. But imagine if the roster list had 1,000 names. You'd be spending your entire afternoon copy-pasting and changing numbers.

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading

Another way to retrieve elements

A second syntax

You can also use for x in my_list to iterate over each value in my_list. Note that x is an arbitrary name you pick. You can use any name. Any of the code below will work.

  • for x in my_list
  • for a in my_list
  • for my_value in my_list
  • for any_variable_name in my_list
Python Challenge

Loading

Python Challenge

Loading

Python Challenge

Loading