Questions tagged [python-2.x]

For questions about Python programming that are specific to version 2.x of the language. Use the more generic [python] tag for all Python questions, and only add this tag if your question is version-specific.

Python 2 is the version of the Python programming language which was for a long time the most widely deployed in production environments; but it is now in the process of being displaced by Python 3. The two versions of the language are not compatible, though many aspects of the syntax are identical. The latest, and last, released version of Python 2 is Python 2.7.18.

Official support for Python 2 has ended on January 1, 2020.

See also Python2 or Python3.

For information on Python in general, visit the main Python tag wiki.

Tagging recommendation:

Use the tag for all Python related questions. If you believe your question includes issues specific to the incompatible Python 2.x or Python 3.x, in addition to the main tag, use or . If you believe your question may be even more specific, you can include a version specific tag such as .

Please do not mix (or a more specific tag such as and (ditto) unless you are specifically asking a question about an interoperability problem between versions.

Community

Free Python Programming Books

2871 questions
164
votes
3 answers

Python: Using .format() on a Unicode-escaped string

I am using Python 2.6.5. My code requires the use of the "more than or equal to" sign. Here it goes: >>> s = u'\u2265' >>> print s >>> ≥ >>> print "{0}".format(s) Traceback (most recent call last): File "", line 1, in
Kit
  • 30,365
  • 39
  • 105
  • 149
155
votes
22 answers

Is it possible to have multiple statements in a python lambda expression?

I have a list of lists: lst = [[567, 345, 234], [253, 465, 756, 2345], [333, 777, 111, 555]] I want map lst into another list containing only the second smallest number from each sublist. So the result should be [345, 465, 333]. If I were just…
ottodidakt
  • 3,631
  • 4
  • 28
  • 34
155
votes
5 answers

write() versus writelines() and concatenated strings

So I'm learning Python. I am going through the lessons and ran into a problem where I had to condense a great many target.write() into a single write(), while having a "\n" between each user input variable(the object of write()). I came up with: nl…
AbeLinkon
  • 1,675
  • 2
  • 12
  • 9
151
votes
6 answers

Copy file with pathlib in Python

I try to copy a file with pathlib import pathlib import shutil my_file=pathlib.Path('/etc/hosts') to_file=pathlib.Path('/tmp/foo') shutil.copy(my_file, to_file) I get this exception: /home/foo_egs_d/bin/python…
guettli
  • 25,042
  • 81
  • 346
  • 663
143
votes
6 answers

Why does Python print unicode characters when the default encoding is ASCII?

From the Python 2.6 shell: >>> import sys >>> print sys.getdefaultencoding() ascii >>> print u'\xe9' é >>> I expected to have either some gibberish or an Error after the print statement, since the "é" character isn't part of ASCII and I haven't…
Michael Ekoka
  • 19,050
  • 12
  • 78
  • 79
138
votes
13 answers

Why does the division get rounded to an integer?

I was trying to normalize a set of numbers from -100 to 0 to a range of 10-100 and was having problems only to notice that even with no variables at all, this does not evaluate the way I would expect it to: >>> (20-10) / (100-10) 0 Float division…
Adam Nelson
  • 7,932
  • 11
  • 44
  • 64
137
votes
7 answers

raw_input function in Python

What is the raw_input function? Is it a user interface? When do we use it?
Janezcka
  • 1,465
  • 3
  • 12
  • 12
131
votes
7 answers

Why does ENcoding a string result in a DEcoding error (UnicodeDecodeError)?

I'm really confused. I tried to encode but the error said can't decode.... >>> "你好".encode("utf8") Traceback (most recent call last): File "", line 1, in UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0:…
thoslin
  • 6,659
  • 6
  • 27
  • 29
131
votes
6 answers

List comprehension rebinds names even after scope of comprehension. Is this right?

Comprehensions show unusual interactions with scoping. Is this the expected behavior? x = "original value" squares = [x**2 for x in range(5)] print(x) # Prints 4 in Python 2! At the risk of whining, this is a brutal source of errors. As I write…
Jabavu Adams
  • 2,348
  • 3
  • 18
  • 16
127
votes
10 answers

nonlocal keyword in Python 2.x

I'm trying to implement a closure in Python 2.6 and I need to access a nonlocal variable but it seems like this keyword is not available in python 2.x. How should one access nonlocal variables in closures in these versions of python?
adinsa
  • 1,271
  • 2
  • 9
  • 3
126
votes
5 answers

Why does Python give the "wrong" answer for square root? What is integer division in Python 2?

x = 16 sqrt = x**(.5) #returns 4 sqrt = x**(1/2) #returns 1 I know I can import math and use sqrt, but I'm looking for an answer to the above. What is integer division in Python 2? This behavior is fixed in Python 3.
Merlin
  • 24,552
  • 41
  • 131
  • 206
125
votes
13 answers

Printing without newline (print 'a',) prints a space, how to remove?

I have this code: >>> for i in xrange(20): ... print 'a', ... a a a a a a a a a a a a a a a a a a a a I want to output 'a', without ' ' like this: aaaaaaaaaaaaaaaaaaaa Is it possible?
pythonFoo
  • 2,194
  • 3
  • 15
  • 17
122
votes
4 answers

How do you use subprocess.check_output() in Python?

I have found documentation about subprocess.check_output() but I cannot find one with arguments and the documentation is not very in depth. I am using Python 3 (but am trying to run a Python 2 file through Python 3) I am trying to run this…
JOHANNES_NYÅTT
  • 3,215
  • 7
  • 20
  • 24
117
votes
11 answers

How to add an element to the beginning of an OrderedDict?

I have this: d1 = OrderedDict([('a', '1'), ('b', '2')]) If I do this: d1.update({'c':'3'}) Then I get this: OrderedDict([('a', '1'), ('b', '2'), ('c', '3')]) but I want this: [('c', '3'), ('a', '1'), ('b', '2')] without creating new dictionary.
user2392209
  • 1,201
  • 3
  • 10
  • 8
115
votes
5 answers

String formatting in Python 3

I do this in Python 2: "(%d goals, $%d)" % (self.goals, self.penalties) What is the Python 3 version of this? I tried searching for examples online but I kept getting Python 2 versions.
JoseBazBaz
  • 1,445
  • 4
  • 14
  • 23