• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/103

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

103 Cards in this Set

  • Front
  • Back

A list is ...

a sequential collection of Python data values

The values that make up a list are called

elements

lists are similar to strings (which are ordered sequences of characters) except for what (2 things)

the elements of a list can be any type, and elements within a list can be of any type. strings can only have characters.

a list within another list is said to be ____ and the inside list is called a ____

nested, sublist

a list with no elements is called _____ and it's denoted by ____

empty list, []

alist = [[5, 10], "cat", "dog", 4, 5]


return len(alist)



What will len(alist) evaluate to? Explain.

5, because [5, 10] is a sublist, which counts as 1

What is printed by the following statements:


numbers = [17, 123, 87, 34, 66, 8398, 44]


print(numbers[-2])


print(numbers[len(numbers)-1])

8398


44

What is printed by the following statements:


alist = [5, "cat", ["dog, "boy", "girl"], 8, "man"]


print(alist[1][2])


print(alist[2][2])

t


girl

What are the boolean operators you use to find out if an element is in a list?

in


not in

How do you assign the list [1, 2, 3] to variable apple?

apple = [1, 2, 3]

alist = ["cat", [1, 2, 3], "dog]


print("cat" in alist)


print(2 in alist)



What gets printed? Why?

True


False (2 is within a sublist; isn't a top level item)

What gets printed?


print([1, 2, [1, 2]] * 2)

[1, 2, [1, 2], 1, 2, [1, 2]]

What gets printed?


print([1, 2] + [3, 4])

[1, 2, 3, 4]

What gets printed?


print(["man", "woman"] + [1, 2, 3])

["man", "woman", 1, 2, 3]

What gets printed?


print([2] * 4)

[2, 2, 2, 2]

What gets printed?


alist = [1, 2, 3, 4, 5]


print([1:])

[2, 3, 4, 5]

What gets printed?


alist = ["a", "b", "c", "d"]


print([:3])

["a", "b", "c"]

What gets printed?


alist = [1, 2, 3, 4, 5]


print([:])

["a", "b", "c", "d"]

What gets printed?


alist = ["a", "b", "c", "d"]


print([1:3])

["b", "c"]

What gets printed?


alist = ["a", "b", "c", "d"]


print([2:3])

["c"]

Are lists mutable?

yes

Are strings mutable?

No

Write a statement to change "cherry" to "orange" in the following list:


fruit = ["banana", "apple", "cherry"]


What prints before and after you make the change in the following:


print(fruit)

fruit[-1] = "orange"


or


fruit[2] = "orange"


before: ["banana", "apple", "cherry"]


after: ["banana", "apple", "orange"]

Change "apple" to "pear" and "cherry" to "orange". (don't make a new list)


fruit = ["banana", "apple", "cherry"]


fruit[1:3] = ("pear", "orange")

Use slice operator to Remove "apple" and "banana" from the list. (don't make a new list)


fruit = ["banana", "apple", "cherry"]

fruit[0:2] = []

Add "orange" and "pear" in between "banana" and "apple". (don't make a new list)


fruit = ["banana", "apple", "cherry"]

fruit[1:1] = ["orange", "pear"]

Add "pear" to the end of the list. (don't make a new list)


fruit = ["banana", "apple", "cherry"]

fruit[3:3] = ["pear"]

Delete "apple" from list. Do not use slice operator.


fruit = ["banana", "apple", "cherry"]

del fruit[1]

Delete "apple" and "cherry" from list. Use del and a slice operator.


fruit = ["banana", "apple", "cherry"]

del fruit[1:3]

What operator can you use to figure out if two variables refer to the same object?

is

a = "banana"


b = "banana"


print(a is b)


What is printed? What does this mean?

True


This means that the variables a and b refer to the same object, "banana" (stirngs are immutable)

Draw a reference diagram representing:


a = "banana"


b = "banana"

a = [81, 82, 83]
b = [81, 82, 83]


print(a is b)


print(a == b)


What is printed? Why?

False


True


Because lists are mutable, so a and b are each referring to their own list.

Draw a reference diagram representing:


a = [81, 82, 83]
b = [81, 82, 83]

A list is a collection of _____ to objects

references

a = [81, 82, 83]
b = a
print(a is b)


What prints? Why?


What term refers to the list because of this? (the list is ____ed)

True, because variables refer to objects, and both variables refer to the same object


The list is aliased.

a = [81, 82, 83]
b = a


Draw a reference diagram


When a list has two different variables referring to it, it is ___ed.

aliased

a = [81, 82, 83]
b = a


b[0] = 5


print(a)


What prints?

[5, 82, 83]

What do you do if you want to modify a copy of a list and keep the original? Give the term as well as the statement to do this with the following list:


a = [1, 2, 3]

clone the list


b = a[:]


Use the slice operator!

a = [1, 2, 3]


b = a * 2


c = [a] * 2


print(b)


print(c)



What prints for each print statement?

print(b) evaluates to [1, 2, 3, 1, 2, 3]


print(c) evaluates to [[1, 2, 3], [1, 2, 3]]

a = [1, 2, 3]


c = [a] * 2


Draw a reference diagram

a = [1, 2, 3]


c = [a] * 2


a[1] = 9


print(c)


What prints?


Draw a reference diagram

[[1, 9, 3], [1, 9, 3]]


 

[[1, 9, 3], [1, 9, 3]]


What prints?


mylist = []


mylist.append(1)


mylist.append(2)


mylist.append(3)


print(mylist)

[1, 2, 3]

What prints?


mylist = [1, 2, 3]


mylist.append(4)


print(mylist)

[1, 2, 3, 4]

What prints?


mylist = [1, 2, 3, 4]


mylist.insert(2, 5)


print(mylist)

[1, 2, 5, 3, 4]

What prints?


mylist = [1, 4, 2, 4, 3, 4]


a = mylist.count(4)


print(a)

3

What prints?


mylist = [1, 4, 2, 4, 3, 4]


a = mylist.index(3)


print(a)

4

What prints?


mylist = [1, 4, 2, 4, 3, 4]


a = mylist.reverse()


print(a)

[4, 3, 4, 2, 4, 1]

What prints?


mylist = [1, 4, 2, 4, 3, 4]


a = mylist.sort()


print(a)

[1, 2, 3, 4, 4, 4]

What prints?


mylist = [1, 4, 2, 4, 3, 4]


mylist.pop()


print(mylist)

[1, 4, 2, 4, 3]

What prints?


mylist = [1, 4, 2, 4, 3, 4]


mylist.pop(1)


print(mylist)

[1, 2, 4, 3, 4]

What prints?


mylist = [1, 4, 2, 4, 3, 4]


mylist.remove(2)


print(mylist)

[1, 4, 4, 3, 4]

What prints?


mylist = [1, 4, 2, 4, 3, 4]


mylist.remove(4)


print(mylist)

[1, 2, 4, 3, 4]

the pop method removes which element of a list (if no parameter specified)

the last

how do you specify which element of a list is removed using the pop method?

you pass in a parameter specifying the index of the element. For example alist.pop(3)


3 is the index of the item you want to remove

mylist = [4, 4, 3, 4, 1, 2]


mylist.remove(4)


This will remove all of the 4's or just the first 4?

Just the first 4

mylist = [4, 4, 3, 4, 1, 2]


mylist = mylist.sort()
print(mylist)



What prints?

None (and you get an error)

mylist = [4, 4, 3, 4, 1, 2]


mylist.sort()
print(mylist)



What prints?

[1, 2, 3, 4, 4, 4]

alist = [4, 2, 8, 6, 5]


alist = alist.pop(0)


print(alist)



What prints?

4



The list is lost.

Add "cat" to the end of the following list:


alist = [1, 2, 3]



- using the append method


- using concatenation

append method: alist.append("cat")


concatenation: alist = alist + ["cat"]

alist = [4, 2, 8, 6, 5]


alist = alist + 999


print(alist)



What prints?

Nothing; you get an error b/c you can't concatenate a list with an integer. You need to put 999 in a list [999]


What is it called when you iterate over a list?

list traversal

Write a for loop to iterate over the following list by index and print each item in the list.



alist = ["cat", "dog", "mouse"]

for i in range(len(alist)):


print(alist[i])



Write a for loop to iterate over the following list by item and print each item in the list.



alist = ["cat", "dog", "mouse"]

for i in alist:


print(i)

Write a for loop to iterate over the following list by item and print the index of each item in the list.



alist = ["cat", "dog", "mouse"]

for i in alist:


print(alist.index(i))

Functions that take lists as arguments and change them during execution are called ______ and the changes they make are called ________.

modifiers


side effects

if you pass a list in as a parameter for a function, then change the list in the function, does that change the original list?

yes, it changes the original list

things = [1, 2, 3]



def doubleStuff(aList):
for position in range(len(aList)):
aList[position] = 2 * aList[position]


doubleStuff(things)


print(things)



What prints?


[2, 4, 6]


(The original list that things references to, changed.)


things = [1, 2, 3]



def doubleStuff(aList):
for position in range(len(aList)):
aList[position] = 2 * aList[position]


doubleStuff(things)



draw a reference diagram for alist and things.

A pure function does not produce ____; meaning it does not alter the ____.

side effects; parameters

Write the following modifier function as a pure function:



def doubleStuff(aList):


for position in range(len(aList)):
aList[position] = 2 * aList[position]


def doubleStuff(aList):


newlist = []


for i in aList:


newlist.append(i * 2)


return newlist



Should you write modifier or pure functions?

write pure functions but only use modifiers only if there's a very huge advantage.

Whenever you need to write a function that creates and returns a list, the pattern is usually...

What's the easier way to do this?  Write out the general syntax for this easier way.

What's the easier way to do this? Write out the general syntax for this easier way.

list comprehension


[<expression> for <item> in <sequence> if <condition>]


(the if clause is optional)

def doubleStuff(aList):


newlist = []


for i in aList:


newlist.append(i * 2)


return newlist



Rewrite this function using list comprehension

newlist = [i * 2 for i in aList]

def doubleStuff(aList):


newlist = [i * 2 for i in aList]


return newlist



Rewrite this function with an if clause to exclude any values of aList less than 2


def doubleStuff(aList):


newlist = [i * 2 for i in aList if i >= 2]


return newlist

What is the nested list?



nested = ["hello", 2.0, 5, [10, 20]]

[10, 20]

nested = ["hello", 2.0, 5, [10, 20]]
innerlist = nested[3]
print(innerlist)


What prints?

[10, 20]

nested = ["hello", 2.0, 5, [10, 20]]


Write two different ways extract and print the 10.

innerlist = nested[3]


print innerlist[0]



print nested[3][0]

if True:


print "true"



Will anything print?

Yes.


"true"



What method do you use to break a string into a list of words?

string.split()

What prints?



song = "The rain in Spain..."
wds = song.split()
print(wds)

["The", "rain", "in", "Spain"]

With the split method, how can you specify a certain character/certain characters as the word boundary? answer: Use a ______.



Write a statement to split the following string using the characters "ai" as a word boundary.



spain = "The rain in Spain"



What does the statement evaulate to?

Use a delimiter.



"ai" is the delimiter in the following statement:



spain.split("ai")




spain = "The rain in Spain"


words = spain.split("ai")


print(words)

["The r", "n in Sp", "n"]

the inverse of the split method is the _____ method

join

Use the join method to join the elements of the following list as one string:


- using ; to join the elements


- with no space between the elements



colors = ["red", "blue", "green"]


";".join(colors)


"".join(colors)

Write the two separate statements to convert the following list:


colors = ["red", "blue", "green"]


to the following strings:



"redgreenblue"



"red;green;blue"

"".join(colors)


";".join(colors)

What's the method called that turns any sequence you give it to a list?



Write the general sytax to turn a string intoa list using this method.

type conversion fuction



list("string")

xs = list("Crunchy Frog")
print(xs)



What prints?

['C', 'r', 'u', 'n', 'c', 'h', 'y', ' ', 'F', 'r', 'o', 'g']



Note: the spaces are elements in the list too!

The split method will break a string into _____ while the list method will break a string into ____.

words; characters

What is a tuple? How are they like lists? How are they unlike lists?

A tuple, like a list, is a sequence of items of any time. Unlike lists, however, tuples are immutable.

What is the general syntax for a tuple assigment?

blahblah = ("blah", 5, "blah", "blah", 4)



blahblah is the tuple

What prints?


julia = ("Julia", "Roberts", 1967, "Duplicity", 2009, "Actress", "Atlanta, Georgia")
print(julia[2])

1967

julia = ("Julia", "Roberts", 1967, "Duplicity", 2009, "Actress", "Atlanta, Georgia")
print(julia[2:6])


1967 Duplicity 2009 Actress

Create a new tuple called roberts that replaces Duplicity and 2009 with Eat Pray Love and 2010



julia = ("Julia", "Roberts", 1967, "Duplicity", 2009, "Actress", "Atlanta, Georgia")

roberts = julia[0:3] + ("Eat Pray Love", 2010) + julia[5:]


Create a tuple called blah with the single element 5

blah = (5,)

haircolor = ("red", "brown", "black")



use tuple assignment to assign the following variables values as follows:



assign "red" to bob


assign "brown" to jim


assign "black" to greg

(bob, jim, greg) = haircolor

swap the values of the variables a and b using conventional assignment

temp = a


a = b


b = temp

swap the values of the variables a and b using tuple assignment

(a, b) = (b, a)



(variables on the left, values on the right)

What does this evaluate to:



(a, b, c) = (1, 2, 3, 4)

Error (number of variables on the left needs to equal the number of values on the right)

Add a return statement to the following function that returns both the add and mult values.


def calcAddMult(n1, n2):


add = n1 + n2


mult = n1 * n2


______________



print(calcAddMult(2, 3))


What prints?


return (add, mult)



5 6

What is a pattern?

A sequence of statements, or a style of coding something that has general applicability in a number of different situations