Questions tagged [python-3.x]

DO NOT USE UNLESS YOUR QUESTION IS FOR PYTHON 3 ONLY. Always use alongside the standard [python] tag.

Python is a widely-used, interpreted, object-oriented, and high-level programming language with dynamic semantics, used for general-purpose programming. It was created by Guido van Rossum, and first released on February 20, 1991. Python 3 is the latest version of the Python programming language, first released on December 3rd, 2008. It features simplifications and improvements to the syntax of the language. Some of these changes are backwards incompatible, and therefore Python 3 has its own tag.

Although Python 3 is now the recommended and supported version of the language, some users still remain on version 2.7 for various reasons. If you start new projects or begin to learn Python, version 3 is now the recommended target under normal circumstances:

Python 3 is strongly recommended for any new development. As of January 2020, Python 2 has reached End Of Life status, meaning it will receive no further updates or bugfixes, including for security issues. Many frameworks and other add on projects are following a similar policy. As such, we can only recommend learning and teaching Python 3.

One of the main differences is in the print statement.

Python 2:

print "Hello World"

Python 3:

print("Hello World")

For more information on the differences, see Porting Python 2 Code to Python 3.

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 may 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.

Python Free Tutorials

Python Online Books

Python API Reference

Python Online IDE

Python Package Index

341062 questions
777
votes
14 answers

What is the best way to remove accents (normalize) in a Python unicode string?

I have a Unicode string in Python, and I would like to remove all the accents (diacritics). I found on the web an elegant way to do this (in Java): convert the Unicode string to its long normalized form (with a separate character for letters and…
MiniQuark
  • 46,633
  • 36
  • 147
  • 183
736
votes
6 answers

Are dictionaries ordered in Python 3.6+?

Dictionaries are insertion ordered as of Python 3.6. It is described as a CPython implementation detail rather than a language feature. The documentation states: dict() now uses a “compact” representation pioneered by PyPy. The memory usage of the…
Chris_Rands
  • 38,994
  • 14
  • 83
  • 119
676
votes
23 answers

How to install pip with Python 3?

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?
deamon
  • 89,107
  • 111
  • 320
  • 448
653
votes
4 answers

How to specify multiple return types using type-hints

I have a function in python that can either return a bool or a list. Is there a way to specify the return types using type hints? For example, is this the correct way to do it? def foo(id) -> list or bool: ...
Yahya Uddin
  • 26,997
  • 35
  • 140
  • 231
644
votes
9 answers

How do I use raw_input in Python 3?

In Python 2: raw_input() In Python 3, I get an error: NameError: name 'raw_input' is not defined
Lonnie Price
  • 10,265
  • 7
  • 24
  • 13
637
votes
11 answers

Getting a map() to return a list in Python 3.x

I'm trying to map a list into hex, and then use the list elsewhere. In python 2.6, this was easy: A: Python 2.6: >>> map(chr, [66, 53, 0, 94]) ['B', '5', '\x00', '^'] However, in Python 3.1, the above returns a map object. B: Python 3.1: >>>…
mozami
  • 7,391
  • 4
  • 21
  • 20
637
votes
9 answers

How to use StringIO in Python3?

I am using Python 3.2.1 and I can't import the StringIO module. I use io.StringIO and it works, but I can't use it with numpy's genfromtxt like this: x="1 3\n 4.5 8" numpy.genfromtxt(io.StringIO(x)) I get the following error: TypeError:…
Babak Abdolahi
  • 6,559
  • 3
  • 15
  • 4
615
votes
7 answers

Error: " 'dict' object has no attribute 'iteritems' "

I'm trying to use NetworkX to read a Shapefile and use the function write_shp() to generate the Shapefiles that will contain the nodes and edges, but when I try to run the code it gives me the following error: Traceback (most recent call last): …
friveraa
  • 6,265
  • 2
  • 12
  • 7
584
votes
4 answers

Deep copy of a dict in python

I would like to make a deep copy of a dict in python. Unfortunately the .deepcopy() method doesn't exist for the dict. How do I do that? >>> my_dict = {'a': [1, 2, 3], 'b': [4, 5, 6]} >>> my_copy = my_dict.deepcopy() Traceback (most recent calll…
Olivier Grégoire
  • 33,839
  • 23
  • 96
  • 137
570
votes
10 answers

Import error: No module name urllib2

Here's my code: import urllib2.request response = urllib2.urlopen("http://www.google.com") html = response.read() print(html) Any help?
delete
525
votes
4 answers

How can I specify the function type in my type hints?

How can I specify the type hint of a variable as a function type? There is no typing.Function, and I could not find anything in the relevant PEP, PEP 483.
Jon
  • 11,356
  • 5
  • 40
  • 74
519
votes
18 answers

List attributes of an object

Is there a way to grab a list of attributes that exist on instances of a class? class new_class(): def __init__(self, number): self.multi = int(number) * 2 self.str = str(number) a = new_class(2) print(',…
MadSc13ntist
  • 19,820
  • 8
  • 25
  • 19
485
votes
19 answers

How to embed image or picture in jupyter notebook, either from a local machine or from a web resource?

I would like to include image in a jupyter notebook. If I did the following, it works : from IPython.display import Image Image("img/picture.png") But I would like to include the images in a markdown cell and the following code gives a 404 error…
Ger
  • 9,076
  • 10
  • 37
  • 48
480
votes
21 answers

How to set Python's default version to 3.x on OS X?

I'm running Mountain Lion and the basic default Python version is 2.7. I downloaded Python 3.3 and want to set it as default. Currently: $ python version 2.7.5 $ python3.3 version 3.3 How do I set it so that every time I run $ python it…
Marcus
  • 9,032
  • 11
  • 45
  • 84
477
votes
5 answers

What does a bare asterisk do in a Python parameter list? (What are "keyword-only" parameters?)

What does a bare asterisk in the parameters of a function do? When I looked at the pickle module, I see this: pickle.dump(obj, file, protocol=None, *, fix_imports=True) I know about a single and double asterisks preceding parameters (for variable…
Eric
  • 5,686
  • 2
  • 23
  • 36