Introduction to Python
Python is a high-level, interpreted, interactive and object-oriented scripting language which finds its application in many areas like –
- Webscripting
- 3d Modelling (Blender)
- Desktop Applications – Games (Pygame)
- Scientific usage (SciPy/NumPy)
Python source code is available under the GNU General Public License (GPL). There are two major Python versions, Python 2 and Python 3.
Python features
- Open Source and Simple to use
- Very powerful and Ubiquitous
- Supports broad standard library
- Supports interactive testing and debugging
- Established interface with all major DB’s
- Runs on variety of hardware platforms
Technical features of Python
- Object-oriented (supports both
functional
andstructured
programming) Dynamically
andstrongly
typed- Whitespace delimited (Indentation)
- Scripting language which supports large applications.
- High-level dynamic data types and supports dynamic type checking
- Automatic
garbage collection
- Interpreted makes compiler interact with developer.
- Easy integration with
C
,C++
,COM
,ActiveX
,CORBA
andJava
.
Python Implementations
- CPython – Python implementation on standard C language.
- Jython – Python implementation with Java virtual machine to blend with Java.
- Pypy – Python implemented in Python and its Just-in time compiler making it fastest.
- Iron Python – for windows, which implements common runtime libraries to interface with .NET.
Difference between Python2 & Python3
Python versions
- Two main Python current versions
- At the time of this writing: 2.7.14 and 3.6.4
- Small but significant differences
Python 2
- Is still in current use
- Will be moved to maintenance after its final release 2.7.x
- Some updates from Python 3.x have been backported to 2.7
- Not fully compatible with Python 3
Python 3
- Intentionally broke compatibility with 2.x in order to make things better
- Most modules have now been updated with 3.x versions
- Should be the default for any new project you start
Reasons to Use Python 3
- From Python.org: “Short version : Python 2.x is legacy, Python 3.x is the present future of the language”
- Unless you need a 2.x module, use Python 3
Some sample differences:
Print:
Python 2
treats “print” as statement rather a function.Python 3
explicitly treats “print” as a function.
Integer Division:
Python 2
treats numbers without any digits. (Output of expression 3 / 2 is 1, not 1.5). To get the result 1.5, you would have to write 3.0 / 2.0.Python 3
evaluates 3 / 2 as 1.5 by default, which is more intuitive for new programmers.
List Comprehension Loop Variables: Common name for the variables that is iterated over in a list comprehension as a global variable get interchanged. This is fixed in Python 3.
Unicode Strings: By default Python 3 stores strings as Unicode unlike Python 2.
Raising Exceptions: Python 3 requires different syntax for raising exceptions.
- Python 2:
raise IOError, “some error message”
- Python3:
raise IOError(“some error message”)