7.1 and 7.2 Comp Sci Quiz

¡Supera tus tareas y exámenes ahora con Quizwiz!

7.2.4 List of Tuples, Tuples of Lists

""" This program displays the rules of modifying lists and tuples. """ # Here we have a list with a tuple embedded my_list = ["a", "b", "c", (1, 2, 3)] print(my_list) # You can alter the list this way because you are allowed to replace a tuple # and you are allowed to change a list. my_list[3] = (10, 2, 3) print(my_list) # Here we have a tuple with a list embedded my_tuple = ("a", "b", "c", [1, 2, 3]) print(my_tuple) # You can alter the tuple by changing the element inside the list because # you are allowed to change a list. my_tuple[3][0] = 10 print(my_tuple)

7.2.5 String <--> List

""" This program shows how strings can be turned into lists and lists into strings. """ my_string = "abcde" print(my_string) # Turn string into a list my_list = list(my_string) print(my_list) # Turn list into a string my_other_string = "".join(my_list) print(my_other_string)

7.1.6 Concatenating Tuples

""" This program shows how to concatenate tuples. """ x = (1, 2) y = (5, 6) my_tuple = x + (3, 4) + y print(my_tuple) my_tuple = x + (3,) + y # This should print (1, 2, 3, 5, 6) print(my_tuple)

7.1.5 Tuples With a Single Element

""" This program shows how to create a tuple with a single element. """ # This just creates an integer... x = (3) print(x) print(type(x)) # This creates a tuple x = (3,) print(x) print(type(x))

7.1.4 A Tuple is Heterogenous

""" This program shows the different elements that can be found within a tuple. """ my_tuple = (0, 1, "hi", (2, 3)) print(my_tuple[0]) print(my_tuple[1]) print(my_tuple[2]) # my_tuple[3] is, itself, a tuple. print(my_tuple[3]) # The first square bracket accesses the tuple (2, 3) within the larger tuple. # The second square bracket accesses the integers within this smaller tuple. print(my_tuple[3][0]) print(my_tuple[3][1])

7.1.3 A Tuple Is a Sequence

""" This program shows the different ways to index a tuple. """ my_tuple = (1, 2, 3, 4, 5) print(my_tuple[0]) print(my_tuple[:2]) print(len(my_tuple)) # -- THE LINE BELOW WOULD CAUSE AN ERROR -- # my_tuple[0] = 10

7.2.7 Splitting a String

""" This program will turn a phrase into a list by separating elements at the whitespace (or spaces) entered. """ my_string = "try to be a rainbow in someone's cloud" print(my_string) my_list = my_string.split() print(my_list)

Consider the following code segment. The indices on the exam are different. They do not start at 0, but rather at 1. aList ← ["dog", false, "bird"] Which of the following will throw an error? I. aList[0]II. aList[2]III. aList[3]IV. aList[4] I and II I and IV II and IV III and IV

I and IV

Which of the following is NOT true about tuples? Tuples are ordered. Tuples are immutable. Tuples can contain elements of different types. Tuples use parentheses to access individual elements, rather than square brackets.

Tuples use parentheses to access individual elements, rather than square brackets.

Consider the following code segment. aList ← [20, 40, 60] bList ← [10, 30, 50] aList ← bList bList ← [15, 45, 90] What is stored in aList? [15, 45, 90] [10, 30, 50] [20, 40, 60] [ ] (empty set)

[10, 30, 50]

Which of the following will create an empty list and assign it to aList? aList ← 0 aList ← [0] aList ← [ ] aList ← " "

aList ← [ ]

The AP exam uses the notation [index1, index2, index3...] to create a list with those values as the first, second, third, and so on elements. The indices on the exam are different. They do not start at 0, but rather at 1. Consider the following code segment. aList ← [15, "hello", true] Which element can be found at index 2? aList 15 hello true

hello

Which of the following Python programs creates a list with the numbers 1 through 5? my_list = (1, 2, 3, 4, 5) my_list = [1, 2, 3, 4, 5] my_list = 1, 2, 3, 4, 5 my_list = "1, 2, 3, 4, 5"

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

Which of the following is the correct way to declare a tuple? x = (1, 2, 3) x = [1, 2, 3]

x = (1, 2, 3)


Conjuntos de estudio relacionados

Acidic, Basic, and Neutral Solutions

View Set

Chapter 3, Add Two-Digit Numbers - Grade 2

View Set

Abeka 7th grade History Section 22.3 Review questions 7-11

View Set

Illinois rules of the road chapter 7 and 8 study questions

View Set