So sánh python 2 vs 3

Python thường được quảng cáo là một trong những ngôn ngữ lập trình tốt nhất cho người mới bắt đầu học, và cú pháp và chức năng đơn giản của nó khiến điều đó trở nên khó tranh luận. Nhưng rất nhiều hướng dẫn vẫn sử dụng Python 2, hiện đã lỗi thời. Python 3 giới thiệu nhiều tính năng mới và điều quan trọng là phải biết về chúng trong tương lai, cũng như sự khác biệt chính giữa Python 3 và người tiền nhiệm của nó.

Python 2 được phát hành lần đầu tiên vào năm 2000. Nó đã cải tiến các phiên bản ngôn ngữ trước đó và giới thiệu các tính năng phổ biến cho các ngôn ngữ lập trình khác như thu gom rác, hiểu danh sách và hỗ trợ Unicode. Phiên bản 2.1 có một vài nâng cấp nhỏ và 2.2 được phát hành vào cuối năm 2001. Phiên bản 2.2 có sự hợp nhất kiểu đặc trưng, ​​hợp nhất các loại và các lớp thành một hệ thống phân cấp duy nhất, biến Python thành ngôn ngữ hướng đối tượng.

Các phiên bản đã phát triển cho đến Python 2.7, là phiên bản chính cuối cùng cho đến Python 3. Python 2 đã chính thức nghỉ hưu, vì vậy hãy tham gia nhóm và di chuyển sang Python 3 càng sớm càng tốt. Và những bạn có ứng dụng Python gốc trên GitHub, hãy đảm bảo cập nhật lên Python 3 để người dùng của bạn không gặp phải bất kỳ trục trặc nào.

Python 2 made code development process easier than earlier versions. It implemented technical details of Python Enhancement Proposal (PEP). Python 2.7 (last version in 2.x ) is no longer under development and in 2020 will be discontinued.

What is Python 3?

Python 3 is a newer version of the Python programming language which was released in December 2008. This version was mainly released to fix problems that exist in Python 2. The nature of these changes is such that Python 3 was incompatible with Python 2. It is backward incompatible.

Some features of Python 3 have been backported to Python 2.x versions to make the migration process easy in Python 3. As a result, for any organization who was using Python 2.x version, migrating their project to 3.x needed lots of changes. These changes not only relate to projects and applications but also all the libraries that form part of the Python ecosystem.

  • Python 3 syntax is simpler and easily understandable whereas Python 2 syntax is comparatively difficult to understand.
  • Python 3 default storing of strings is Unicode whereas Python 2 stores need to define Unicode string value with “u.”
  • Python 3 value of variables never changes whereas in Python 2 value of the global variable will be changed while using it inside for-loop.
  • Python 3 exceptions should be enclosed in parenthesis while Python 2 exceptions should be enclosed in notations.
  • Python 3 rules of ordering comparisons are simplified whereas Python 2 rules of ordering comparison are complex.
  • Python 3 offers Range() function to perform iterations whereas, In Python 2, the xrange() is used for iterations.

Why should you learn Python 2?

So sánh python 2 vs 3
Google Trends Python 2 vs. Python 3

Although, Python 2 is an old open source version here are where you still need to learn Python 2:

  • To become a DevOps engineer and you need to work with configurations management tools like puppet or ansible. Here, you need to work with both of these versions.
  • If your company’s code written in Python 2, you will require to learn to work with that
  • If your development team is working on a project that depends on specific third-party libraries or software which you are not able to port to Python 3, then Python 2 is the only option available for you.

Why should you use Python 3?

So sánh python 2 vs 3
Stack Overflow Questions Python 2 vs. Python 3

Here, are prime reasons for using Python 3.x versions:

  • Python 3 supports modern techniques like AI, machine learning, and data science
  • Python 3 is supported by a large Python developer’s community. Getting support is easy.
  • Its easier to learn Python language compared to earlier versions.
  • Offers Powerful toolkit and libraries
  • Mixable with other languages

History of Python 2

  • Python 2.0 – October 16, 2000
  • Python 2.1 – April 17, 2001
  • Python 2.2 – December 21, 2001
  • Python 2.3 – July 29, 2003
  • Python 2.4 – November 30, 2004
  • Python 2.5 – September 19, 2006
  • Python 2.6 – October 1, 2008
  • Python 2.7-July 3, 2010

History of Python 3

  • Python 3.0 – December 3, 2008
  • Python 3.1 – June 27, 2009
  • Python 3.2 – February 20, 2011
  • Python 3.3 – September 29, 2012
  • Python 3.4-March 16, 2014
  • Python 3.5 – September 13, 2015
  • Python 3.6- October 2016
  • Python 3.7- June 2018.

Key Differences Between Python 2 and Python 3

Here is the main difference between Python 2 and Python 3:

So sánh python 2 vs 3
Difference between Python 2.x and Python 3.x Basis of comparison Python 3 Python 2 Release Date 2008 2000 Function print print (“hello”) print “hello” Division of Integers Whenever two integers are divided, you get a float value When two integers are divided, you always provide integer value. Unicode In Python 3, default storing of strings is Unicode. To store Unicode string value, you require to define them with “u”. Syntax The syntax is simpler and easily understandable. The syntax of Python 2 was comparatively difficult to understand. Rules of ordering Comparisons In this version, Rules of ordering comparisons have been simplified. Rules of ordering comparison are very complex. Iteration The new Range() function introduced to perform iterations. In Python 2, the xrange() is used for iterations. Exceptions It should be enclosed in parenthesis. It should be enclosed in notations. Leak of variables The value of variables never changes. The value of the global variable will change while using it inside for-loop. Backward compatibility Not difficult to port python 2 to python 3 but it is never reliable. Python version 3 is not backwardly compatible with Python 2. Library Many recent developers are creating libraries which you can only use with Python 3. Many older libraries created for Python 2 is not forward-compatible.

Python 2 vs. Python 3 Example Code

Python 3

def main(): print("Hello World!") if name== "main": main()

Python 2

def main(): print "Hello World!" if name== "main": main()

Which Python Version to Use?

When it comes to Python 2 vs Python 3 differences today, Python 3 version is the outright winner. That’s because Python 2 won’t be available after 2020. Mass Python 3 adoption is the clear direction of the future.

After considering declining support for Python 2 programming language and added benefits from upgrades to Python 3, it is always advisable for a new developer to select Python version 3. However, if a job demands Python 2 capabilities, that would be an only compelling reason to use this version.