Getting Started

Why Learn Python?

Python is a general-purpose, versatile and popular programming language. It’s great as a first language because it is concise and easy to read, and it is also a good language to have in any programmer’s stack as it can be used for everything from web development to software development and scientific applications such as data mining and machine learning.

Fun Fact: Python is named after the BBC show “Monty Python’s Flying Circus” and has nothing to do with reptiles.

Installation

Python is available on a wide variety of platforms including Windows, Linux and Mac OS X. 

Getting Python

The most up-to-date and current source code, binaries, documentation, news, etc., is available on the official website of Python https://www.python.org/ You can download Python documentation from https://www.python.org/doc/

Windows Installation

Here are the steps to install Python on Windows machine.

  1. Open a Web browser and go to https://www.python.org/downloads/.
  2. Follow the link for the Windows installer python-2.7.15.msi file 
  3. To use this installer python-2.7.15.msi, the Windows system must support Microsoft Installer 2.0. Save the installer file to your local machine and then run it to find out if your machine supports MSI.
  4. Run the downloaded file. This brings up the Python install wizard, which is really easy to use. Just accept the default settings, wait until the install is finished, and you are done.

Linux and Unix Installation

Python comes pre-installed on all versions after Ubuntu 14.04. you can access it by typing the following into the terminal for Python 2

 $ python --version

If you don’t have Python 2 installed then, you can install it by typing the following into the terminal :

sudo add-apt-repository ppa:fkrull/deadsnakes-python2.7
sudo apt-get update
sudo apt-get install python2.7

Alternatively: Here are the simple steps to install Python on Linux/Unix machine.

  1. Open a Web browser and go to https://www.python.org/downloads/.
  2. Follow the link to download zipped source code available for Unix/Linux.
  3. Download and extract files.
  4. Editing the Modules/Setup file if you want to customize some options.
  5. run ./configure script
  6. make
  7. make install

This installs Python at the standard location /usr/local/bin and its libraries at /usr/local/lib/pythonXXwhere XX is the version of Python.

Mac OS Installation

Recent Macs come with Python installed, but it may be several years out of date. See http://www.python.org/download/mac/ for instructions on getting the current version along with extra tools to support development on the Mac. For older Mac OS’s before Mac OS X 10.3 (released in 2003), MacPython is available.

Setting Up PATH

Programs and other executable files can be in many directories, so operating systems provide a search path that lists the directories that the OS searches for executables. The path is stored in an environment variable, which is a named string maintained by the operating system. This variable contains information available to the command shell and other programs. The path variable is named as PATH in Unix or Path in Windows (Unix is case sensitive; Windows is not). In Mac OS, the installer handles the path details. To invoke the Python interpreter from any particular directory, you must add the Python directory to your path.

Setting path at Unix/Linux

To add the Python directory to the path for a particular session in Unix −

  • In the csh shell − type setenv PATH “$PATH:/usr/local/bin/python” and press Enter.
  • In the bash shell (Linux) − type export PATH=”$PATH:/usr/local/bin/python” and press Enter.
  • In the sh or ksh shell − type PATH=”$PATH:/usr/local/bin/python” and press Enter.
  • Note − /usr/local/bin/python is the path of the Python directory

Setting path at Windows

To add the Python directory to the path for a particular session in Windows − At the command prompt − type path %path%;C:\Python and press Enter. Note − C:\Python is the path of the Python directory

Running Python

There are three different ways to start Python:

Using the interactive interpreter

Type the following on the command line/shell window/interpreter On linux/Unix type

$python  

On Windows

C:> python 

Script from a command line

To execute a python script (usually ends with a .py eg hello.py), type the following on a command line On Unix/linux

$python script.py 

On Windows/DOS

C: >python script.py 

Note − Be sure the file permission mode allows execution.

Integrated Development Environment

You can run Python from a Graphical User Interface (GUI) environment as well, if you have a GUI application on your system that supports Python.

  • Unix − IDLE is the very first Unix IDE for Python.
  • Windows − PythonWin is the first Windows interface for Python and is an IDE with a GUI.
  • Macintosh − The Macintosh version of Python along with the IDLE IDE is available from the main website, downloadable as either MacBinary or BinHex’d files.