DSA Flashcards
midterms (170 cards)
four collection data types in the Python programming language
LIST, TUPLE, SET, DICTIONARY
4 properties of list
ordered, indexed, mutable (changeable), allow duplicates
which one is used in list ?
list = [1, 3, ‘hello’]
list = {1, 3, ‘hello’}
list = (1; 3 ; ‘hello’)
list = [1; 3 ; ‘hello’]
list = [1, 3, ‘hello’]
The list starts with what index in a forward direction?
index 0
The list starts with what index in a backward direction?
index -1
format in updating elements of list
list[index]= new value
To add an item to the end of the list, use the _____ method
append()
thislist.append(“orange”)
To determine if a specified item is present in a list use the _____
in keyword
if “apple” in thislist:
print(“Yes, ‘apple’ is in the list”)
To add an item at the specified index in a list, use the ____ method:
insert()
listName.insert(1, “orange”)
The ____ removes the specified item in a list
remove() method
listName.remove(“banana”)
The ____ removes the specified index in a list, (or the last item if index is not specified)
pop() method
listName.pop()
The ____ removes the specified index in a list
del keyword
del listName[index]
The _____ empties the list
clear() method
listName.clear()
a collection of objects which ordered and immutable
Tuple
differences between tuples and lists
tuples - unchangeable, uses ( ), more memory efficient
list - changeable, uses [ ], less memory efficient
how to create tuple
tup1 = (‘physics’, ‘chemistry’, 1997, 2000)
tup2 = (1, 2, 3, 4, 5 )
tup3 = “a”, “b”, “c”, “d”
The ____ is written as two parentheses containing nothing
empty tuple
tup1 = ()
To write a tuple containing a single value, you have to include a ______, even though there is only one value
comma
tup1 = (50,);
how are tuple items accessed ?
using their specific
index value
tup[index]
or
count = 0
for i in tuple1:
print(“tuple1[%d] = %d”%(count, i))
count = count+1
Tuples are _____ which means you cannot update or change the values of tuple elements
immutable
you can take _______ to create new tuples
portions of existing tuples
tup3 = tup1 + tup2;
the tuple items ____ be deleted by using the del keyword as tuples are _____
cannot, immutable
To delete
an entire tuple, we can use the_____ with the tuple name.
del keyword
del tuple1
this operator enables a collection elements to be repeated multiple times
repetition
tuple1 * 2
# (1, 2, 3, 4, 1, 2, 3, 4 )