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()
What is the output of the list example below?
Samlist = [“sun”, “moon”,”star”,”sky”]
Samlist.remove(“star”)
print(Samlist)
[“sun”, “moon”,”sky”]
What is the output of the list example below?
Samlist = [5,8,2,6,4,7,3]
Samlist.pop()
print(Samlist)
[5,8,2,6,4,7]
What is the output of the list example below?
Samlist = [5,8,2,6,4,7,3]
Samlist.pop(4)
print(Samlist)
[5,8,2,6,7,3]
What is the output of the list example below?
Samlist = [“sun”, “moon”,”star”,”sky”]
del Samlist[1]
print(Samlist)
[“sun”,”star”,”sky”]
What is the output of the list example below?
Samlist = [5,8,2,6,4,7,3]
del Samlist[4]
print(Samlist)
[5,8,2,6,7,3]
What is the output of the list example below?
Samlist = [“sun”, “moon”,”star”,”sky”]
Samlist.clear
print(Samlist)
[]
Use the ________ function, to return the number of times an item appears in the list.
remove ()
pop()
clear()
count()
count()
The _______ method will empty the list.
remove ()
pop()
clear()
count()
clear()
Use ________ method to return the items of the list in reverse order
reverse()
sort()
extend()
copy()
reverse()
What is the output of the list example below?
Samlist = [“sun”, “moon”,”star”,”star”,”sky”]
X = Samlist.count(“star”)
print(X)
2
What is the output of the list example below?
Samlist = [5,8,2,6,4,7,3]
X = Samlist.count(1)
print(X)
0
Use ____ method to return the items of the list in ascending order.
reverse()
sort()
extend()
copy()
sort()
Use ____ method to add the elements of a list to the end of the current list
reverse()
sort()
extend()
copy()
extend()
Use ____ method to copy the list.
reverse()
sort()
extend()
copy()
copy()
What is the output of the list example below?
Samlist = [“sun”, “moon”,”star”,”sun”]
Samlist.reverse()
print(Samlist)
[“sun”,”star”, “moon”,”sun”]
What is the output of the list example below?
Samlist = [5,8,2,6,4,7,3]
Samlist.reverse()
print(Samlist)
[3,7,4,6,2,8,5]
What is the output of the list example below?
Samlist = [“sun”, “moon”,”star”,”sun”]
Samlist.sort()
print(Samlist)
[ “moon”,”star”,“sun”,”sun”]
What is the output of the list example below?
Samlist = [“sun”, “moon”,”star”]
Addlist = [“rain”,”snow”]
Samlist.extend(Addlist)
print(Samlist)
[“sun”, “moon”,”star”,”rain”,”snow”]
What is the output of the list example below?
Samlist = [“sun”, “moon”,”star”]
Copylist = Samlist.copy()
print(Copylist)
[“sun”, “moon”,”star”]
A list is/can:
Ordered
Changeable
Duplicate Member
Indexed
Ordered
Changeable
Duplicate Member
Indexed
A Dictionary is/can:
Ordered
Changeable
Duplicate Member
Indexed
Changeable
Indexed
A Tuples is/can:
Ordered
Changeable
Duplicate Member
Indexed
Ordered
Duplicate Member
Indexed
A Sets is/can:
Ordered
Changeable
Duplicate Member
Indexed
Changeable
a collection which is ordered and unchangeable.
Tuples
is a collection written with round () brackets
Tuples
In tuples index starts with 0
True or False
T
The syntax below is a ______:
Var_name=( , , , ….)
tuple
What is the output of the tuple example below?
Samtuple= (“summer”, “winter”,”fall”,”spring”)
print(Samtuple[2])
fall
What is the output of the tuple example below?
Samtuple= (1,2,3,4,5)
print(Samtuple[3])
4
What is the output of the tuple example below?
Samtuple= (“summer”, “winter”,”fall”,”spring”)
print(Samtuple[1:3])
(“winter”,”fall”)
What is the output of the tuple example below?
Samtuple= (1,2,3,4,5)
print(Samtuple[2:4])
(3,4)
What is the output of the tuple example below?
Samtuple= (1,2,3,4,5)
print(Samtuple[2:])
(3,4,5)
What is the output of the tuple example below?
Samtuple= (“summer”, “winter”,”fall”,”spring”)
print(Samtuple[-2])
fall
What is the output of the tuple example below?
Samtuple= (1,2,3,4,5)
print(Samtuple[-3:-1])
(3, 4)
What is the output of the tuple example below?
Samtuple= (1,2,3,4,5)
for x in Samtuple:
…..print(x)
1 2 3 4 5
What is the output of the tuple example below?
Samtuple= (1,2,3,4,5)
print(len(Samtuple))
5
Like lists, you can add more items to a tuple once it is created
(True or False)
F
Tuples are immuttable/unchangeable. You cannot add items to it once tuple is created
You cannot remove items in a tuple but you can delete the tuple completely
(True or False)
T
use the _______ keyword to delete the tuple completely.
del
index
count
plus (+)
del
What is the output of the tuple example below?
Samtuple= (1,2,3,4,5)
del Samtuple
print(len(Samtuple))
error
the tuple is not defined
For tuples, use the ______ method to search for the first occurrence of the value and return the index position
del
index
count
plus (+)
index
What is the output of the tuple example below?
Samtuple= (1,2,3,4,5)
print(Samtuple.index(2))
1
For tuples, use the ______ method to return the number of time an item occurs in a tuple
del
index
count
plus (+)
count
For tuples, use the ______ operator to join two or more tuples.
del
index
count
plus (+)
plus (+)
a collection which is unordered and unindexed.
Sets
Sets are written with ________ brackets
curly {}
The syntax below is a ______:
Var_name={ , , , ….}
Set
since is ______ unindexed and unordered, you cannot access items in a set by referring to an index.
set
list
tuple
set
An iteration can be used through the set of items or use in keyword to specify an item in the sets.
(True or False)
T
Once a set is created, you cannot change its items.
True or False
T
What is the output of the list example below?
Samlist = [5,8,2,6,4,7,3]
Samlist[2]= 3
print(Samlist)
[5,8,3,6,4,7,3]
The item in index 2 is replaced by 3
______ function: converts tuple to list
list()
______ function: converts list to tuple
tuple()
used to add items to a list – item is appended to the end of the existing list
append() index(item) insert(index, item) sort() remove(item) reverse()
append()
used to determine where an item is located in a list. Returns the index of the first element in the list containing item
append() index(item) insert(index, item) sort() remove(item) reverse()
index(item)
used to insert item at position index in the list
append() index(item) insert(index, item) sort() remove(item) reverse()
insert(index, item)
used to sort the elements of the list in ascending order
append() index(item) insert(index, item) sort() remove(item) reverse()
sort()
removes the first occurrence of item in the list
append() index(item) insert(index, item) sort() remove(item) reverse()
remove(item)
reverses the order of the elements in the list
append() index(item) insert(index, item) sort() remove(item) reverse()
reverse()
_______ built-in functions that returns the item that has the lowest or highest value in a sequence
min and max functions
use the _______ method to add multiple items to the set.
update()
discard()
difference()
intersection()
update()
use the _______ method to return a set that contains the items that exist in both set x and in set y.
update()
discard()
difference()
intersection()
intersection()
use the _______ method to return a set that contains the items that only exist in set x and not in set y.
update()
discard()
difference()
intersection()
difference()
use the _______ method to remove the specified item from both sets.
update()
discard()
difference()
intersection()
discard()
What is the output of the tuple example below?
Samtuple1= (1,2,3)
Samtuple2= (4,5)
Samtuple1 = Samtuple1 + Samtuple2
print(Samtuple1)
(1, 2, 3, 4, 5)
combining tuples
What is the output of the set example below?
Samset= {1,2,3}
Samset.add(4)
print(Samset)
{1, 2, 3, 4}