Module 1: Collections: List, Tuples, Set Flashcards
_________ is a built-in python module that provides useful container datatypes.
Collections
_________ datatypes allow us to store and access values in a convenient way.
Container
_________ in Python are containers that are used to store collections of data, for example, list, dict, set, tuple etc. and they are under the built-in collections.
Collections
a collection which is ordered and changeable, are written with square [ ] brackets
List
In list, index starts with 1, items are separated with comma
True or False
F
index starts with 0, items are separated with comma
lists include double quotation if the item is string
True or False
T
The syntax below belongs to the ________ collection:
var_name = [ , , , ….]
List
Samlist = [“sun”, “moon”,”star”,”cloud”] -> string values
The index of “moon” is _______
1
access the list items by referring to the _______ number
index
Samlist = [5,8,2,5,4] –> are ________ values
non-string
Samlist = [“sun”, “moon”,”star”,”cloud”] -> are _______ values
string
You can access list items by referring to the ________
index number
item
key
value
index number
What is the output of the list example below?
Samlist = [“sun”, “moon”,”star”,”cloud”]
print(Samlist [2])
[“star”]
What is the output of the list example below?
Samlist = [5,8,2,5,4]
print(Samlist[3])
[6]
in ________ you need to specify the range of indexes by specifying where to start and where to end the range. The return value will be a new list in the specified items
accessing range of items
Access range of items syntax:
print (var_list[start_index : end_index])
What is the output of the list example below?
Samlist = [5,8,2,6,4,1,3,7,9]
print(Samlist[2:6])
[2,6,4,1]
What is the output of the list example below?
Samlist = [5,8,2,6,4,1,3,7,9]
print(Samlist[0:2])
[5,8]
accessing range of items without a start or end index. syntax:
print (var_list[: end_index])
start with index 0
In the syntax:
print (var_list[: end_index])
the range will go on to the ______ of the list
end
What is the output of the list example below?
Samlist = [5,8,2,6,4,1,3,7,9]
print(Samlist[:4])
[5,8,2,6]
What is the output of the list example below?
Samlist = [5,8,2,6,4,1,3,7,9]
print(Samlist[5:])
[1,3,7,9]
means beginning from the end, means -1 refers to the last item of the list, -2 refers to the second last item and so on.
Negative indexing
______ of negative indexing needs to specify the negative indexes if you want to start the search form the of the list.
Range
What is the output of the list example below?
Samlist = [“sun”, “moon”,”star”,”cloud”]
print(Samlist [-1])
[“cloud”]
What is the output of the list example below?
Samlist = [5,8,2,6,4]
print(Samlist[-3])
[2]
What is the output of the list example below?
Samlist = [“sun”, “moon”,”star”,”cloud”,”sky”]
print(Samlist [-4:-1])
[“moon”,”star”,”cloud”]
What is the output of the list example below?
Samlist = [5,8,2,6,4]
print(Samlist[-5:-2])
[5,8,2]
Refer to the item name to change the value of a specific item.
(True or False)
F
Refer to the index number to change the value of a specific item.
Change Item Value in list syntax:
Additem[index]= new_item_value_inthelist.
What is the output of the list example below?
Samlist = [“sun”, “moon”,”star”,”cloud”,”sky”]
Samlist [3] = “rain”
print(Samlist)
[“sun”, “moon”,”star”,”rain”,”sky”]
What is the output of the list example below?
Samlist = [5,8,2,6,4]
Samlist [4] = 7
print(Samlist)
[5,8,2,6,7]
Using a ______ loop, you can loop the items in the list.
while
for
nested
for
What is the output of the list example below?
Samlist = [“sun”, “moon”,”star”,”cloud”,”sky”]
for x in Samlist:
…..print(x)
sun moon star cloud sky
What is the output of the list example below?
Samlist = [5,8,2,6,4]
for a in Samlist:
…..print(a)
5 8 2 6 4
Use the _____ keyword, to determine or check if a specified item is present in the list.
in
not
is
as
in
What is the output of the list example below?
Samlist = [“sun”, “moon”,”star”,”cloud”,”sky”]
if “moon” in Samlist:
…..print(“Yes, moon is in the list”)
Yes, moon is in the list
What is the output of the list example below?
Samlist = [5,8,2,6,4] if 3 in Samlist: .....print("3 is in the list") else: .....print("3 is not in the list")
3 is not in the list
Use the _______ function, to determine how many items a list has.
index
len
count
len
Use the ______ method, to add an item at the specified index.
append
index
insert
insert
To remove the specified item in a list, use the ______ method.
remove ()
pop()
clear()
count()
remove ()
to remove an item in the specified index in a list use the ______ keyword.
del
In a list, to remove the item in the specified index or the last item if index is not specified, use the ______ method
remove ()
pop()
clear()
count()
pop()