The List Data Structure, Slides of Computer science

You can create a list in Python by using bracket notation. Example: my_list = [1, 2, 3] ... You can also use a for loop to iterate through a list. When you.

Typology: Slides

2022/2023

Uploaded on 03/01/2023

aeinstein
aeinstein 🇺🇸

4.6

(22)

259 documents

1 / 52

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
+
The List Data Structure
Introduction to Programming - Python
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34

Partial preview of the text

Download The List Data Structure and more Slides Computer science in PDF only on Docsity!

The List Data Structure

Introduction to Programming - Python

Variables vs. Lists n So far we have been working with variables, which can be thought of as “buckets” that hold a particular piece of data n Variables can only hold one piece of data at a time. Example n x = 5 n y = 5. n z = ‘hello’ n q = True n However, there are times when we need to keep track of multiple pieces of data at the same time, and a single variable is limited to holding just one thing at a time

Lists vs. Variables List Variable

Variables vs. Lists

Lists in Python n You can create a list in Python by using bracket notation. Example: my_list = [1, 2, 3] n The above code will create a new list in Python that holds three integers – 1, 2 and 3 – in that order. n Think of a list as a “book” that holds a series of sheets of paper (variables)

Lists in Python n Lists can contain any data type that we have covered so far. Example: my_list = [‘Craig’, ‘John’, ‘Chris’] n Lists can also mix data types. Example: my_list = [‘Craig’, 5.0, True, 67] n You can print the value of a list using the print() function. Example: print (my_list)

List Concatenation n You can use the concatenation operation (“+”) to ask Python to combine lists, much like how you would combine strings. Example: my_list = [1, 2, 3] + [99, 100, 101] print (my_list)

[1, 2, 3, 99, 100, 101]

Indexing List Elements n In a book you can reference a page by its page number n In a list you can reference an element by its index number n Indexes start at the number zero. n Example: my_list = [‘Craig’, ‘John’, ‘Chris’] print (my_list[0])

Craig

Changing the value of an item in a list n Lists are “mutable,” which means that they can be changed once they have been created (unlike strings) n Example: my_list = [1, 2, 3] print (my_list)

[1,2,3] my_list[0] = 99 print (my_list) [99,2,3]

List Mechanics n List variables are considered “references” n This means that they “reference” or “point” to a specific region of your computer’s memory. This behavior can cause some interesting side effects. For example, the following two list variables refer to the same list in memory. mylist1 = [1,2,3] mylist2 = mylist print (mylist1) print (mylist2)

[1,2,3] [1,2,3]

Copying a List n Python will only create new lists when you use [] syntax to define a list for the first time n You can take advantage of this behavior to create true copies of your list objects. For example: mylist1 = [1,2,3] mylist2 = [] + mylist mylist1[0] = 999 print (mylist1) print (mylist2)

[999,2,3] [1,2,3]

Creating Lists n You can create an empty list with no elements using the following syntax: mylist = [] n Sometimes you want to create a list that contains a certain number of “pre-set” elements. For example, to create a list with 10 elements that are all set to zero you could do the following: mylist = [0] * 10

Iterating over a list

Using a “for” loop to iterate through a List n You can also use a for loop to iterate through a list. When you do this the target variable of your loop assumes each value of each element of the list in order. Example: my_list = [1,2,3] for number in my_list: print (number)

1 2 3