DSA Flashcards

midterms

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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))

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

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

A

& operator:
print(Days1&Days2)

intersection() function:
set1.intersection(set2)

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

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

A

removing the unwanted items

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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)

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

it removes that element which is present in both sets.

A

Symmetric difference of sets

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

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

A

intersection of the two sets

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

is a collection which is unordered, changeable and indexed.

A

dictionary

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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.

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

format of a dictionary

A

dict = {key:value, key:value}

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

how to call dictionary values

A

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

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

Adding elements to dictionary one at a time

A

Dict[0] = ‘Peter’

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

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

A

del keyword

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

difference between pop() and popitem()

A

dict.pop(key)
#removes a specific key

dict.popitem()
#removes last

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

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

A

keys

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
55
Q

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

A

if statement

if condition :

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

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

A

else statement
else :

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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

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

in python, ____ is used to declare a block

A

indentation

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

typical number of spaces for indention

A

4 spaces

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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

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

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

A

elif statement

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

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

A

loop / looping

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

advantages of looping statements

A

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

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

used when the number of iterations isnt known

A

while loop

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

the block of statement is executed until condition specified is satisfied

A

while loop

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

other term for while loop

A

pre-tested loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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

90
Q

what is python

A

GDHI:
General Purpose,
Dynamic,
High-Level, and
Interpreted programming language

91
Q

T or F
Python provides high level data structures

A

True

92
Q

T or F
Python does not support Object Oriented Programming

A

False

93
Q

T or F
Python is hard and complicated to learn

A

False
it is simple and easy to learn

94
Q

Python is ___ than Java

A

3-5 times shorter

95
Q

Python’s run time ___ than Java

A

works harder

96
Q

T or F
Components can be developed in Java and combined to form applications in Python

A

True

97
Q

who developed Python

A

Guido van Rossum

98
Q

where is the name Python based from?

A

BBC comedy series:
Monty Python’s Flying Circus

99
Q

when was the BBC series aired on?

A

1970s

100
Q

qualities that Van Rossum wanted in the name

A

unique, sort, a little bit mysterious

101
Q

Python can be used to _____ into Java implementation

A

prototype components

102
Q

Python is often ____ shorter than C++

A

5-10 times

103
Q

python is a _____ language

A

glue

104
Q

T or F
python is used to combine components written in Javascript

A

False
(C++)

105
Q

T or F
you can run Python in a GUI environment

A

True

106
Q

Python is known for its ____ nature that makes it applicable in almost every domain of ____ development

A

general-purpose, software

107
Q

Python is the _____ programming language and can develop any application

A

fastest-growing

108
Q

enumerate all the Python applications

A

Audio or Video-based Applications
Business Applications
Console-based Applications
Desktop GUI Applications
Enterprise Applications
3D CAD Applications
Image Processing applications
Web Applications
Software Development
Scientific and Numeric

109
Q

3 ways to run a python program

A

Interactive Interpreter Prompt (IIP)
Script File (SF)
Integrated Development Environment (IDE)

110
Q

execute the python statement one by one

A

Interactive Interpreter Prompt (IIP)

111
Q

used when we are concerned with the output of each line

A

Interactive Interpreter Prompt (IIP)

112
Q

steps in opening the Interpreter mode

A

1.) open terminal (command Prompt)
2.) type python
3.) start coding line by line

113
Q

it writes multiple lines of code into a file which can be executed later

A

Script file

114
Q

steps in Script mode programming

A

1.) open text editor (notepad)
2.) create file
3.) save file with the .py extension
4.) open command line
5.)navigate to file directory and run

115
Q

first unix IDE for Python

A

IDLE

116
Q

first window interface for python which is an IDE with a ____

A

PythonWin, GUI

117
Q

Macintosh version of python

A

IDLE IDE and Python
(downloadable as MacBinary and BinHex’d file)

118
Q

a name used to identify a variable, function, class module, or other object

A

Python Identifier

119
Q

what do identifiers start?

A

A-Z
a-z
_ (underscore)

120
Q

Class Identifiers

A

uppercase letters (A-Z)

121
Q

other identifiers

A

lowercase (a-z)

122
Q

What is not allowed in Identifiers

A

Starting with numbers
Special characters as variable names ($, @, %)

123
Q

how to signify that an identifier is private

A

single leading underscore (_)

124
Q

Python reserved keywords

A

and, assert, break, class, continue, def, del, elif, else, except, exec, finally, for, from, global, if, import, in, is, lambda, not, or, pass, print, raise, return, try, while, with, yield

125
Q

how are blocks of code denoted in Python

A

line indention/ indention

126
Q

line continuation character

A

\

127
Q

it allows the line to continue even if they are not on the same line

A

line continuation character

128
Q

What statements do not need the use of line continuation character

A

statements with [], {}, or ()

129
Q

what is the use of a single and double quote (‘ and “)

A

used to denote string literals

130
Q

what is the use of a triple quote ( ‘’’ )

A

span the string across multiple lines

131
Q

used to make a next line

A

\n

132
Q

It signifies a comment

A

Hash sign (#)

133
Q

It signifies a multiline comment

A

triple quote ( ‘’’ )

134
Q

These are containers for storing data values

A

variables

135
Q

Python has ___ in declaring a variable

A

no command

136
Q

The ______ is used to output a variable

A

print statement

137
Q

Different data types categories in python

A

Text type: str
Numeric type: int, float, complex
Sequence type: list, tuple, range
Mapping type: dict
Set Types: set, frozenset
Boolean type: bool
Binary types: bytes, bytearray, memoryview

138
Q

used to get the datatype of any object

A

type () function

139
Q

what type of data type is this:
x = ‘Hello’

A

str

140
Q

what type of data type is this:
x = 20

A

int

141
Q

what type of data type is this:
x = 20.5

A

float

142
Q

what type of data type is this:
x = 2j

A

complex

143
Q

what type of data type is this:
x = [20.5, “apple”, banna”]

A

list

144
Q

what type of data type is this:
x = (20.5, “apple”, banna”)

A

tuple

145
Q

what type of data type is this:
x = {20.5, “apple”, banna”}

A

set

146
Q

what type of data type is this:
x = {name : Sem, age : 10}

A

dict

147
Q

what type of data type is this:
x = range (2, 35, 2)

A

range

148
Q

what type of data type is this:
x = frozenset({0.5, “apple”, banna”})

A

frozenset

149
Q

what type of data type is this:
x = True

A

bool

150
Q

what type of data type is this:
x = b”Hello”

A

bytes

151
Q

what type of data type is this:
x = bytearray(5)

A

bytearray

152
Q

what are the string methods in Python

A

String Literals (“Hello”)
Slice ( a[2:7] )
String Length ( len(a) )
Strip ( a.strip() )
Lower ( a.lower() )
Upper ( a.upper() )
Replace ( a.replace(“A”, “B”) )

153
Q

a symbol responsible for a particular operation between two operands

A

Operator(s)

153
Q

It is the pillar of a program on which the logic is built

A

Operators

154
Q

types of Operators

A

Arithmetic operators
Comparison operators
Assignment operators
Logical operators
Bitwise operators
Membership operators
Identity operators

155
Q

The keyword used in Python to define a block of code that executes when none of the preceding conditions are true

A

else keyword

156
Q

data type that allows duplicates and uses parentheses in its declaration

A

tuple

157
Q

keyword used to prematurely exit a loop

A

break keyword

158
Q

method used to add an element to the end of a python list

A

append()

159
Q

method used to remove and return last element from a list in python

A

pop() method

160
Q

built-in python method used to create empty dictionary

A

dict() method

161
Q

statement used to check multiple conditions after an if statement fails

A

elif statements

162
Q

type of loop where one loop is placed inside another loop

A

nested loop

163
Q

collection that is ordered & changeable, allowing duplicate members

A

list

164
Q

statement that allows you to skip the current iteration and continue with the next one in python loops

A

continue statements

165
Q

looping statements that runs a block of code as long a specified condition is true

A

while loop statements

166
Q

collection in python where data is stored in key-value pairs

A

dictionary

167
Q

function used in python to generate a sequence of numbers often used in for loops

A

range() function

168
Q

collection in python that is unordered unindexed and does not allow duplicate

A

set