Recently I had an interest in learning to code in Python. So what exactly is Python and how is it useful? Python is a high-level programming language that is easy to learn, easy to read, uses fewer lines of code compared to C++ or Java, and works great in small or large scale. Python has been embedded into a number of products as a scripting language to provide a more efficient means of management capabilities.
A little trivia for you: Where did the Python name come from? According to Dr. Charles Severance, University of Michigan, it was named after a famous British Comedy show called Monty Python’s Flying Circus.
So why would I want to learn Python? Based on my experience, I have listed a few reasons below.
– Python is a free open source software and there is a large community to provide support if needed.
– Python is fast with a quick edit programming style.
– Python is powerful, comes with a standard library, and APIs are well documented.
– Python has cross-platform support and is capable of running on virtually any machine.
Installation
For those of you who have been inspired by my introduction and would like to get Python installed so you can start playing around with it, I have included the steps below for installing Python on Windows, Ubuntu, and Mac OSX. There are two major versions; Python 2 which is widely deployed but legacy and Python 3 which is the present and future moving forward.
Windows
1. Download Notepad++ to use as your programmer editor, or you could use something like Sublime Text. Make sure you are running as the administrator and use the default install settings. However, you can change the install settings if desired. If using Notepad++ you will want to configure Preferences , Tab Settings and select the Replace by Space and the tab size should be 4. This will help you avoid Python indent errors.
2. Next you want to download Python. You can download either version. Python 2 is older and more established but Python 3 adds some capabilities that were missing in version 2. As of this writing version 3.4.3 is current and is the version I will install.
3. Select Latest Python 3 Release – Python 3.4.3. Scroll down and select the 32 or 64-bit msi installer depending on your system. Download and run the installer.
4. Use the default installation settings which will install Python at the root of C:\Python34\.
5. Once Python is installed you will need to add it to the system path environment variable. Navigate to Control Panel, System and Security, System and open the Advanced System Settings and click on Environment Variables. Select Path, click Edit and add C:\Python34 and C:\Python34\Scripts separated by semi-colon as shown below.
6. Open a new command prompt and verify that you can run Python from the command line. The triple arrow prompt confirms this. One of the differences between v2 and v3 is that v3 requires you to use the parenthesis.
7. The above step is referred to as using interactive python or you may hear it called the REPL (Read, Evaluate, Print, Loop). Interactive Python is good for experiments and testing of programs that are only a few lines long. For longer programs you will want to use scripts. In a script you type everything into a file using a text editor and tell Python to execute the script from the file.
8. To demonstrate this, create a directory to store the files using a .py extension. In my example I created a folder on the desktop called ‘LearnPython’, created a simple script using Notepad++ to count the number of (n’s) in the word banana and saved as counter.py and called it from its location using cmd.exe.
Ubuntu Desktop
1. Python 2 comes installed on modern versions of Ubuntu. In addition, if you are running Ubuntu 13/14/15, a version of Python 3 comes installed by default.
Ubuntu 13.04 – Python 3.3.2
Ubuntu 14.04 – Python 3.4.0
Ubuntu 15.04 – Python 3.4.3
2. The screenshot below shows you how to verify the version of Ubuntu using the command lsb_release -a. To view the version of Python 2 installed type python -V. To view the version of Python 3 installed type python3 -V. To access the interpreter type python3. The triple arrow prompt confirms python is waiting for input.
3. However, if you are running Ubuntu 12.04, you will need to install Python 3 as its not installed by default. You can open a terminal and run sudo apt-get install python3-minimal which will install Python 3.2.3 or you can follow the procedure below to install version 3.4.3 manually.
4. Download Python 3.4.3 and extract the contents of the file to a folder.
5. Press Ctrl-Alt-T to open a terminal. Navigate to the location of the extracted contents. For me this was …/downloads/Python-3.4.3
6. Run the following commands: ./configure
7. Run make
8. Run make test
9. Run sudo make install
10. Type Python3. The triple arrow prompt confirms Python is waiting for input.
Mac OSX
1. Python 2.x is already installed on Macintosh OS/X operating system. If you want to use this version then all you need is a text editor.
2. Download a program called TextWrangler to use as the editor. Note: Under TextWrangler -> Preferences -> Editor Defaults tick the check box for “Expand Tabs” and close the dialog box. This will save you from Python indent errors.
2. To access the interpreter open a terminal program. It is located under Finder -> Utilities -> Terminal.
3. To run your program, navigate to the proper directory and type python <yourfile.py>
4. If you wish to run Python 3.x then you will need to download and install it.
5. Download Python 3.4.3 and from downloads run the .pkg file using the default settings.
6. Open a Terminal program and type python3 -V to verify the version installed.
7. Type python3 to enter the interpreter. The triple arrow prompt confirms Python is waiting for input. You can type Ctrl-Z to exit the interpreter.
PEPS
PEPS (Python Enhancement Proposals) are documents that are used in the development of the Python language.
Accessing the HELP
The REPL includes a simple function called help. Simply type help(). You can type help() for interactive help or use help(object) for help on a specific object. To return to the python interpreter type q or quit.
Wrap Up
Now that you have Python installed on whatever flavor OS you typically work with, I hope you find Python as easy to learn as it was to install. To learn more on Python have a look at Coursera. They have a very good course called “Programming for Everybody” that will get you up to speed in no time.