|
Technical Interview Questions
C++ Interview Questions
Php Interview Questions
Xml
Interview Questions
C Interview Questions
.........More
Soft Skills
Communication Skills
Leadership Skills
.........More
|
|
Python Interview Questions and Answers
Is there a scanf() or sscanf() equivalent?
Not as such.
For simple input parsing, the easiest approach is
usually to split the line into whitespace-delimited
words using the split() method of string objects and
then convert decimal strings to numeric values using
int() or float(). split() supports an optional "sep"
parameter which is useful if the line uses something
other than whitespace as a separator.
For more complicated input parsing, regular expressions
more powerful than C's sscanf() and better suited for
the task.
Is there a scanf() or sscanf() equivalent?
Not as such.
For simple input parsing, the easiest approach is
usually to split the line into whitespace-delimited
words using the split() method of string objects and
then convert decimal strings to numeric values using
int() or float(). split() supports an optional "sep"
parameter which is useful if the line uses something
other than whitespace as a separator.
For more complicated input parsing, regular expressions
more powerful than C's sscanf() and better suited for
the task. 1.3.9 What does 'UnicodeError: ASCII [decoding,encoding]
error: ordinal not in range(128)' mean?
This error indicates that your Python installation can
handle only 7-bit ASCII strings. There are a couple ways
to fix or work around the problem.
If your programs must handle data in arbitrary character
set encodings, the environment the application runs in
will generally identify the encoding of the data it is
handing you. You need to convert the input to Unicode
data using that encoding. For example, a program that
handles email or web input will typically find character
set encoding information in Content-Type headers. This
can then be used to properly convert input data to
Unicode. Assuming the string referred to by value is
encoded as UTF-8:
value = unicode(value, "utf-8")
will return a Unicode object. If the data is not
correctly encoded as UTF-8, the above call will raise a
UnicodeError exception.
If you only want strings converted to Unicode which have
non-ASCII data, you can try converting them first
assuming an ASCII encoding, and then generate Unicode
objects if that fails:
try:
x = unicode(value, "ascii")
except UnicodeError:
value = unicode(value, "utf-8")
else:
# value was valid ASCII data
pass
It's possible to set a default encoding in a file called
sitecustomize.py that's part of the Python library.
However, this isn't recommended because changing the
Python-wide default encoding may cause third-party
extension modules to fail.
Note that on Windows, there is an encoding known as "mbcs",
which uses an encoding specific to your current locale.
In many cases, and particularly when working with COM,
this may be an appropriate default encoding to use.
How do I convert between tuples and lists?
The function tuple(seq) converts any sequence (actually,
any iterable) into a tuple with the same items in the
same order.
For example, tuple([1, 2, 3]) yields (1, 2, 3) and
tuple('abc') yields ('a', 'b', 'c'). If the argument is
a tuple, it does not make a copy but returns the same
object, so it is cheap to call tuple() when you aren't
sure that an object is already a tuple.
The function list(seq) converts any sequence or iterable
into a list with the same items in the same order. For
example, list((1, 2, 3)) yields [1, 2, 3] and list('abc')
yields ['a', 'b', 'c']. If the argument is a list, it
makes a copy just like seq[:] would.
What's a negative index?
Python sequences are indexed with positive numbers and
negative numbers. For positive numbers 0 is the first
index 1 is the second index and so forth. For negative
indices -1 is the last index and -2 is the penultimate
(next to last) index and so forth. Think of seq[-n] as
the same as seq[len(seq)-n].
Using negative indices can be very convenient. For
example S[:-1] is all of the string except for its last
character, which is useful for removing the trailing
newline from a string.
How do I iterate over a sequence in reverse order?
If it is a list, the fastest solution is
list.reverse()
try:
for x in list:
"do something with x"
finally:
list.reverse()
This has the disadvantage that while you are in the
loop, the list is temporarily reversed. If you don't
like this, you can make a copy. This appears expensive
but is actually faster than other solutions:
rev = list[:]
rev.reverse()
for x in rev:
<do something with x>
If it's not a list, a more general but slower solution
is:
for i in range(len(sequence)-1, -1, -1):
x = sequence[i]
<do something with x>
A more elegant solution, is to define a class which acts
as a sequence and yields the elements in reverse order
(solution due to Steve Majewski):
class Rev:
def __init__(self, seq):
self.forw = seq
def __len__(self):
return len(self.forw)
def __getitem__(self, i):
return self.forw[-(i + 1)]
You can now simply write:
for x in Rev(list):
<do something with x>
Unfortunately, this solution is slowest of all, due to
the method call overhead.
With Python 2.3, you can use an extended slice syntax:
for x in sequence[::-1]:
<do something with x>
Page Numbers : 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Have a Question ?
post your questions here. It
will be answered as soon as possible.
Check
HTML Interview
Questions for more HTML Interview Questions with Answers
Check
Job Interview Questions
for more Interview Questions with Answers
|