DSA Flashcards
midterms
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()
The _____ will delete the set completely
del keyword
del thisset
The union of two sets is calculated by using the ____
using pipe (|) operator:
print(Days1|Days2)
using union() method:
print(Days1.union(Days2))
The intersection of two sets can be performed by the___ or the ____
& operator:
print(Days1&Days2)
intersection() function:
set1.intersection(set2)
The intersection_update() method is different from the intersection() method since it modifies the original set by____
removing the unwanted items
what is the output?
a = {“Devansh”, “bob”, “castle”}
b = {“castle”, “dude”, “emyway”}
c = {“fuson”, “gaurav”, “castle”}
a.intersection_update(b, c)
print(a)
{‘castle’}
# the intersection between a, b, c is the only one retained in a
The difference of two sets can be calculated by using the ___ or ___
subtraction (-) operator :
print(Days1-Days2)
difference() method:
Days1.difference(Days2)
The symmetric dfference of two sets is calculated by_____ or ____.
^ operator :
c = a^b
symmetric_difference() method:
c = a.symmetric_difference(b)
it removes that element which is present in both sets.
Symmetric difference of sets
The ____ is given as the set of the elements that common in both sets
intersection of the two sets
is a collection which is unordered, changeable and indexed.
dictionary
in a dictionary, the keys and values are:
Keys must be a single element
Value can be any type such as list, tuple, integer, etc.
format of a dictionary
dict = {key:value, key:value}
how to call dictionary values
Employee.get(“Age”)
or
print(“Age : %d”%Employee[“Age”])
Adding elements to dictionary one at a time
Dict[0] = ‘Peter’
The items of the dictionary can be deleted by using the ___
del keyword
difference between pop() and popitem()
dict.pop(key)
#removes a specific key
dict.popitem()
#removes last
When looping through a dictionary, the return value are the __ of the dictionary
keys
for loop in dictionary
for loop to print all the keys of a dictionary:
for x in Employee:
print(x)
for loop to print all the values of the dictionary
for x in Employee:
print(Employee[x])
for loop to print the values of the dictionary by using values() method:
for x in Employee.values():
print(x)
for loop to print the items of the dictionary by using items() method:
for x in Employee.items():
print(x)
properties of dict Keys
- cannot store multiple values for the same keys
- last assigned is considered as the value of the key
- key cannot be any mutable object
The ____ statement is used to test as specific condition if it is true
if statement
if condition :
The ____statement is used to test as specific condition if it is false
else statement
else :
it enables us to use if else statements inside an outer if statement
nested if statement
if condition :
if condition:
else:
else
in python, ____ is used to declare a block
indentation
typical number of spaces for indention
4 spaces
if an ‘if clause’ consist of only 1 line, it may go to the ____ as the as the header statement
same line
“if the previous conditions were not true, then try this condition”
elif statement
it enables us to alter the code to repeat in finite number of times
loop / looping
advantages of looping statements
code re-usability
prevent redundancy
we can traverse over the elements of data structures
used when the number of iterations isnt known
while loop
the block of statement is executed until condition specified is satisfied
while loop
other term for while loop
pre-tested loop
used to execute a part of code until given condition is satisfied. Better to use if iteration is known in advance
for loop