Hello! I recently decided to start studying the Python language, however, when researching the language, I noticed that there are differences between version 2 and 3. Are these differences really significant?
Hello! I recently decided to start studying the Python language, however, when researching the language, I noticed that there are differences between version 2 and 3. Are these differences really significant?
Are these differences really significant?
From the point of view of syntax, of learning, etc., no, they are not. You can perfectly learn the "Python 2 way" and the "Python 3 way" at the same time, and even use it now and then, depending on the situation (I speak of small scripts only, and you'll soon understand why). >
But from the standpoint of maintainability, yes, these differences can be a big stone in your shoe if you start a project in one version and then decide to switch to another. For that reason, if you are starting now, and especially if you want to make some system with a longer lifecycle, I would recommend the following:
print
as a function, unicode strings without u
at the front, integer division resulting in floating point, etc. Know the differences in Python 2 to know better recognize discrepancies and fix bugs, but do not get used to doing something that has 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, there is usually no problem: this is what the __future__
module exists for. The main differences between the versions (these 3 that I mentioned and a 4th that I do not know) were "backported" pro Python 2.7, 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, as this portability guide mentions, invoking Python with flag -3
causes it to warn you of potential incompatibilities between the two versions;
If you have no reason to program in 2 (eg, some fundamental library for your project that only exists in pro 2), prefer 3. Some operating systems come with 2 installed by default (meaning 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 tell you what all these differences are, but among the main according to the documentation the ones that I find most shocking are the following:
print
no 3 is a function, not a statement:
print 42 # Python 2
print(42) # Python 3
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 # 3 are Unicode by default, and byte sequences 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 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)
Not so much. If there are no requirements to use version 2.7, because it will work with some legacy system, it's pretty obvious you should learn about the newer version, in the case of 3.5.1, it's 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 .