If you are new to Python, you might be confused about the different versions that are available. Although Python 3 is the latest generation of the language, many programmers still use Python 2.7, the final update to Python 2, which was released in 2010.In this course, we will be working on python 3.
Why we are leaning python 3?
In 2008, Guido Van Rossum (author of python) founded that this version is lacking of many thing and needed to overcome all of those things and released complete python 3, But still a problem persisted, now developer were not able to run their earlier code on python 3, in 2010. there was not much difference in python 3 but still it was a problem of compatibility with older versions. Moreover many companies had written their code in version 2, now they wanted to upgraded into python 3 . Then they decided they’ll be continuously improving python 3 and keep introducing new libraries to overcome compatibility issues and keep maintaining python 2. Now its 2018, most of the users have migrated to python 3. That’s why we should learn python 3.
Note: python2 will be keep maintained till 2020 only.
Note: If you’re a beginner you may skip following part. You will learn all the things ahead.
What if you are starting a new project and have the choice to pick?
I would say there is currently no “right” or “wrong” as long as both Python 2.7.x and Python 3.x support the libraries that you are planning to use. However, it is worthwhile to have a look at the major differences between those two most popular versions of Python to avoid common pitfalls when writing the code for either one of them, or if you are planning to port your project.
The __future__ module
Python 3.x introduced some Python 2-incompatible keywords and features that can be imported via the in-built __future__ module in Python 2. It is recommended to use __future__ imports it if you are planning Python 3.x support for your code. For example, if we want Python 3.x’s integer division behavior in Python 2, we can import it via
More features that can be imported from the __future__ module are listed in the table below:
|feature||optional in||mandatory in||effect|
|nested_scopes||2.1.0b1||2.2||PEP 227: Statically Nested Scopes|
|generators||2.2.0a1||2.3||PEP 255: Simple Generators|
|division||2.2.0a2||3.0||PEP 238: Changing the Division Operator|
|absolute_import||2.5.0a1||3.0||PEP 328: Imports: Multi-Line and Absolute/Relative|
|with_statement||2.5.0a1||2.6||PEP 343: The “with” Statement|
|print_function||2.6.0a2||3.0||PEP 3105: Make print a function|
|unicode_literals||2.6.0a2||3.0||PEP 3112: Bytes literals in Python 3000|
The Print function
Very trivial, and the change in the print-syntax is probably the most widely known change, but still it is worth mentioning: Python 2’s print statement has been replaced by the print() function, meaning that we have to wrap the object that we want to print in parentheses.
Python 2 doesn’t have a problem with additional parentheses, but in contrast, Python 3 would raise a SyntaxError if we called the print function the Python 2-way without the parentheses.
print 'Python', python_version() print 'Hello, World!' print('Hello, World!') print "text", ; print 'print more text on the same line'
Python 2.7.6 Hello, World! Hello, World! text print more text on the same line
print('Python', python_version()) print('Hello, World!') print("some text,", end="") print(' print more text on the same line')
Python 3.4.1 Hello, World! some text, print more text on the same line print 'Hello, World!' File "
", line 1 print 'Hello, World!' ^ SyntaxError: invalid syntax
Printing “Hello, World” above via Python 2 looked quite “normal”. However, if we have multiple objects inside the parentheses, we will create a tuple, since
print 'Python', python_version() print('a', 'b') print 'a', 'b' Python 2.7.7 ('a', 'b') a b
This change is particularly dangerous if you are porting code, or if you are executing Python 3 code in Python 2, since the change in integer-division behavior can often go unnoticed (it doesn’t raise a SyntaxError).
So, I still tend to use a float(3)/2 or 3/2.0 instead of a 3/2 in my Python 3 scripts to save the Python 2 guys some trouble (and vice versa, I recommend a from __future__ import division in your Python 2 scripts).
print 'Python', python_version() print '3 / 2 =', 3 / 2 print '3 // 2 =', 3 // 2 print '3 / 2.0 =', 3 / 2.0 print '3 // 2.0 =', 3 // 2.0 Python 2.7.6 3 / 2 = 1 3 // 2 = 1 3 / 2.0 = 1.5 3 // 2.0 = 1.0
print('Python', python_version()) print('3 / 2 =', 3 / 2) print('3 // 2 =', 3 // 2) print('3 / 2.0 =', 3 / 2.0) print('3 // 2.0 =', 3 // 2.0) Python 3.4.1 3 / 2 = 1.5 3 // 2 = 1 3 / 2.0 = 1.5 3 // 2.0 = 1.0