python12.html - CHAPTER 12 lists the following are true for...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CHAPTER 12 lists the following are true for lists, strings, and tuples: -- The elements are ordered (i.e. order matters) -- you can access each of the elements using an index -- indices start at 0 -- you can iterate over them (e.g. use them in a for) -- you can use the function len() to get the number of elements -- You can test for the existence of an element using the in operator (or for the non-existence by using not in). -- support the use of the operators + and * ss = "hello" #ss = ('h','e','l','l','o') #ss = ['h','e','l','l','o'] print(len(ss)) for x in ss: print(x) print('e' in ss) print('x' not in ss) in tuples and lists you can apply the max(), min() and sum() functions. main difference between tuples and lists: lists are mutable tuples are inmutable #strings and tuples cannot be changed ss = ('h','e','l','l','o') #tuple #ss[0] = 'x' #ERROR! print(ss) ss = "hello" #string #ss[0] = 'x' print(ss) # ERROR! ss = ['h','e','l','l','o'] #list ss[0] = 'x' print(ss) # ['x', 'e', 'l', 'l', 'o']
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
replace a slice with a list fruitlist = ["apple", "banana", "cherry", "durian", "orange"] print( fruitlist ) fruitlist [1:3] = ["raspberry", "strawberry", "blueberry"] # ['apple', 'raspberry', 'strawberry', 'blueberry', 'durian', 'orange'] print( fruitlist ) what is printed? fruitlist = ["a", "b", "c", "d", "e"] fruitlist [2:4] = ["x", "y", "z"] print( fruitlist ) ['a', 'b', 'x', 'y', 'z', 'e'] what is printed? fruitlist = ["a", "b", "c", "d", "e"] fruitlist [2:2] = ["x", "y", "z"] print( fruitlist ) ['a', 'b', 'x', 'y', 'z', 'c', 'd', 'e'] what is printed? fruitlist = ["a", "b", "c", "d", "e"] fruitlist [2:4] = [] print( fruitlist ) ['a', 'b', 'e'] Exercise01 Change a list that contains only words by turning every word in the list into the same word consisting of only capitals. what is printed? fruitlist = ["apple", "banana"] fruitlist += "cherry" print( fruitlist ) ['apple', 'banana', 'c', 'h', 'e', 'r', 'r', 'y'] what is printed? fruitlist = ["apple", "banana"] fruitlist += ["cherry"] print( fruitlist )
Image of page 2
['apple', 'banana', 'cherry'] append -- what is printed? fruitlist = ["apple", "banana", "cherry", "durian"] fruitlist.append( "orange" ) print( fruitlist ) ['apple', 'banana', 'cherry', 'durian', 'orange'] Notice that the list has been changed!
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern