Professional Documents
Culture Documents
Python
Python
Q30 How will you efficiently load data from a text file?
Q51
Q52
Q53
Q54
Q55
Q56
Q57
Q58
Q59
Q60
Q61
Q62
Q63
Q64
Q65
Q66
Q67
Q68
Q69
Q70
Q71
Q72
Q73
Q74
Q75
Q76
Q77
Q78
Q79
Q80
Q81
Q82
Q83
Q84
Q85
Q86
Q87
Q88
Q89
Q90
Q91
Q92
Q93
Q94
Q95
Q96
Q97
Q98
Q99
Q100
Q101
Q102
Q103
Q104
Q105
Q106
Q107
Q108
Q109
Q110
Q111
Q112
Q113
Q114
Q115
Q116
Q117
Q118
Q119
Q120
Q121
Q122
Q123
Q124
Q125
Q126
Q127
Q128
Q129
Q130
Q131
Q132
Q133
Q134
Q135
Q136
Q137
Q138
Q139
Q140
Q141
Q142
Q143
Q144
Q145
Q146
Q147
Q148
Q149
Q150
Q151
Q152
Q153
Q154
Q155
Q156
Q157
Q158
Q159
Q160
Q161
Q162
Q163
Q164
Q165
Q166
Q167
Q168
Q169
Q170
Q171
Q172
Q173
Q174
Q175
Q176
Q177
Q178
Q179
Q180
Q181
Q182
Q183
Q184
Q185
Q186
Q187
Q188
Q189
Q190
Q191
Q192
Q193
Q194
Q195
Q196
Q197
Q198
Q199
Q200
Q201
Q202
Q203
Q204
Q205
Q206
Q207
Q208
Q209
Q210
Q211
Q212
Q213
Q214
Q215
Q216
Q217
Q218
Q219
Q220
Q221
Q222
Q223
Q224
Q225
Q226
Q227
Q228
Q229
Q230
Q231
Q232
Q233
Q234
Q235
Q236
Q237
Q238
Q239
Q240
Q241
Q242
Q243
Q244
Q245
Q246
Q247
Q248
Q249
Q250
Q251
Q252
Q253
Q254
Q255
Q256
Q257
Q258
Q259
Q260
Q261
Q262
Q263
Q264
Q265
Q266
Q267
Q268
Q269
Q270
Q271
Q272
Q273
Q274
Q275
Q276
Q277
Q278
Q279
Q280
Q281
Q282
Q283
Q284
Q285
Q286
Q287
Q288
Q289
Q290
Q291
Q292
Q293
Q294
Q295
Q296
Q297
Q298
Q299
Q300
Q301
Q302
Q303
Q304
Q305
Q306
Q307
Q308
Q309
Q310
Q311
Q312
Q313
Q314
Q315
Q316
Q317
Q318
Q319
Q320
Q321
Q322
Q323
Q324
Q325
Q326
Q327
Q328
Q329
Q330
Q331
Q332
Q333
Q334
Q335
Q336
Q337
Q338
Q339
Q340
Q341
Q342
Q343
Q344
Q345
Q346
Q347
Q348
Q349
Q350
Q351
Q352
Q353
Q354
Q355
Q356
Q357
Q358
Q359
Q360
Q361
Q362
Q363
Q364
Q365
Q366
Q367
Q368
Q369
Q370
Q371
Q372
Q373
Q374
Q375
Q376
Q377
Q378
Q379
Q380
Q381
Q382
Q383
Q384
Q385
Q386
Q387
Q388
Q389
Q390
Q391
Q392
Q393
Q394
Q395
Q396
Q397
Q398
Q399
Q400
Q401
Q402
Q403
Q404
Q405
Q406
Q407
Q408
Q409
Q410
Q411
Q412
Q413
Q414
Q415
Q416
Q417
Q418
Q419
Type of Question
Coding
Coding
Syntax
Syntax
Coding
Theory
Theory
Theory
Coding
Coding
Theory
Theory
Syntax
Theory
Theory
Coding
Coding
Coding
Theory
Theory
Coding
Theory
Theory
Coding
Theory
Theory
Syntax
Theory
Theory
Theory
Theory
Syntax
Coding
Coding
Coding
Theory
Theory
Theory
Coding
Theory
Theory
Theory
Theory
Theory
Syntax
Syntax
Theory
Theory
Coding
Syntax
Difficulty Level
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Advanced
Answer
The with statement simplifies exception handling by encapsulating common
preparation and cleanup
If bool(a) returns tasks
False, then in assigned
x is so-called the
context
valuemanagers.
of b.
In
Forshort, it letsthe
instance, youopen
assign values to
statement is aa variable in an outer
context manager in (but
itself,non-global)
which lets
scope.
you open a file, keep it open as long as the execution is in the context of the
The
with nonlocal
statementstatement
where youcauses theand
used it, listed identifiers
close to refer
it as soon as youto leave
previously
the
bound variables in the nearest enclosing scope excluding globals.
context, no matter whether you have left it because of an exception or during
regular control flow.
Objects are data with methods attached, closures are functions with data
attached. The method of binding data to a function without actually passing
them as parameters is called closure.
If you pass a mutable object into a method, the method gets a reference to
that same object and you can mutate it to your heart's delight, but if you
rebind the reference in the method (like b = b + 1), the outer scope will know
nothing about it, and after you're done, the outer reference will still point at
the original object.
NumPy's
So to achieve arrays theare more compact
desired effect your thanbest Python
choicelists is to- areturn
list ofalists tupleas you
describe, in Python,
containing the multiple results: would take at least 20 MB or so, while a NumPy 3D
array with single-precision floats in the cells would fit in 4 MB. Access to
reading and writing items is also faster with NumPy.
has 4 main conventional uses in Python:
Python
The differencedoesn't is allow
mostlymulti-threading
due to "indirectness"in the truest sense of
- a Python listtheis anword.arrayIt has
of a
multi-threading
To hold the
pointers package
result ofobjects,
to Python but if
the lastatexecutedyou want
least 4 bytes to multi-thread
expression(/statement) to
per pointer plus 16 in speed your code
an for even
bytes
up,
the then
interactive
smallest it'sinterpreter
usually
Python not a good
session.
object (4 for idea
This
type topointer,
use it. 4was
precedent for set by the count,
reference standard 4 for
CPython
value - and interpreter,
the memory andallocators
other interpreters
rounds uphaveto followed
16). A NumPy suit array is an
A MonkeyPatch
Python is a piece of Python thecode thatdocumentation
extends or (GIL).
modifies other
For
array
code ofhas
translation
at uniform
runtime
a construct
lookup
valuesin--
(typically
called
i18n the Global
(see
single-precision
at startup).
Interpreter
gettext
numbers Lock
take 4 bytes forThe GIL
example),
each,
makes
as sure
in code like: raise
double-precision that only
ones, one of your 'threads'
forms.ValidationError(_("Please
8 bytes. Lesson flexible,can execute
but you at
enter any one
a correct for
pay substantially time. A
It is
thread often
username")) used
acquires to replace
the GIL, a
does method
a little the
work, module
then or
passes class
the level
GIL with
onto a
the
the
customflexibility of standard Python lists.
implementation.
nexta thread.
As general This
purpose happens very quickly
"throwaway" so toname
variable the human eye itthat
to indicate maypart seemof alike
your
function
NumPy threadsresult
is common areis
not just moreexecuting
being in
deliberately
efficient; parallel, but
ignored
it is also they are really
(Conceptually,
more convenient. just
it taking
is
You being
get turns
a lot ofor
The
using most
the
discarded.), same as in CPU usecase
code core.
like: Allis this
label,adding apassing
GILwhich
has_label,workaround
_= adds for a bug
overhead
text.partition(':'). intoa module
execution.
vector
Generator
class and
when matrix
functions
you operations
are
can'tdefinition
replace the for
functions free,
that
original can sometimes
suspend
code. In this their allows
execution
case where one
you replace to avoid
after the
As part
unnecessary
returningcode of a function
a value,work. And
in order they are(using
to resume also either def
efficiently
it atwith
some or lambda),
implemented.
later time and return the another
"wrong"
signature through monkey patching an implementation inside your
value.
own Thisisisfixed
module/package. made (e.g. by a callback
possible by the yield or parent
keyword, classwhichAPI), you but thisuse particular
in place of
function
return. implementation
The most common doesn't needfunction
all of the parameters, as in code like:
Pickling
callback is
= just
lambda the Python
_: True way of saying serializing. Pickling lets youis the
generator you have worked with
range.
serialize Here is one into
an object wayaofstringimplementing
(or anything it (only
elseworks with positive
you choose) in order step,
to beI
will leave it as an exercise to make one
persisted on storage or sent over network. Unpickling is the process of that supports negative steps):
These
restoring deal theclosely
originalwith unpacking.
object from a pickledIf you put *args in function’s parameter list, all
string.
unnamed
Yes, it isarguments
valid. By will be stored
default, allinthe
the args array. **kwargs works
functions in Python the same
return way, but for
the particular
Triple quotes ‘’” or ‘“ are string delimiters that can span
named parameters: multiple lines in
value
Python. Triple quotes are usually used when spanning multiple lines,aor
“None” unless an explicit return statement is present to return value
that’s not “None”.
enclosing a string that has a mix of single and double quotes contained
therein.
Like 2D plotting, 3D graphics is beyond the scope of NumPy and SciPy, but
just as in the 2D case, packages exist that integrate with NumPy. Matplotlib
provides basic 3D plotting in the mplot3d subpackage, whereas Mayavi
provides a wide range of high-quality 3D visualization features, utilizing the
powerful VTK engine.
Python’s lists, even though hugely efficient containers capable of a number
of functions, have several limitations when compared to NumPy arrays. It is
not possible to perform vectorised operations which includes element-wise
addition and multiplication.
They also require that Python store the type information of every element
since they support objects of different types. This means a type dispatching
code must be executed each time an operation on an element is done. Also,
each iteration would have to undergo type checks and require Python API
bookkeeping resulting in very few operations being carried by C loops.
Sometimes objects within the same scope have the same name but function
differently. In such cases, scope resolution comes into play in Python
automatically. A few examples of such behavior are:
Python modules namely 'math' and 'cmath' have a lot of functions that are
common to both of them - log10(), acos(), exp() etc. To resolve this
ambiguity, it is necessary to prefix them with their respective module, like
math.exp() and cmath.exp().
Consider the code below, an object temp has been initialized to 10 globally
and then to 20 on function call. However, the function call didn't change the
value of the temp globally. Here, we can observe that Python draws a clear
line between global and local variables, treating their namespaces as
separate identities.
Shallow Copy is a bit-wise copy of an object. The copied object created has
an exact copy of the values in the original object. If either of the values is a
reference to other objects, just the reference addresses for the same are
copied.
Deep Copy copies all values recursively from source to target object, i.e. it
even duplicates the objects referenced by the source object.
xrange() and range() are quite similar in terms of functionality. They both
generate a sequence of integers, with the only difference that range() returns
a Python list, whereas, xrange() returns an xrange object.
So how does that make a difference? It sure does, because unlike range(),
xrange() doesn't generate a static list, it creates the value on the go. This
technique is commonly used with an object-type generator and has been
termed as "yielding".
.py files contain the source code of a program. Whereas, .pyc file contains
the bytecode of your program. We get bytecode after compilation of .py file
(source code). .pyc files are not created for all the files that you run. It is only
created for the files that you import.
Before executing a python program python interpreter checks for the
compiled files. If the file is present, the virtual machine executes it. If not
found, it checks for .py file. If found, compiles it to .pyc file and then python
virtual machine executes it.
Having .pyc file saves you the compilation time
Reindexing is the process of conforming a dataframe to a new index with
optional filling logic. If the values are missing in the previous index, then
NaN/NA is placed in the location. A new object is returned unless a new
index is produced that is equivalent to the current one. The copy value is set
to False. This is also used for changing the index of rows and columns in the
dataframe
Yes, they can, but with some bit of help. We need to add the parse_dates
argument while we are reading data from the sources. Consider an example
where we read data from a CSV file, we may encounter different date-time
formats that are not readable by the pandas library. In this case, pandas
provide flexibility to build our custom date parser with the help of lambda
functions
We can use the method numpy.loadtxt() which can automatically read the
file’s header and footer lines and the comments if any.
This method is highly efficient and even if this method feels less efficient,
then the data should be represented in a more efficient format such as CSV
etc. Various alternatives can be considered depending on the version of
NumPy used.
Shallow copy does the task of creating new objects storing references of
original elements. This does not undergo recursion to create copies of
nested objects. It just copies the reference details of nested objects.
Deep copy creates an independent and new copy of an object and even
copies all the nested objects of the original element recursively.
the main is considered as an entry point of execution for a program. But in
python, it is known that the interpreter serially interprets the file line-by-line.
This means that python does not provide main() function explicitly. But this
doesn't mean that we cannot simulate the execution of main. This can be
done by defining user-defined main() function and by using the __name__
property of python file. This __name__ variable is a special built-in variable
that points to the name of the current module.
We can use the StringIO module from the io module to read from the Google
Drive link and then we can use the pandas library using the obtained data
source
Instance variables are variables that are created locally within a class to
refer to an object of the class. A class variable is one that is created globally
in a class and is accessible within all instance of that class.
Class variables are declared with keyword static and Instance variables are
declared without static keyword.
Recompile Python
Link the extension module using g++ (e.g., “g++ -shared -o mymodule.so
mymodule.o”).
import random
list = [2, 18, 8, 4]
print “Prior Shuffling – 0”, list
random.shuffle(list)
print “After Shuffling – 1”, list
random.shuffle(list)
print “After Shuffling – 2”, list
The break statement states that the function of the loop is over and to move
on to the next block of code. For example, when the item being searched is
found, there is no need to keep looping. The break statement comes into
play here and the loop terminates and the execution moves on to the next
section of the code.
Code Example:
try:
….#Any code
except:
finally.
Stage 1: Creating a class where we can override the run method of the
Thread class.
Stage 2: We make a call to start() on the new thread. The thread is taken
forward for scheduling purposes.
Stage 3: Execution takes place wherein the thread starts execution, and it
reaches the running state.
Stage 4: Thread waits until the calls to methods including join() and sleep()
take place.
Stage 5: After the waiting or execution of the thread, the waiting thread is
sent for scheduling.
Stage 6: Running thread is done by executing the terminates and reaches
the dead state.
The sequences in Python are indexed. It consists of positive and negative
numbers. Positive numbers use 0 as the first index, 1 as the second index,
and so on. Hence, any index for a positive number n is n-1.
Unlike positive numbers, index numbering for the negative numbers starts
from -1 and it represents the last index in the sequence. Likewise, -2
represents the penultimate index. These are known as negative indexes.
Negative indexes are used for:
Removing any new-line spaces from the string, thus allowing the string to
except the last character, represented as S[:-1]
Showing the index to representing the string in the correct order
Typically, the given function is the first argument and the iterable is available
as the second argument to a map() function. Several tables are given if the
function takes in more than one argument.
Python modules are files containing Python code. This code can either be
functions classes or variables. A Python module is a .py file containing
executable code.
os
sys
math
random
data time
JSON
The self variable in the init method refers to the newly created object while in
other methods, it refers to the object whose method was called.
1
2
3
4
from random import shuffle
x = ['Keep', 'The', 'Blue', 'Flag', 'Flying', 'High']
shuffle(x)
print(x)
Help() and dir() both functions are accessible from the Python interpreter and
used for viewing a consolidated dump of built-in functions.