DSA Flashcards

midterms (170 cards)

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
this operator concatinates the collection mentioned on either side of the operator
concatenation tuple1 + tuple2
25
operator that returns true if particular item exist in collection, otherwise false
Membership 2 in tuple1 #true or false
26
used to get the length of collection
length len(tuple1)
27
collection of unordered, unindexed, unchangeable, items that do not allow duplicate values
Set
28
The set can be created by enclosing the comma-separated immutable items with the ____
curly braces { }
29
the ____ can be used to create the set by the passed sequence
set() method set4 = set()
30
Empty curly braces will create a what? ex: set3 = {}
dictionary
31
how to access items in a set?
using a for loop (for x in thisset: ) or using the in keyword (print("banana" in thisset) )
32
how to add elements to set
add() method set.add("element")
33
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"])
34
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
35
The _____ empties the set:
clear() method thisset.clear()
36
The _____ will delete the set completely
del keyword del thisset
37
The union of two sets is calculated by using the ____
using pipe (|) operator: print(Days1|Days2) using union() method: print(Days1.union(Days2))
38
The intersection of two sets can be performed by the___ or the ____
& operator: print(Days1&Days2) intersection() function: set1.intersection(set2)
39
The intersection_update() method is different from the intersection() method since it modifies the original set by____
removing the unwanted items
40
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
41
The difference of two sets can be calculated by using the ___ or ___
subtraction (-) operator : print(Days1-Days2) difference() method: Days1.difference(Days2)
42
The symmetric dfference of two sets is calculated by_____ or ____.
^ operator : c = a^b symmetric_difference() method: c = a.symmetric_difference(b)
43
it removes that element which is present in both sets.
Symmetric difference of sets
44
The ____ is given as the set of the elements that common in both sets
intersection of the two sets
45
is a collection which is unordered, changeable and indexed.
dictionary
46
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.
47
format of a dictionary
dict = {key:value, key:value}
48
how to call dictionary values
Employee.get("Age") or print("Age : %d"%Employee["Age"])
49
Adding elements to dictionary one at a time
Dict[0] = 'Peter'
50
The items of the dictionary can be deleted by using the ___
del keyword
51
difference between pop() and popitem()
dict.pop(key) #removes a specific key dict.popitem() #removes last
52
When looping through a dictionary, the return value are the __ of the dictionary
keys
53
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)
54
properties of dict Keys
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
The ____ statement is used to test as specific condition if it is true
if statement if condition :
56
The ____statement is used to test as specific condition if it is false
else statement else :
57
it enables us to use if else statements inside an outer if statement
nested if statement if condition : if condition: else: else
58
in python, ____ is used to declare a block
indentation
59
typical number of spaces for indention
4 spaces
60
if an 'if clause' consist of only 1 line, it may go to the ____ as the as the header statement
same line
61
"if the previous conditions were not true, then try this condition"
elif statement
62
it enables us to alter the code to repeat in finite number of times
loop / looping
63
advantages of looping statements
code re-usability prevent redundancy we can traverse over the elements of data structures
64
used when the number of iterations isnt known
while loop
65
the block of statement is executed until condition specified is satisfied
while loop
66
other term for while loop
pre-tested loop
67
used to execute a part of code until given condition is satisfied. Better to use if iteration is known in advance
for loop
68
other term for for loop
per-tested loop
69
this statement continues until condition is satisfied
do-while loop
70
used when executing the loop at least once is necessary (mostly menu driven programs)
do-while loop
71
other term for do - while loop
post tested loop
72
one or more loop inside a loop
nested loop
73
while loop syntax
while condition: task
74
it stops the loop even if while condition is true
break
75
it could stop the current iteration and continue with the next
continue
76
run a block of code once condition is no longer true
else ex: while condition: task else : task
77
it is the variable that takes the value of item inside the sequence on each iteration
iterating_var
78
used to iterate over a sequence (list, tuple, string)
for loop
79
iterating over a sequence is called_____
traversal
80
it generate a sequence of numbers
range() function
81
what is the output? print (list(range (2, 20, 3)))
[2, 5, 8, 11, 14, 17]
82
what is the output? print (range (2, 20))
range (2, 20)
83
what is the output? print (list(range (2, 5)))
[2, 3, 4]
84
what is the output? g = [1, 2, 3, 4, 5] for i in range(len(g)): print (" i skip from", g[i])
i skip from 1 i skip from 2 i skip from 3 i skip from 4 i skip from 5
85
heavly used in list of list
nested loops
86
All syntax in list
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
all syntax in tuple
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
all syntax in set
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
All syntax in dictionary
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
what is python
GDHI: General Purpose, Dynamic, High-Level, and Interpreted programming language
91
T or F Python provides high level data structures
True
92
T or F Python does not support Object Oriented Programming
False
93
T or F Python is hard and complicated to learn
False it is simple and easy to learn
94
Python is ___ than Java
3-5 times shorter
95
Python's run time ___ than Java
works harder
96
T or F Components can be developed in Java and combined to form applications in Python
True
97
who developed Python
Guido van Rossum
98
where is the name Python based from?
BBC comedy series: Monty Python's Flying Circus
99
when was the BBC series aired on?
1970s
100
qualities that Van Rossum wanted in the name
unique, sort, a little bit mysterious
101
Python can be used to _____ into Java implementation
prototype components
102
Python is often ____ shorter than C++
5-10 times
103
python is a _____ language
glue
104
T or F python is used to combine components written in Javascript
False (C++)
105
T or F you can run Python in a GUI environment
True
106
Python is known for its ____ nature that makes it applicable in almost every domain of ____ development
general-purpose, software
107
Python is the _____ programming language and can develop any application
fastest-growing
108
enumerate all the Python applications
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
3 ways to run a python program
Interactive Interpreter Prompt (IIP) Script File (SF) Integrated Development Environment (IDE)
110
execute the python statement one by one
Interactive Interpreter Prompt (IIP)
111
used when we are concerned with the output of each line
Interactive Interpreter Prompt (IIP)
112
steps in opening the Interpreter mode
1.) open terminal (command Prompt) 2.) type python 3.) start coding line by line
113
it writes multiple lines of code into a file which can be executed later
Script file
114
steps in Script mode programming
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
first unix IDE for Python
IDLE
116
first window interface for python which is an IDE with a ____
PythonWin, GUI
117
Macintosh version of python
IDLE IDE and Python (downloadable as MacBinary and BinHex'd file)
118
a name used to identify a variable, function, class module, or other object
Python Identifier
119
what do identifiers start?
A-Z a-z _ (underscore)
120
Class Identifiers
uppercase letters (A-Z)
121
other identifiers
lowercase (a-z)
122
What is not allowed in Identifiers
Starting with numbers Special characters as variable names ($, @, %)
123
how to signify that an identifier is private
single leading underscore (_)
124
Python reserved keywords
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
how are blocks of code denoted in Python
line indention/ indention
126
line continuation character
\
127
it allows the line to continue even if they are not on the same line
line continuation character
128
What statements do not need the use of line continuation character
statements with [], {}, or ()
129
what is the use of a single and double quote (' and ")
used to denote string literals
130
what is the use of a triple quote ( ''' )
span the string across multiple lines
131
used to make a next line
\n
132
It signifies a comment
Hash sign (#)
133
It signifies a multiline comment
triple quote ( ''' )
134
These are containers for storing data values
variables
135
Python has ___ in declaring a variable
no command
136
The ______ is used to output a variable
print statement
137
Different data types categories in python
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
used to get the datatype of any object
type () function
139
what type of data type is this: x = 'Hello'
str
140
what type of data type is this: x = 20
int
141
what type of data type is this: x = 20.5
float
142
what type of data type is this: x = 2j
complex
143
what type of data type is this: x = [20.5, "apple", banna"]
list
144
what type of data type is this: x = (20.5, "apple", banna")
tuple
145
what type of data type is this: x = {20.5, "apple", banna"}
set
146
what type of data type is this: x = {name : Sem, age : 10}
dict
147
what type of data type is this: x = range (2, 35, 2)
range
148
what type of data type is this: x = frozenset({0.5, "apple", banna"})
frozenset
149
what type of data type is this: x = True
bool
150
what type of data type is this: x = b"Hello"
bytes
151
what type of data type is this: x = bytearray(5)
bytearray
152
what are the string methods in Python
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
a symbol responsible for a particular operation between two operands
Operator(s)
153
It is the pillar of a program on which the logic is built
Operators
154
types of Operators
Arithmetic operators Comparison operators Assignment operators Logical operators Bitwise operators Membership operators Identity operators
155
The keyword used in Python to define a block of code that executes when none of the preceding conditions are true
else keyword
156
data type that allows duplicates and uses parentheses in its declaration
tuple
157
keyword used to prematurely exit a loop
break keyword
158
method used to add an element to the end of a python list
append()
159
method used to remove and return last element from a list in python
pop() method
160
built-in python method used to create empty dictionary
dict() method
161
statement used to check multiple conditions after an if statement fails
elif statements
162
type of loop where one loop is placed inside another loop
nested loop
163
collection that is ordered & changeable, allowing duplicate members
list
164
statement that allows you to skip the current iteration and continue with the next one in python loops
continue statements
165
looping statements that runs a block of code as long a specified condition is true
while loop statements
166
collection in python where data is stored in key-value pairs
dictionary
167
function used in python to generate a sequence of numbers often used in for loops
range() function
168
collection in python that is unordered unindexed and does not allow duplicate
set