












































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 52
This page cannot be seen from the preview
Don't miss anything!













































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