This page looks best with JavaScript enabled

Introduction To Python

 ·  ☕ 4 min read

Background

Python is a dynamic language, which means that python is compiled (turned into code that your computer understands) at runtime rather than statically compiled. To put it simply, you write python code in a plain text file and then execute that file with python. There is no compiling code into runnable (binary) code.

Details

A Python ‘interpreter’ (a program that understands Python code and turns it into computer code) and your python code are all that is required for python.

Setup - Linux

Python is included in nearly every Linux distribution as it is used in many components of Linux. If, for some reason, it is not installed you can install it through your distributions application manager (such as apt-get in debian based distros or yum in RedHat based distros).

Setup - Windows

Python is typically not included in Windows based systems; however, installation is not difficult. Grab the official interpreter from python.org You probably want the ‘Python x.x.x Windows x86 MSI Installer’ download as you do not need the source. If you have a 64-bit install of Windows then grab the ‘Python x.x.x Windows x86-64 MSI Installer’ version.

After downloading the appropriate version, install python and you should be ready to go!

Confirm Python Installation

We should first confirm Python is installed and set up correctly. This is a very easy task:

  • Open a command prompt (referred to as a shell)
  • Type the following and press enter: python --version
    You should see something similar to Python 2.7.6

If you do not see the above you more than likely have an installation problem with python which must be fixed before you can continue

Basic Info

Python comes with a few tools used for developing and debugging code. One of the most important is the python shell. The python shell runs inside a command prompt shell. To use the python shell simply open up your command prompt and type ‘python’. You should see a few messages returned and your prompt should now be a ‘»>’, for example, on linux you would see something similar to:

drad@xia:~$ python
Python 2.7.6 (default, Jan 11 2014, 14:34:26)
[GCC 4.8.2] on linux2
Type 'help', 'copyright', 'credits' or 'license' for more information.
>>>

If you hit enter a few times you will notice that the cursor stays as ‘»>’ for each line. This tells you that you are in a python shell rather than your standard command prompt shell. To exit the python shell simply type ‘quit()’ and press enter.

The Hello World!

As with most languages, an example is often the easiest way to see, understand, and learn the language. The following example will print ‘Hello World!’ from the python shell.

  • Open a shell
  • Start a python shell (type python and press enter)
  • Type the following: print 'Hello World!';
  • Press enter

You should see something similar to the following:

drad@xia:~$ python
Python 2.7.6 (default, Jan 11 2014, 14:34:26)
[GCC 4.8.2] on linux2
Type 'help', 'copyright', 'credits' or 'license' for more information.
>>> print 'Hello World!'
Hello World!
>>>

I know the above is not glamorous or all that difficult but you have now ran your first bit of python!

Hello World! Script

Most Python code is not executed through the python shell as programs quickly get complex and have many different components (libraries, dependencies, etc.). For this reason, among others, python code is typically stored in files (and files in projects but this is a later topic). We will now take the ‘Hello World!’ example from above and put it in a script to illustrate.

  • Open a text editor (stick with a basic editor like nano or notepad for now)
  • In the text editor, paste the following code:
 #!/usr/bin/python
 # Author: drad     Date: 2014-01-29
 # This is the Hello World! Script program.
 print 'Hello World!'

Save the file as ‘hello.py’

  • Open your command prompt and type the following: python hello.py

You should get the same results as before:

python hello.py
Hello World!

The above information and examples are very basic but should provide a good introduction into Python. Please stay tuned for further articles on Python.

Notes

  • we recommend starting any new Python effort with Python 3.x
  • this article was originally wrote/tested with Python 2.7.6
Share on

drad
WRITTEN BY
drad
Sr. Consultant