DSA Flashcards

long quiz

1
Q

four collection data types in the Python programming language

A

LIST, TUPLE, SET, DICTIONARY

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

4 properties of list

A

ordered, indexed, mutable (changeable), allow duplicates

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

which one is used in list ?
list = [1, 3, ‘hello’]
list = {1, 3, ‘hello’}
list = (1; 3 ; ‘hello’)
list = [1; 3 ; ‘hello’]

A

list = [1, 3, ‘hello’]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The list starts with what index in a forward direction?

A

index 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

The list starts with what index in a backward direction?

A

index -1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

format in updating elements of list

A

list[index]= new value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

To add an item to the end of the list, use the _____ method

A

append()

thislist.append(“orange”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

To determine if a specified item is present in a list use the _____

A

in keyword

if “apple” in thislist:
print(“Yes, ‘apple’ is in the list”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

To add an item at the specified index in a list, use the ____ method:

A

insert()

listName.insert(1, “orange”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

The ____ removes the specified item in a list

A

remove() method

listName.remove(“banana”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

The ____ removes the specified index in a list, (or the last item if index is not specified)

A

pop() method

listName.pop()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

The ____ removes the specified index in a list

A

del keyword

del listName[index]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

The _____ empties the list

A

clear() method

listName.clear()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

a collection of objects which ordered and immutable

A

Tuple

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

differences between tuples and lists

A

tuples - unchangeable, uses ( ), more memory efficient
list - changeable, uses [ ], less memory efficient

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

how to create tuple

A

tup1 = (‘physics’, ‘chemistry’, 1997, 2000)
tup2 = (1, 2, 3, 4, 5 )
tup3 = “a”, “b”, “c”, “d”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

The ____ is written as two parentheses containing nothing

A

empty tuple

tup1 = ()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

To write a tuple containing a single value, you have to include a ______, even though there is only one value

A

comma

tup1 = (50,);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

how are tuple items accessed ?

A

using their specific
index value

tup[index]
or
count = 0
for i in tuple1:
print(“tuple1[%d] = %d”%(count, i))
count = count+1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Tuples are _____ which means you cannot update or change the values of tuple elements

A

immutable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

you can take _______ to create new tuples

A

portions of existing tuples

tup3 = tup1 + tup2;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

the tuple items ____ be deleted by using the del keyword as tuples are _____

A

cannot, immutable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

To delete
an entire tuple, we can use the_____ with the tuple name.

A

del keyword

del tuple1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

this operator enables a collection elements to be repeated multiple times

A

repetition

tuple1 * 2
# (1, 2, 3, 4, 1, 2, 3, 4 )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

this operator concatinates the collection mentioned on either side of the operator

A

concatenation

tuple1 + tuple2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

operator that returns true if particular item exist in collection, otherwise false

A

Membership

2 in tuple1
#true or false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

used to get the length of collection

A

length
len(tuple1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

collection of unordered, unindexed, unchangeable, items that do not allow duplicate values

A

Set

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

The set can be created by enclosing the comma-separated immutable items with the ____

A

curly braces { }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

the ____ can be used to create the set by the passed sequence

A

set() method

set4 = set()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

Empty curly braces will create a what?
ex:
set3 = {}

A

dictionary

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

how to access items in a set?

A

using a for loop (for x in thisset: ) or using the in keyword (print(“banana” in thisset) )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

how to add elements to set

A

add() method
set.add(“element”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

How many vaules does the set() method can hold?

ex:
Months =
set(“January”,”February”, “March”, “April”, “May”, “June”)

A

one
it should be Months =
set([“January”,”February”, “March”, “April”, “May”, “June”])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

difference between the discard() and remove() method in a set

A

discard() function if the item does not exist in the set then the set remain unchanged

remove() method will through an error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

The _____ empties the set:

A

clear() method
thisset.clear()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

The _____ will delete the set completely

A

del keyword

del thisset

37
Q

The union of two sets is calculated by using the ____

A

using pipe (|) operator:
print(Days1|Days2)

using union() method:
print(Days1.union(Days2))

38
Q

The intersection of two sets can be performed by the___ or the ____

A

& operator:
print(Days1&Days2)

intersection() function:
set1.intersection(set2)

39
Q

The intersection_update() method is different from the intersection() method since it modifies the original set by____

A

removing the unwanted items

40
Q

what is the output?
a = {“Devansh”, “bob”, “castle”}
b = {“castle”, “dude”, “emyway”}
c = {“fuson”, “gaurav”, “castle”}
a.intersection_update(b, c)
print(a)

A

{‘castle’}
# the intersection between a, b, c is the only one retained in a

41
Q

The difference of two sets can be calculated by using the ___ or ___

A

subtraction (-) operator :
print(Days1-Days2)

difference() method:
Days1.difference(Days2)

42
Q

The symmetric dfference of two sets is calculated by_____ or ____.

A

^ operator :
c = a^b
symmetric_difference() method:
c = a.symmetric_difference(b)

43
Q

it removes that element which is present in both sets.

A

Symmetric difference of sets

44
Q

The ____ is given as the set of the elements that common in both sets

A

intersection of the two sets

45
Q

is a collection which is unordered, changeable and indexed.

A

dictionary

46
Q

in a dictionary, the keys and values are:

A

Keys must be a single element
Value can be any type such as list, tuple, integer, etc.

47
Q

format of a dictionary

A

dict = {key:value, key:value}

48
Q

how to call dictionary values

A

Employee.get(“Age”)
or
print(“Age : %d”%Employee[“Age”])

49
Q

Adding elements to dictionary one at a time

A

Dict[0] = ‘Peter’

50
Q

The items of the dictionary can be deleted by using the ___

A

del keyword

51
Q

difference between pop() and popitem()

A

dict.pop(key)
#removes a specific key

dict.popitem()
#removes last

52
Q

When looping through a dictionary, the return value are the __ of the dictionary

A

keys

53
Q

for loop in dictionary

A

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)

54
Q

properties of dict Keys

A
  1. cannot store multiple values for the same keys
  2. last assigned is considered as the value of the key
  3. key cannot be any mutable object
55
Q

The ____ statement is used to test as specific condition if it is true

A

if statement

if condition :

56
Q

The ____statement is used to test as specific condition if it is false

A

else statement
else :

57
Q

it enables us to use if else statements inside an outer if statement

A

nested if statement

if condition :
if condition:
else:
else

58
Q

in python, ____ is used to declare a block

A

indentation

59
Q

typical number of spaces for indention

A

4 spaces

60
Q

if an ‘if clause’ consist of only 1 line, it may go to the ____ as the as the header statement

A

same line

61
Q

“if the previous conditions were not true, then try this condition”

A

elif statement

62
Q

it enables us to alter the code to repeat in finite number of times

A

loop / looping

63
Q

advantages of looping statements

A

code re-usability
prevent redundancy
we can traverse over the elements of data structures

64
Q

used when the number of iterations isnt known

A

while loop

65
Q

the block of statement is executed until condition specified is satisfied

A

while loop

66
Q

other term for while loop

A

pre-tested loop

67
Q

used to execute a part of code until given condition is satisfied. Better to use if iteration is known in advance

A

for loop

68
Q

other term for for loop

A

per-tested loop

69
Q

this statement continues until condition is satisfied

A

do-while loop

70
Q

used when executing the loop at least once is necessary (mostly menu driven programs)

A

do-while loop

71
Q

other term for do - while loop

A

post tested loop

72
Q

one or more loop inside a loop

A

nested loop

73
Q

while loop syntax

A

while condition:
task

74
Q

it stops the loop even if while condition is true

A

break

75
Q

it could stop the current iteration and continue with the next

A

continue

76
Q

run a block of code once condition is no longer true

A

else
ex:
while condition:
task
else :
task

77
Q

it is the variable that takes the value of item inside the sequence on each iteration

A

iterating_var

78
Q

used to iterate over a sequence (list, tuple, string)

A

for loop

79
Q

iterating over a sequence is called_____

A

traversal

80
Q

it generate a sequence of numbers

A

range() function

81
Q

what is the output?
print (list(range (2, 20, 3)))

A

[2, 5, 8, 11, 14, 17]

82
Q

what is the output?
print (range (2, 20))

A

range (2, 20)

83
Q

what is the output?
print (list(range (2, 5)))

A

[2, 3, 4]

84
Q

what is the output?
g = [1, 2, 3, 4, 5]
for i in range(len(g)):
print (“ i skip from”, g[i])

A

i skip from 1
i skip from 2
i skip from 3
i skip from 4
i skip from 5

85
Q

heavly used in list of list

A

nested loops

86
Q

All syntax in list

A

list[2] = 10 #assign value in index 2

thislist.append(“orange”) # add item on end

thislist.insert(1, “orange”) # insert item in specified index

thislist.remove(“banana”) # remove specific item

del thislist # remove whole list

thislist.clear() #empties list

87
Q

all syntax in tuple

A

tup1 = () # empy tuple

tup[1]= #access of item

tup3 = tup1 + tup2 # combine to create new tuple

tup * 2 # reiterate items twice

item in tup # prints true or false

for i in tup # print all items in tup

len(tup) # length of tup

88
Q

all syntax in set

A

s = set() # create empty set

for x in thisset
print(“banana” in thisset) # access item

Months.add (“August”) # adds one item to set
Months.update([“July”,”August”,”September”,”October”]); # multiple

hisset.remove(“banana”) # remove specified item but show error if not found

thisset.discard(“banana”) # remove item but if not found set is unchanged

this.clear() # empties set

del thisset # delete whole set

89
Q

All syntax in dictionary

A

dict = {} # empty
dict = dict({key: item})
dict = dict([(key, item),( key, item)])
Employee.get(“Age”) # get value
Employee[“Age”] # get value
Dict[key] = item # add one at a time
del Employee[“Name”] # delete item value
del Employee #delete dictionary
ditct.pop(key) # remove pair and transfer
ditct.popitem() # remove item and transfer