This preview shows page 1-2-3-4-5 out of 14 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 14 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Page 1 6.189 – Notes/Homework Administrivia Name: Instructions: 1. Err..complete the questions :). Putting a serious effort into this homework is mandatory for receiving a passing grade in this course (note the word effort – getting them all wrong is ok, but skipping this homework is not.) 2. These notes (and homework problems) are a continuation of the notes from Exam 1. 3. Some problems may be marked {alone} (it’ll be in red and braces.) Work on those problems alone! 4. When we ask for output, you DON'T have to write the spaces/newlines in. Program Text: Output: Day 4: List Basics Notes: This section covers the basics of lists. We'll cover them in more detail after we cover objects and references. We've used variables to store basic information such as numbers, strings, and boolean values. We can also store more complex information. A list stores a sequence of elements [x1,x2,x3,...]. Note that order matters: [x1,x2] != [x2,x1] You can store different types of variables in the same list: [1,3,"test",True]. You can even store lists in lists! (Note: For now, don't store lists in lists! You'll understand why when we talk about objects and mutability.) Chapters 9.1-9.3 cover basic syntax on using lists. Here’s a quick review: - You can create a list using square brackets [1,2,5,1,3,"test"]. - To read an element from list some_list, use some_list[index]. The index of an element is just its position, but note that the first element is considered to have a position of 0! print “X”, print “X”, XX Session 6Page 2 - The built-in function len(x) will find the length of any list passed in as a parameter. This works more generally with any complex data structure (like dictionaries, which we'll cover later) as well as strings. We'll also be using the following syntax for adding something to the end of a list. Program Text: Output: For now, just memorize the syntax -- you'll understand what's going on in a later section. Problem 12 {alone}: What is the output of the following code. The output of each program has at most two semi-colons. Program Text: Output: Program Text: Output: some_list = [1,2,3,4] some_list.append(5) print some_list [1,2,3,4,5] some_list = [3,6,2,5] i = 1 while i < 3: print some_list[i], ";" i = i + 1 print some_list[3] list1 = [2,5,6,3,7,8,12] list2 = [1,2,6,5,7,3,12] i = 0 while i < 6: if list1[i] == list2[i]: print list1[i], ";", i = i + 1 print len(list1)Page 3 Day 6: Objects and References Notes: Lists are fundamentally different than everything we've seen to date. They are the first example we've seen of an object. You've learned that we can use variables to store information in a table. This works for simple things like numbers, but not for complex information like a list. Instead, Python stores this information in an area of memory called the heap. Understanding what the heap is isn't important. What is important to know, however, is that instead of storing objects in the variable table, Python stores a pointer or reference to the object. A reference is like an arrow. The easiest way to explain this is with an example: Program Text: Output: At point 1, the variable table looks something like the following. After this, we pass the location of the list (which is the value stored in the variable) to the function. Thus, at point 2 both variables point to the same thing. example_list = [] def append_one(my_list): "This is a function that adds something to the end of a list" #Point 2 my_list.append(1) #Point 3 #Point 1 append_one(example_list) print example_list #Point 4 [1]Page 4 We change the list afterwards, and the table looks like the following at point 3. The local variable disappears when the function ends, and at point 4 the variable table looks like the following. Hence the output of the code is [1]! This differs from problem 10 – since these variables are storing arrows, any changes we make within the function are reflected outside of it too. This arrow is literally stored as an integer in the table – think of it as a locker number. We can find out the value of this number using the id(x) function.Page 5 Program Text: Output: Note: Your numbers may differ from mine. The first and last numbers will still be the same, though. You probably won't need to use the id function ever. Its really useful for figuring out whether two variables are pointing to the same list or not, though. Problem 13: What is the output of the following code. Use single digits for the output of id(x). Program Text: Output: def small_function(my_list): print "in function:" print id(my_list) my_list.append(1) example_list = [] example2_list = [3,6] print id(example_list) print id(example2_list) small_function(example_list) 18162752 12860152 in function: 18162752 some_list = [1,3,2] def f1(my_list): new_list = [2] print id(my_list), "; ", print id(new_list), ";", print id(some_list), "; ", f1(some_list)Page 6 Program Text: Output: Day 6: Mutability Notes: Now that you understand the true nature of a list, we can talk about the concept of mutability. Lists are an example of a mutable object. Mutability is a synonym for changable – in essence, we're saying that you can change a list. Be careful -- while this sounds very simple, it can get quite tricky. For the next problem, the references example will help a lot. Remember that statements like list1 = [1,2,5] change the reference (what the arrow is pointing to) and statements like list1.append(5) change the list itself. Problem 14: What is the output of the following code. Program Text: Output: some_list = [1,3,2] def f1(my_list) my_list.append(7) print id(my_list), “;”, return [1,4,5] print id(some_list), “;”, some_list.append(5) print id(some_list), “;”, some_list = [4,3,7] print id(some_list), “;”, some_list = f1(some_list) print id(some_list) list1 = [1,2,4] list2 = list1 list2.append(3) print list1Page 7 Program Text: Output: Program Text: Output: Program Text: Output: example_list = [1,2,4] def f1(my_list): my_list.append(3) f1(example_list) print example_list list1 = [1,2,4] list2 = [1,2,4] list2.append(3) print list1 example_list = [1,2,4] def f1(my_list): my_list = [1,2,4,3] f1(example_list) print example_listPage 8 Program Text: Output: We’ll talk


View Full Document

MIT 6 189 - List Basics

Download List Basics
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view List Basics and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view List Basics 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?