DSA Flashcards
long quiz
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 )
this operator concatinates the collection mentioned on either side of the operator
concatenation
tuple1 + tuple2
operator that returns true if particular item exist in collection, otherwise false
Membership
2 in tuple1
#true or false
used to get the length of collection
length
len(tuple1)
collection of unordered, unindexed, unchangeable, items that do not allow duplicate values
Set
The set can be created by enclosing the comma-separated immutable items with the ____
curly braces { }
the ____ can be used to create the set by the passed sequence
set() method
set4 = set()
Empty curly braces will create a what?
ex:
set3 = {}
dictionary
how to access items in a set?
using a for loop (for x in thisset: ) or using the in keyword (print(“banana” in thisset) )
how to add elements to set
add() method
set.add(“element”)
How many vaules does the set() method can hold?
ex:
Months =
set(“January”,”February”, “March”, “April”, “May”, “June”)
one
it should be Months =
set([“January”,”February”, “March”, “April”, “May”, “June”])
difference between the discard() and remove() method in a set
discard() function if the item does not exist in the set then the set remain unchanged
remove() method will through an error
The _____ empties the set:
clear() method
thisset.clear()