Wednesday, September 15, 2010

python interview questions (1)

   Think in python, a nice online python book.


1. What is Python?
Python is an interpreted, interactive, object-oriented programming language. It incorporates modules, exceptions, dynamic typing, very high level dynamic data types, and classes.


2.  How can I find the methods or attributes of an object?
For an instance x of a user-defined class, dir(x) returns an alphabetized list of the names containing the instance attributes and methods and attributes defined by its class.

3. How do I convert a string to a number?
For integers, use the built-in int() type constructor, e.g. int('144') == 144. Similarly, float() converts to floating-point, e.g. float('144') == 144.0. The number 144 to the string '144', use the built-in function str().

4. What is tuple and list? How to convert between tuple and list?
A tuple is a sequence of values. The values can be any type, and they are indexed by integers, so in that respect tuples are a lot like lists. The important difference is that tuples are immutable.

Like a string, a list is a sequence of values. In a string, the values are characters; in a list, they can be any type. The values in a list are called elements or sometimes items. The list is mutable.

A dictionary is like a list, but more general. In a list, the indices have to be integers; in a dictionary they can be (almost) any type.

The function tuple(seq) converts any sequence (actually, any iterable) into a tuple. The function list(seq) converts any sequence or iterable into a list with the same items in the same order.  

5. What is numpy and scipy?
numpy is a python extension module to support efficient operation on arrays of homogeneous data.


SciPy is a set of Open Source scientific and numeric tools for Python. It currently supports special functions, integration, ordinary differential equation (ODE) solvers, gradient optimization, parallel programming tools, an expression-to-C++ compiler for fast execution, and others.

6.What scientific libraries are available in SciPy?

The full SciPy also has the following modules:
cluster
information theory functions (currently, vq and kmeans)
fftpack
fast Fourier transform module based on fftpack and fftw when available
integrate
numeric integration for bounded and unbounded ranges. ODE solvers.
interpolate
interpolation of values from a sample data set.
io
reading and writing numeric arrays, MATLAB .mat, and Matrix Market .mtx files
lib
access to the BLAS and LAPACK libraries
linalg
linear algebra and BLAS routines based on the ATLAS implementation of LAPACK
maxentropy
Support for fitting maximum entropy models, either discrete or continuous
misc
other routines that don't clearly fit anywhere else. The Python Image Library (PIL) interface is located here.
optimize
constrained and unconstrained optimization methods and root-finding algorithms
signal
signal processing (1-D and 2-D filtering, filter design, LTI systems, etc.)
sparse
Some sparse matrix support. LU factorization and solving Sparse linear systems
special
special function types (bessel, gamma, airy, etc.)
stats
statistical functions (stdev, var, mean, etc.)
weave
compilation of numeric expressions to C++ for fast execution
6. How to load an array from a text file in pyhton?

Use numpy.loadtxt. Even if your text file has header and footer lines or comments, loadtxt can almost certainly read it; it is convenient and efficient.

7. What is matplotlib and pylab?

matplotlib is a python 2D plotting library which produces publication quality figures in a variety of hard-copy formats and interactive environments across platforms.

The package pylab combines matplotlib, scipy with numpy into a single namespace.


    from pylab import *

That gets them NumPy, SciPy, and Matplotlib. A rough equivalent would be:

   from pylab import *
   from numpy import *
   rom scipy import *
plotting commands
FunctionDescription
acorr plot the autocorrelation function
annotate annotate something in the figure
arrow add an arrow to the axes
axes create a new axes
axhline draw a horizontal line across axes
axvline draw a vertical line across axes
axhspan draw a horizontal bar across axes
axvspan draw a vertical bar across axes
axis set or return the current axis limits
barbs a (wind) barb plot
bar make a bar chart
barh a horizontal bar chart
broken_barh a set of horizontal bars with gaps
box set the axes frame on/off state
boxplot make a box and whisker plot
cla clear current axes
clabel label a contour plot
clf clear a figure window
clim adjust the color limits of the current image
close close a figure window
colorbar add a colorbar to the current figure
cohere make a plot of coherence
contour make a contour plot
contourf make a filled contour plot
csd make a plot of cross spectral density
delaxes delete an axes from the current figure
draw Force a redraw of the current figure
errorbar make an errorbar graph
figlegend make legend on the figure rather than the axes
figimage make a figure image
figtext add text in figure coords
figure create or change active figure
fill make filled polygons
fill_between make filled polygons between two curves
findobj recursively find all objects matching some criteria
gca return the current axes
gcf return the current figure
gci get the current image, or None
getp get a graphics property
grid set whether gridding is on
hexbin make a 2D hexagonal binning plot
hist make a histogram
hold set the axes hold state
ioff turn interaction mode off
ion turn interaction mode on
isinteractive return True if interaction mode is on
imread load image file into array
imsave save array as an image file
imshow plot image data
ishold return the hold state of the current axes
legend make an axes legend
locator_params adjust parameters used in locating axis ticks
loglog a log log plot
matshow display a matrix in a new figure preserving aspect
margins set margins used in autoscaling
pcolor make a pseudocolor plot
pcolormesh make a pseudocolor plot using a quadrilateral mesh
pie make a pie chart
plot make a line plot
plot_date plot dates
plotfile plot column data from an ASCII tab/space/comma delimited file
pie pie charts
polar make a polar plot on a PolarAxes
psd make a plot of power spectral density
quiver make a direction field (arrows) plot
rc control the default params
rgrids customize the radial grids and labels for polar
savefig save the current figure
scatter make a scatter plot
setp set a graphics property
semilogx log x axis
semilogy log y axis
show show the figures
specgram a spectrogram plot
spy plot sparsity pattern using markers or image
stem make a stem plot
subplot make a subplot (numrows, numcols, axesnum)
subplots_adjust change the params controlling the subplot positions of current figure
subplot_tool launch the subplot configuration tool
suptitle add a figure title
table add a table to the plot
text add some text at location x,y to the current axes
thetagrids customize the radial theta grids and labels for polar
tick_params control the appearance of ticks and tick labels
ticklabel_format control the format of tick labels
title add a title to the current axes
tricontour make a contour plot on a triangular grid
tricontourf make a filled contour plot on a triangular grid
tripcolor make a pseudocolor plot on a triangular grid
triplot plot a triangular grid
xcorr plot the autocorrelation function of x and y
xlim set/get the xlimits
ylim set/get the ylimits
xticks set/get the xticks
yticks set/get the yticks
xlabel add an xlabel to the current axes
ylabel add a ylabel to the current axes
autumn set the default colormap to autumn
bone set the default colormap to bone
cool set the default colormap to cool
copper set the default colormap to copper
flag set the default colormap to flag
gray set the default colormap to gray
hot set the default colormap to hot
hsv set the default colormap to hsv
jet set the default colormap to jet
pink set the default colormap to pink
prism set the default colormap to prism
spring set the default colormap to spring
summer set the default colormap to summer
winter set the default colormap to winter
spectral set the default colormap to spectral

1 comment:

  1. Hi

    Tks very much for post:

    I like it and hope that you continue posting.

    Let me show other source that may be good for community.

    Source: CVS interview questions

    Best rgs
    David

    ReplyDelete