python-3.x interview questions
Top python-3.x frequently asked interview questions
Why does Python 3 allow "00" as a literal for 0 but not allow "01" as a literal for 1? Is there a good reason? This inconsistency baffles me. (And we're talking about Python 3, which purposely broke backward compatibility in order to achieve goals like consistency.)
For example:
>>> from datetime import time
>>> time(16, 00)
datetime.time(16, 0)
>>> time(16, 01)
File "<stdin>", line 1
time(16, 01)
^
SyntaxError: invalid token
>>>
Source: (StackOverflow)
I have both python2.7
and python3.2
installed in Ubuntu 12.04
.
The symbolic link python
links to python2.7
.
When I type:
sudo pip install package-name
It will default install python2
version of package-name
.
Some package supports both python2
and python3
.
How to install python3
version of package-name
via pip
?
Source: (StackOverflow)
I'm a new Python programmer who is making the leap from 2.6.4 to 3.1.1. Everything has gone fine until I tried to use the 'else if' statement. The interpreter gives me a syntax error after the 'if' in 'else if' for a reason I can't seem to figure out.
def function(a):
if a == '1':
print ('1a')
else if a == '2'
print ('2a')
else print ('3a')
function(input('input:'))
I'm probably missing something very simple; however, I haven't been able to find the answer on my own.
Source: (StackOverflow)
I am still quite new to programming, and I was googling this question and I do not seem to get a straight answer.
I googled what a cache was and I understand it is an encrypted file of similar files (a bit of a convoluted paraphrase but I think some will agree it is accurate).
What, as a programmer, do we do with the folder __pycache__
? Is it what we give people instead of our source code? Is it just data I have input? This folder keeps getting created and I am wondering what it is for...
Source: (StackOverflow)
I'm using this code to get standard output from an external program:
>>> from subprocess import *
>>> command_stdout = Popen(['ls', '-l'], stdout=PIPE).communicate()[0]
The communicate() method returns an array of bytes:
>>> command_stdout
b'total 0\n-rw-rw-r-- 1 thomas thomas 0 Mar 3 07:03 file1\n-rw-rw-r-- 1 thomas thomas 0 Mar 3 07:03 file2\n'
However, I'd like to work with the output as a normal Python string. So that I could print it like this:
>>> print(command_stdout)
-rw-rw-r-- 1 thomas thomas 0 Mar 3 07:03 file1
-rw-rw-r-- 1 thomas thomas 0 Mar 3 07:03 file2
I thought that's what the binascii.b2a_qp() method is for, but when I tried it, I got the same byte array again:
>>> binascii.b2a_qp(command_stdout)
b'total 0\n-rw-rw-r-- 1 thomas thomas 0 Mar 3 07:03 file1\n-rw-rw-r-- 1 thomas thomas 0 Mar 3 07:03 file2\n'
Does anybody know how to convert the bytes value back to string? I mean, using the "batteries" instead of doing it manually. And I'd like it to be ok with Python 3.
Source: (StackOverflow)
I'm mainly a C# developer, but I'm currently working on a project in Python.
How can I represent the equivalent of an Enum in Python?
Source: (StackOverflow)
>>> timeit.timeit("'x' in ('x',)")
0.04869917374131205
>>> timeit.timeit("'x' == 'x'")
0.06144205736110564
Also works for multiple options, both seem to grow linearly:
>>> timeit.timeit("'x' in ('x', 'y')")
0.04866674801541748
>>> timeit.timeit("'x' == 'x' or 'x' == 'y'")
0.06565782838087131
>>> timeit.timeit("'x' in ('y', 'x')")
0.08975995576448526
>>> timeit.timeit("'x' == 'y' or 'x' == 'y'")
0.12992391047427532
Based on this, I think I should totally start using in
everywhere instead of ==
!
Source: (StackOverflow)
Can someone please explain this to me? This doesn't make any sense to me.
I copy a dictionary into another and edit the second and both are changed. Why is this happening?
>>> dict1 = {"key1": "value1", "key2": "value2"}
>>> dict2 = dict1
>>> dict2
{'key2': 'value2', 'key1': 'value1'}
>>> dict2["key2"] = "WHY?!"
>>> dict1
{'key2': 'WHY?!', 'key1': 'value1'}
Source: (StackOverflow)
I want to import a function from another file in the same directory.
Sometimes it works for me with from .mymodule import myfunction
but sometimes I get a
SystemError: Parent module '' not loaded, cannot perform relative import
Sometimes it works with from mymodule import myfunction
, but sometimes I also get a
SystemError: Parent module '' not loaded, cannot perform relative import
I don't understand the logic here, and I couldn't find any explanation. This looks completely random.
Could someone explain to me what's the logic behind all this?
Source: (StackOverflow)
I want to install pip. It should support Python 3, but it requires setuptools, which is available only for Python 2.
How can I install pip with Python 3?
Source: (StackOverflow)
Why do I receive a syntax error when printing a string in Python 3?
>>> print "hello World"
File "<stdin>", line 1
print "hello World"
^
SyntaxError: invalid syntax
Source: (StackOverflow)
It seems they canceled in Python 3.0 all the easy way to quickly load a script file - both execfile() and reload().
Is there an obvious alternative I'm missing?
Source: (StackOverflow)
Using Python 3.x, I have a list of strings for which I would like to perform a natural alphabetical sort.
Natural sort: The order by which files in Windows are sorted.
For instance, the following list is naturally sorted (what I want):
['elm0', 'elm1', 'Elm2', 'elm9', 'elm10', 'Elm11', 'Elm12', 'elm13']
And here's the "sorted" version of the above list (what I have):
['Elm11', 'Elm12', 'Elm2', 'elm0', 'elm1', 'elm10', 'elm13', 'elm9']
I'm looking for a sort function which behaves like the first one.
Source: (StackOverflow)
Should I put the shebang in my Python scripts? In what form?
#!/usr/bin/env python
or
#!/usr/local/bin/python
Are these equally portable? Which form is used most?
Note: the tornado project uses the shebang. On the other hand the Django project doesn't.
Source: (StackOverflow)