Learning Python - Part 1

Python is a well known and increasingly popular programming language, used in both quick scripts designed for a particular task and large complicated applications. Python is also increasingly being used in web applications, thanks to the recent proliferation of frameworks like Pyramid, Django, WebCore, Flask, and others.

Over the next few weeks I will be introducing the language, how to program in it, and some popular frameworks and toolkits used to develop applications. I will be basing these tutorials on a really great, free eBook called A Byte of Python (make sure you download the version for Python 2.x).

Introducing Python

For the programming geeks: Python is a cross-platform, object-orientated, dynamically typed, interpreted programming language. Python is easy to learn, but powerful to use, making it ideal to use for both teaching and application development.

Python is really just a language specification, but when people talk about Python, they are usually referring to the Python interpreter which is written in C, otherwise known as CPython. CPython is known as the reference implementation, which in short means that the people who write the language specification also write that interpreter, and that it showcases how Python should look and work and how other Python interpreters should work.

CPython works on all major operating systems, and is the interpreter we will be using. If you are using a fairly recent version of Mac OS X, or you're using Linux, you most likely already have Python installed. Windows users will need to download and install Python (make sure you download Python 2.7), and I recommend using the ActivePython installer from ActiveState, as it contains some Windows-specific modules pre-installed which are necessary when installing some Python modules on Windows.

Introducing CPython

The CPython interpreter has two modes when run from the command line. The first is fairly normal, where when given a Python source file the interpreter runs the file from top to bottom like any other interpreter. The second is where an interactive prompt runs and you can type in commands and they will be executed as you type them in.

The first method is used when running applications, as you never see the interpreter, you only see the application. The second method is very useful when you are exploring a new module, or trying to figure out how to use a particular object or implement a specific piece of functionality.

Let's Get Started: Understanding Programming

Let's get started with a really simple program, the famous "Hello World."

Run the Python interactive interpreter by opening your terminal application (usually known as the command prompt on Windows) and typing in python. Windows users might need to navigate to the directory they installed Python into. You should see something like this:

Python 2.7.2+ (default, Oct  4 2011, 20:06:09) [GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.

The ">>>" is the Python prompt. This is where you type in your code. Now type in, print 'Hello World' and press enter.

>>> print 'Hello World'
Hello World

There we go, you've just written your first Python program! It may not look like much, but it illustrates a very important point in programming.

When you typed in print 'Hello World' you issued an instruction to the Python interpreter, and therefore your computer, to print out "Hello World." It may not seem like a big deal, but the underlying concept is that we are issuing commands to the computer to make it perform certain functions. No matter how big or small your application is, no matter what programming language or programming paradigm you are using, you are simply giving the computer tasks to perform.

Update: Part 2


comments powered by Disqus