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
24 answers

Convert bytes to a string in Python 3

I captured the standard output of an external program into a bytes object: >>> from subprocess import * >>> stdout = Popen(['ls', '-l'], stdout=PIPE).communicate()[0] >>> stdout b'total 0\n-rw-rw-r-- 1 thomas thomas 0 Mar 3 07:03 file1\n-rw-rw-r--…
Tomas Sedovic
  • 42,675
  • 9
  • 40
  • 30
12 answers

Why is "1000000000000000 in range(1000000000000001)" so fast in Python 3?

It is my understanding that the range() function, which is actually an object type in Python 3, generates its contents on the fly, similar to a generator. This being the case, I would have expected the following line to take an inordinate amount of…
  • 43,029
  • 15
  • 76
  • 119
7 answers

What is the Python 3 equivalent of "python -m SimpleHTTPServer"

What is the Python 3 equivalent of python -m SimpleHTTPServer?
  • 16,863
  • 3
  • 17
  • 24
31 answers

Relative imports in Python 3

I want to import a function from another file in the same directory. Usually, one of the following works: from .mymodule import myfunction from mymodule import myfunction ...but the other one gives me one of these errors: ImportError: attempted…
John Smith Optional
  • 22,259
  • 12
  • 43
  • 61
5 answers

Best way to convert string to bytes in Python 3?

TypeError: 'str' does not support the buffer interface suggests two possible methods to convert a string to bytes: b = bytes(mystring, 'utf-8') b = mystring.encode('utf-8') Which method is more Pythonic? See Convert bytes to a string for the…
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
13 answers

How do I return dictionary keys as a list in Python?

With Python 2.7, I can get dictionary keys, values, or items as a list: >>> newdict = {1:0, 2:0, 3:0} >>> newdict.keys() [1, 2, 3] With Python >= 3.3, I get: >>> newdict.keys() dict_keys([1, 2, 3]) How do I get a plain list of keys with Python 3?
15 answers

Should I put #! (shebang) in Python scripts, and what form should it take?

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…
  • 43,129
  • 22
  • 67
  • 91
9 answers

How do I type hint a method with the type of the enclosing class?

I have the following code in Python 3: class Position: def __init__(self, x: int, y: int): self.x = x self.y = y def __add__(self, other: Position) -> Position: return Position(self.x + other.x, self.y +…
Michael van Gerwen
  • 12,011
  • 3
  • 11
  • 8
43 answers

How can I represent an 'Enum' in Python?

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?
John Rutherford
  • 10,704
  • 7
  • 30
  • 32
11 answers

What is __pycache__?

From what I understand, a cache is an encrypted file of similar files. What do we do with the __pycache__ folder? Is it what we give to people instead of our source code? Is it just my input data? This folder keeps getting created, what it is for?
  • 10,577
  • 3
  • 14
  • 6
14 answers

UnicodeDecodeError: 'charmap' codec can't decode byte X in position Y: character maps to

I'm trying to get a Python 3 program to do some manipulations with a text file filled with information. However, when trying to read the file I get the following error: Traceback (most recent call last): File "SCRIPT LOCATION", line NUMBER, in…
Eden Crow
  • 14,684
  • 11
  • 26
  • 24
11 answers

What does -> mean in Python function definitions?

I've recently noticed something interesting when looking at Python 3.3 grammar specification: funcdef: 'def' NAME parameters ['->' test] ':' suite The optional 'arrow' block was absent in Python 2 and I couldn't find any information regarding its…
  • 9,561
  • 3
  • 15
  • 12
8 answers

Fixed digits after decimal with f-strings

Is there an easy way with Python f-strings to fix the number of digits after the decimal point? (Specifically f-strings, not other string formatting options like .format or %) For example, let's say I want to display 2 digits after the decimal…
  • 9,057
  • 3
  • 11
  • 11
11 answers

"TypeError: a bytes-like object is required, not 'str'" when handling file content in Python 3

I've very recently migrated to Python 3.5. This code was working properly in Python 2.7: with open(fname, 'rb') as f: lines = [x.strip() for x in f.readlines()] for line in lines: tmp = line.strip().lower() if 'some-pattern' in tmp:…
  • 9,668
  • 3
  • 23
  • 28
23 answers

Using Python 3 in virtualenv

Using virtualenv, I run my projects with the default version of Python (2.7). On one project, I need to use Python 3.4. I used brew install python3 to install it on my Mac. Now, how do I create a virtualenv that uses the new version? e.g. sudo…
  • 32,405
  • 54
  • 166
  • 302
2 3
99 100