DOC PREVIEW
TAMU CSCE 110 - Review of Lists and Sets
Type Lecture Note
Pages 3

This preview shows page 1 out of 3 pages.

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

Unformatted text preview:

CSCE 110 1nd EditionLecture 18Outline of Last Lecture:I. List comprehension in simulationsA. Simulation ExampleB. Example 2II. Input/Output with list comprehensionOutline of Current Lecture:I. Review of basic propertiesA. List propertiesB. Set propertiesCurrent Lecture:I. Review of propertiesA. Lists - Input/OutputThis set of notes will just be a review of the properties we have already learned. We will only be concerned with the output given depending on what we type into a Python shell. The output will be highlighted in green.>>> a = [1, 2, 3] Here, we initialize the list, a>>> a*3 We are using a multiplication operator on a[1, 2, 3, 1, 2, 3, 1, 2, 3] This creates a list with all the elements, in order, three times>>> a = [1, 2, [3, 4], 5] We change the value of the list, a, (lists are mutable)>>> print len(a) We want to know the length of the list, a4 There are four elements in the list ([3, 4] is only one element)>>> print a[1:2] We are taking a range slice of the list[2] Because we took a range slice, the output will be in brackets>>> print a[1] We are taking just one index of the list2 Because we only take one element, it is not in brackets>>> print a[2:3] We want another range slice[[3,4]] Double brackets, since we are calling a list as a range slice>>> a[1:2] = [[20, 30], 40] We are resetting the value of the first index of the list>>> print a Note: we changed the individual index to two separate values[1, [20, 30], 40, [3, 4], 5] The length of the list has now increased by one>>> a = [1, 2, 3]>>> a[1] = [4] >>> print aThese notes represent a detailed interpretation of the professor’s lecture. GradeBuddy is best used as a supplement to your own notes, not as a substitute.[1, [4], 3]When we assign a new value to one index, we change it to exactly what is on the right side of the equal sign. Here, because we have the 4 in brackets, the new value of the index 1 of the list is a list, [4].>>> a = [1, 2, 3]>>> a[1:2] = [4]>>> print a[1, 4 3]When we change the value of a range slice, this is called a "slice assignment." We must put the new value in brackets, which will make the new value whatever is inside the first pair of bracketsonly. Here, we change the index 1 of the list to the integer 4, not a list only containing the element 4. If we didn't put the right side in any brackets at all, we would get an error.>>> a = [1, 2, 3]>>> a[1:2] = 'hello'>>> print a[1, 'h', 'e', 'l', 'l', 'o', 3]Because we did not put the new value of index 1 of the list in brackets (we are doing a slice assignment here), the list will add each index of the string to index 1 separate elements.>>> a = [1, 2, 3]>>> a[1:2] = ['hello']>>> print a[1, 'hello', 3]Because we now have the changed value for the range slice in brackets, it will be added to index1 as one element.>>> a = [1, 2, 3]>>> a[1] = 'hello'>>> print a[1, 'hello', 3]Because we are not doing a range assignment here, we do not need to put the new value in brackets.B. Sets - Input/OutputWe will look at the operations we have already learned. We can take intersections, unions, differences, add to sets, and check membership of sets using issubset (one word). Unions combine unique elements of two sets, and the order of the sets listed in the operation does not matter. If you switch the order, the output will be the same. Intersections of two sets takes only the elements that are in both sets, and once again, the order of the sets does not matter. When you take the difference of two sets, the elements of the second set listed will besubtracted from the first, which means that order does matter here! The order also matters for issubset, which gives a Boolean that denotes whether all elements of the first set given are elements in the second set. The add operator simply adds a specified element to a set. >>> A = set([14.7, 1, 'b'])>>> B = set([1, 'c', 14.7, 'dog'])>>> print A.union(B)set([1, 'c', 'b', 14.7, 'dog'])>>> print B.union(A)set([1, 'c', 'b', 14.7, 'dog'])>>> print A.intersection(B)set([14.7, 1])>>> print B.intersection(A)set([14.7, 1])>>> print A.difference(B)set(['b'])>>> print B.difference(A)set(['c', 'dog'])>>> print A.issubset(B)False>>> print B.issubset(A)False>>> C = set([1, 14.7, 'peach', 'aggie'])>>> print A.union(C.intersection(A))set([14.7, 'b',


View Full Document

TAMU CSCE 110 - Review of Lists and Sets

Type: Lecture Note
Pages: 3
Documents in this Course
06-IO

06-IO

29 pages

21-OOP

21-OOP

8 pages

key

key

6 pages

21-OOP

21-OOP

8 pages

Load more
Download Review of Lists and Sets
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 Review of Lists and Sets 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 Review of Lists and Sets 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?