{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info iconThis 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']
Background 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 )
Background 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!
Background 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 ]}