Contents
Preface
Getting started with python is an effort to enable self learning by using small basic examples of the python building blocks. My belief is that if your foundations are good your can build many high rise building using that foundation.
Hemant Sharma
Introduction
Python is a high level general-purpose programing language which is often compared to Tcl, Perl, Java, JavaScript, Visual Basic or Scheme. Python runs on Windows, Linux/Unix, Mac OS X, and has been ported to the Java and .NET virtual machines and used for
For real life usage Python is generally used to create in house testing and automation tools for deployment, monitoring system & environment building. There are not many commercial products created with python but it is more often used to supplement the main products created with java or c/c++ and other languages.
Python is written by Dutch computer programme, Guido Van Rossum in early 1991 with the following objectives
1. An easy and intuitive language
2. Open source
3. Easy to understand coe as plain English
4. Suitability for everyday tasks
The name python is given by Van Rossum inspired by a British sketch comedy series, Monty Pythons Flying Circus, broadcast by the BBC from 1969 to 1974.
The Python Software Foundation (PSF), a non-profit corporation, holds the intellectual property rights of Python programming language. PSF manage the open source licensing for Python version 2.1 and later and own and protect the trademarks associated with Python.
PSF run the annual North American PyCon conferences, support other Python conferences around the world, and fund Python related development
Features
Python comes with extensive standard library of built in modules. Some modules are written in C to support system level access such as I/O. Other modules written in python provides standard functionality like string operation using regular expression ,protocols like HTTP, FTP, SMTP, XML-RPC, POP, IMAP, CGI programming , system calls , TCP/IP sockets . A number of third party modules are also available to supplement existing python modules.
Releases
There are two current versions of Python, 2.x and 3.x, Python 2.7.15 is the latest version for 2.x and python 3.6.5 is the latest version for 3.x as of now. 3.x version is cleaned up and enhanced version of python 2.x and does not have backward compatibility due to complete overhauling of 2.x version.
Top 10 key differences between 2.x and 3x versions
- Print command is statement in 2.x and a function in 3.x
- The integer division is classical division in 2.x which truncates the decimal but true division in 3.x always returning the result with decimals.
- 2.x has ASCII str() type and unicode function but no byte types; 3.x has Unicode(utf8).
- xrange () function in 2.x returns a list to use in for loop like control structure, it is replaced by range() in 3.x which returns a iteratable structure.
- Exception Arguments: 3.x requires exception arguments to be enclosed with parentheses while 2.x can accept a quoted notation.
- Exception handling in 3.x requires keyword as to catch the standard exception in user defined variable and no as keyword is required in 2.x
- 3.x has only next() function to retrieve the next item from the list whereas 2.x can use next() or .next() method.
- 3.x prevents loop variable and global variables mix, in 2.x variable can get mixed if the same variable is being used globally and in a for loop also.
- 3.x rounds of decimal to nearest even number
- input() function stores input as string in 3.x , user input can be other types also in 2.x and we raw_input() function is need to capture.
Downside of using 3.x
python 3.x is not backward compatible with 2.x and much of the 2.x software doesnt work on 3.x version yet. Features which are only available in 3.x releases will not be back ported to the 2.x series.
Choosing between Python 2 & Python 3
Few things to consider when deciding a version for your use
- If you have lot of python code already in use most likely it is in 2.x version and you should choose 2.x as 3.x is not backward compatible.
- Many third party packages may not support 3.x so if you plan to use third party packages check if support for 3.x is available or you can port it easily to 3.x
- Most of the Linux and other distributions come pre-installed with versions which are a few releases behind and your distribution may still have 2.x as python distribution.
Code Conversion Tools
2to3 tool allows 3.x code to be generated from 2.x source code,
3to2 tool, allows 3.x code to convert back to 2.x code. In theory,
These tools are good if code doesnt make use of 3.x features extensively. Since there is no backward compatibility code will not convert successfully
1. Installation
python installation is simple and straight forwards but if you are working on a shared or company provided computer, it worthwhile to check if it is already installed. It can save some time in getting started with python
Checking for existing installation in Unix/Linux, Windows or Mac
Open a terminal window for command prompt and type python. If python is already installed then an interactive python shell with details of version if already installed
C:\> python
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 20:32:19) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
$python
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
However if python is installed in a nonstandard location, you may have to go to that location to see what version is installed.
From python shell you can get help on a module, keyword, or topic by typing help() , which opens a help subshell , where you can type topics or keywords to get help , similar to man pages in Unix/Linux
>>> help () ### Launches help subshell to run help commands
help>
Download Python
If python is not installed or you want to install a different version you can download latest Python binary or source distribution from python.org, at http://www.python.org/download/
You can download 2.7.x or 3.x version depending on what you decide based on your requirements.
Installation on different platforms
Windows
Download and extract zip arrive in a directory, go to command prompt and run following command
python setup.py install
Unix /Linux
Download source code in a directory and run following from the command prompt
#./configure
#make
#make install
README file in the root of the Python source has details of configuration options and caveats for specific Unix platforms.
make install can overwrite or masquerade existing python binary. make altinstall is therefore recommended instead of make install since it only installs exec_prefix/bin/pythonversion.
Mac installation
Python comes pre-installed on Mac OS X, but often it is old version and best option is to upgrade by downloading and installing a newer version from python download location. You can choose between source code and 32/64 installer versions.
Working in python shell