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
1
vote
2 answers

Python Tkinter: Why use Tkinter.W not str "w"

I'm learning to use Tkinter and in the tutorials it tells me to import W from Tkinter, but W is just a str ("w"). My question is why use Tkinter.W not "w". Is it because Tkinter will sometimes have the var equal something else dependent upon…
Lupus Ossorum
  • 450
  • 3
  • 16
1
vote
1 answer

Python 3.4 -> Connect to PostGres SQL using SQLAlchemy win32

I am using Postgres and Pandas and having trouble with connecting the two. I have created the enginestr using SQLAlchemy as below from sqlalchemy import create_engine engine =…
1
vote
1 answer

How to use the __subclasscheck__ magic method?

How can we make a class who lies about who he has subclassed? After reading the doc I've attempted this: >>> class AllYourBase(type): ... @classmethod ... def __subclasscheck__(cls, other): ... return True ... >>> class…
wim
  • 338,267
  • 99
  • 616
  • 750
1
vote
1 answer

Arbitrary string to valid Python name

I'm trying to autogenerate Python code based on external data. Task is to convert arbitrary string to valid Python name. I've come up with compact solution which seems to be technically correct: import string VALID_NAME_CHARACTERS =…
Łukasz Rogalski
  • 22,092
  • 8
  • 59
  • 93
1
vote
1 answer

UltimateListCtrl - selections and sorting

i wrote a little python program with wxpython and ulc for a personal database. I thought i was finished and finally imported all the data from an original access database an now was faced with a problem. Having nearly 1000 entries in the ulc…
Sonic
  • 117
  • 1
  • 10
1
vote
3 answers

StopIteration after defining xrange

I wrote the following code to define blocks of 4 lines in a text file and output the block if the 2nd line of the block is composed of only one type of character. It is assumed (and previously verified) that the 2nd line is always composed of a…
biohazard
  • 2,017
  • 10
  • 28
  • 41
1
vote
1 answer

ungrab_pointer() in Xlib doesn't exist but ungrab_button won't release the mouse

I can't find a way to unregister my grab_pointercall with python-xlib. If you don't un-grab you won't be able to click inside windows which is a annoying issue if you're used to it. Aside from the code being ugly, the relevant parts are thse: try: …
Torxed
  • 22,866
  • 14
  • 82
  • 131
1
vote
1 answer

Random list from list with no repetition

I created a random list from a source list, the problem is that it has an item that is repeated twice. I would like to have no repetition in my list. Here are my code and the output. COLOR_INDEX = ['FF000000', 'FFFF0000', 'FF00FF00', 'FF0000FF', …
Julia_arch
  • 376
  • 2
  • 4
  • 15
1
vote
1 answer

Python difflib: not detecting changes

import difflib test1 = ")\n )" test2 = "#)\n #)" d = difflib.Differ() diff = d.compare(test1.splitlines(), test2.splitlines()) print "\n".join(diff) OUTPUT: - ) + #) - ) + #) ? + as you can see, it didnt detect the change for the first line (…
ealeon
  • 12,074
  • 24
  • 92
  • 173
1
vote
2 answers

Break from array and run another function in python

My code: def exit(): return "Exit" arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] def func(): for element in arr: print element I want to run exit() after every 3 elements of the array and then resume from the last element.
Sanidhay
  • 159
  • 12
1
vote
12 answers

Extracting data from dictionary returned by json.loads

Here is what I am trying to do: Prompt for an URL, read the JSON data from that URL using urllib and then parse and extract the comment counts from the JSON data and compute their sum. Here is what I have so far in Python: import json import…
cybernerd
  • 71
  • 1
  • 1
  • 11
1
vote
1 answer

How do you temporary run your code as 'root'?

RELATED: Python multiprocessing: Permission denied I want to use Python's multiprocessing.Pool import multiprocessing as mp pool = mp.Pool(3) for i in range(num_to_run): pool.apply_async(popen_wrapper, args=(i,), callback=log_result) I get…
ealeon
  • 12,074
  • 24
  • 92
  • 173
1
vote
1 answer

Comparing two sets of numbers

I am trying to compare two sets for matching numbers. The once a number has been found append the username associated with the numbers to a dictionary along with the amount of numbers they have matched. My explanation is not the best but perhaps my…
Joshua
  • 2,979
  • 1
  • 14
  • 20
1
vote
3 answers

Integer division to next integer

As you may know, if you do: >>> 11/2 # 5 And >>> 11/2.0 # 5.5 I'd like to get 6 in this case. I tried with: >>> 11//2 # 5 And >>> 11//2.0 # 5.0 The last one gives the prev integer. I'd like to get the next integer. Even is the result is like x.1…
Gocht
  • 9,924
  • 3
  • 42
  • 81
1
vote
4 answers

calculating factorial in Python

When calculating math.factorial(100) I get: 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000L Why is there an L at the end of the number?
AlexBrand
  • 11,971
  • 20
  • 87
  • 132
1 2 3
99
100