14
Hello ! I recently decided to start studying the Python language, however when researching the language I realized that there are differences between version 2 and 3. These differences are really significant ?
14
Hello ! I recently decided to start studying the Python language, however when researching the language I realized that there are differences between version 2 and 3. These differences are really significant ?
18
These differences are really significant ?
From the point of view of syntax, learning, etc., they are not. You can perfectly learn the "way of Python 2" and the "way of Python 3" simultaneously, and even use now each other depending on the situation (I speak of small scripts only, and soon you will understand why).
But from a maintainability point of view, yes, these differences can be a big thorn in your shoe if you start a project in one version and then decide to switch to another. For this reason, if you are starting now, and especially if you intend to make some system with longer life cycle, I would recommend the following:
print
as function, Unicode strings without u
in front, entire division resulting in floating point, etc. Know the differences pro Python 2 to know better recognize discrepancies and fix bugs, but don’t get used to doing something that changed from 2 pro 3, even if you are programming in 2;If you need to do something in 2 that was only introduced in 3, in general there is no problem: that’s what the module __future__
exists. The main differences between the versions (those 3 that I mentioned and one more 4th that I do not know) were "backported" Python 2.7 pro, so they can be used as long as you include the following at the beginning of your file:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
In addition, how this portability guide (in English) mentions, invoking Python with the flag -3
causes him to warn you of potential incompatibilities between the two versions;
If you have no reason to program in 2 (e.g., some library that is fundamental to your project that only exists pro 2), prefer 3. Some operating systems come with 2 installed by default (which means more work to properly configure 3)but try not to let it get in the way of using the newer version, if feasible.
That said, I can not say what are all these differences, but among the main according to the documentation the most impactful are the following::
print
paragraph 3 is a function, not an instruction:
print 42 # Python 2
print(42) # Python 3
Various functions built-in and methods of the main types, which previously returned lists, now return iterators. This is not a problem in the most common uses, but if at some point you really need a list, get used to creating one explicitly:
x = list(range(10)) # No Python 2 range já retorna uma lista, você apenas a copia
# No Python 3 range retorna um iterador, é preciso criar a lista
for i in range(10): # Igual em ambos (mais eficiente no 3)
Strings in 3 are Unicode by default, and strings of bytes must be prefixed by b
:
x = "Olá" # No 2, sem o "import unicode_literals", seria inválido
x = u"Olá" # Como era no 2, NÃO RECOMENDADO usar em códigos novos
y = b"\x80" # Só existe a partir do 3; note que 0x80 não é um caractere ASCII válido
Integer divided by integer is integer in 2, but is floating point in 3:
x = 3/2 # 1.5 no Python 3, 1 no Python 2 sem o "import division"
y = 3//2 # 1 em ambos ("//" é a divisão inteira)
1
Not so much. If there are no requirements to use version 2.7, because it will work with some legacy system, it is quite obvious that you should learn on top of the newer version, in the case of 3.5.1, it is far superior. If you ever need to use an older version for any reason, it will be easy to adapt.
There is a official page on the subject.
Browser other questions tagged python python-3.x python-2.7
You are not signed in. Login or sign up in order to post.