1 / 5

Main Differences Python 2 VS Python 3

<br>Letu2019s have a detailed understanding about Python version 2 release dates and Python 3 release dates<br>

Download Presentation

Main Differences Python 2 VS Python 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Main Differences Python 2 VS Python 3 Python Version History Let’s have a detailed understanding about Python version 2 release dates and Python 3 release dates S. No Python 2 Version & Release Date Python 3 Version & Release Date 1 Python 2.0 - released on October 16, 2000 Python 3.0 - released on December 3, 2008 2 Python 2.1 - released on April 17, 2001 Python 3.1 - released on June 27, 2009 3 Python 2.2 - released on December 21, 2001 Python 3.2 - released on February 20, 2011 4 Python 2.3 - released on July 29, 2003 Python 3.3 - released on September 29, 2012 5 Python 2.4 - released on November 30, 2004 Python 3.4 - released on March 16, 2014 6 Python 2.5 - released on September 19, 2006 Python 3.5 - released on September 13, 2015 7 Python 2.6 - released on October 1, 2008 Python 3.6 - released on October 2016 8 Python 2.7 - released on July 3, 2010 Python 3.7 - released on June 2018 Python 2 vs Python 3 Key Comparison Python 2 Python 3 Release Date 2000 2008 Syntax The syntax of Python 2 was difficult to understand The syntax of Python 3 is simpler and easier Print Function print "hello" print ("hello")

  2. When two integers are divided, you always get an integer value. Whenever two integers are divided, you always get a float value Integer Division To store Unicode string value, you would require to define them with "u". Unicode In Python 3, default storing of strings is Unicode. Rules of ordering Comparisons In this version, Rules of ordering comparisons is very easier Rules of ordering comparison are very complex. The new Range () function introduced to perform iterations. Xrange In Python 2, the xrange () is used for iterations. Returning iterable objects instead of lists In Python 3, some function & methods return iterable objects In Python 2, some function & methods return lists Parsing user inputs via input () raw_input () to be used in order to read other data types than String It always stores the user input as Str objects In Python 2, decimals are rounded to the nearest number (No matter be it a Odd or Even number) In Python 3, decimals are rounded to the nearest even number. Rounding Python 2 accepts both notation (Old & New Syntax). Meaning it should be enclosed in notations or parenthesis. Raising Exceptions It should be enclosed in parenthesis. No need to use any keyword Eg: print 'Python', python_version () try: NameError except NameError, err: print err, '--> our Error message' Need to use "as" keyword to handle the exception Eg: print ('Python', python_version ()) try: NameError except NameErroras err: print (err, '--> Error message') Handling Exceptions The value of the global variable will change while using it inside for-loop. Leak of variables The value of variables never changes. Backward compatibility Python version 3 is not backwardly compatible with Python 2. Not difficult to port python 2 to python 3 but it is never reliable. Many older libraries created for Python 2 is not forward-compatible. Many recent developers are creating libraries which you can only use with Python 3. Library Why to Choose Python 3

  3. In Python, Version 3 is the future, here are some of the reason why to choose Python 3 •Legacy Python 2 code will no longer be maintained after 2020 •Upgrade option is available in Python 3 •Easier to write a code •Avoid the syntax confusion •Improved number management helps to perform math calculation •Lot of popular add-ons are supported •Unicode’s are supported Porting Process / Migrating Process – From Python 2 to Python 3 Porting / Migrating the code from Python 2 to Python 3 involves only 3 steps. They are •Auto Conversion •Manual Changes •Runtime Validation & Fix Auto Conversion •Python 3 team has provided a new tool called 2to3tool where it reads the Python 2.x source and apply some fixers to convert it in to Python 3.x code where the common syntax change will be done by the auto conversion tool. •Library named “lib2to3” contains rich set of fixers which will handle almost all the code. •For example, you have a python 2 source file named example.py and if you want to convert it in to python 3 then we can use the library 2to3 in the command line as below $2to3 example.py This will do necessary modifications in the Source file Manual Changes •If you ask Auto Conversion will do complete porting then the answer is “No, it will not do complete porting”.

  4. •We need to run the Test Class and need to see if there are any errors. If error exists then that needs to be fixed accordingly based on the switching or comparison issue where the key name to be fixed in the function. •To be honest, this is an iterative process or approach until all the code runtime has been validated. •Once the common pattern has been identified, then there is another process named as “grep” & “sed” to replace across many files. Runtime Validation & Fix: •Once all the conversion process has been done, then we need to perform lot of code runtime validation. This will help us to identify the things earlier which might cause an issue. •As python is a dynamic language, it is better to avoid visual static code inspection / changes. Common Runtime Problems and it’s solution: Problem 1: While porting, we may face “Type errors” during Runtime like bytes instead of str and vice versa Solution 1: We need to see the variable type in the statement and based on that use, xxx. encode () to get bytes or xxx. decode () to get strings If input is not known, then use try xxx. encode () except Attribute Error: pass Problem 2: Type Error: root – ERROR –——->installation failed: a bytes-like object is required, not ‘str’ Solution 2: Change the byte type to string type or add b as prefix to the string under comparison Problem 3: AttributeErrorsuite_setUp () or suite_tearDown () is missing for some test suites. Solution 3: Add the dummy suite_setUp () and suite_tearDown () methods. Problem 4: Tab Error: inconsistent use of tabs and spaces in indentation Solution 4: Search for tab characters and replace with space characters. Problem 5: Type Error: ‘FileNotFoundError’ object is not sub-scriptable Solution 5: In Python 3, FileNotFoundError is not sub-scriptable so use errno attribute, e.errno Steps to be performed post migration: •Caniusepython3 will help to check which software dependencies will block you from supporting Python 3 using the tools provided.

  5. •Futurize or Modernize will make sure that your Python 2 code is compatible with Python 3 •In setup.py file, one can update the classifiers which contain Programming Language:: Python :: 3 to indicate your code supports Python 2 and 3. •Automating tests using tox will help you to understand that code stays compatible with Python 2 and 3

More Related