In the first lesson we introduced Python, the CPython interactive interpreter, and wrote a very simple program. This lesson we'll look at choosing a good editor, rewriting our previous program into a script and Python's basic types.

Choosing a Good Editor

People who have been in the programming industry for a long time can tell you how important a good editor is to writing code efficiently and effectively. They can also tell you about the editor flamewars that occur, most notably the war between VI and Emacs. We won't get into any wars here, but I will try to explain the charateristics of a good editor, and list some of the editors I have used.

Firstly, a good editor always has syntax highlighting. This means that it colours words and characters according to their meaning in a particular programming language. For instance, the "print" keyword we used in our first program might be bold because it is a special word in Python, and the "Hello World" text might be yellow because it is a string. The syntax highlighting makes it easier to read the code by making it easier to differentiate between different elements in your code.

Your editor should support indentation, automatically indenting your cursor to the same level as your code. It should be able to indent with either tab characters or spaces, be able to adjust how many spaces to indent by and how wide your tabs are in spaces. You should also be able to indent using the indentation settings by simply pressing the tab key.

A good editor also can set the line endings of your files, and convert between line endings. It should support Windows, UNIX/Linux and Macintosh line endings.

Of course your editor should be able to both load and save files. This may seem like a pretty obvious thing, but with more and more things becoming web-based, it won't be long before we have completely web-based editors.

These are the essential features you need in a good editor. There are other features, like being able to choose the file encoding, which are useful to have, but they are not crucial to developing in Python.

Some good editors are Kate (from KDE), GEdit (from Gnome), Notepad++ (for Windows), TextMate and BBEdit (Mac OS X).

Choosing a Good IDE

IDE stands for Integrated Development Environment. An IDE is a text editor on steroids, it usually contains functions and tools to assist in development of a particular language or development environment. As with editors, making sure you have a good IDE is important when developing. A good IDE has all the functionality of a good editor, and then some.

Firstly, a good IDE should have code completion. Code completion is that feature where a little window pops up and shows you a number of options that match what you're typing. The really excellent IDE's use the Python modules you've imported into your current file, no matter where they come from.

A good IDE should be able to run your program from within it, redirecting any output into a window or dialog for later inspection. An integrated debugger, which gives you the ability to step through your code line by line, can be crucial to figuring out complex problems.

A good IDE should have either a shell prompt, or a Python shell, or both built in. A Python shell in particular is extremely useful for trying out various ideas without having to run through your entire application.

Another essential feature in a good IDE is refactoring support. If you find that you need to rename a certain function or object, refactoring makes your life much much easier by performing an intelligent find and replace. Refactoring can also implement declared methods in languages like C++ which have separate implementations of declared methods and functions.

Some good Python IDE's are Eric (cross-platform), PyCharm (cross-platform, commercial, though there are some free licenses available), Eclipse with the PyDev plugin (cross-platform).

A Proper Python Program

We previously wrote a really small program in the Python interactive interpreter, but that obviously isn't the way real Python programs work, so let's rewrite our program.

Open your editor or IDE, make sure you have a new file open, and type in the following:

#!/usr/bin/env pythonprint 'Hello World'

Now save that file as "" somewhere easily accessible, open your terminal, ans run it by typing in:

$ python

Don't type the "$", that's just a sign for your command prompt. You should see something like this:

$ python helloworld.pyHello World

Yay! That looks more like a real program.

If you're wondering what that first line is, it's called a hashbang, and on Linux, OS X and other UNIX-style operating systems it tells the system how to run this file when it is marked as an executable.

The Basics of Python

Just printing out "Hello World" is not really all that exciting, and it most certainly is not useful. To make our code useful we need to use constants and variables and other programming structures.

Literal Constants

42, 'This is a string' and 3.14e-1 are all literal constants, that is, they represent exactly what they are (they are literal) and they will not and cannot change (they are constant). An explicit value like this is called a literal constant.


Variables are little containers that can hold a value. Variables come in various types (explained below) and are typically set via some sort of input, be it literal constants, user input, or even reading from a file.

Number Types

Python has 4 types of numbers, integers, long integers, floating point numbers and complex numbers.

Integers and long integers are just whole numbers like 2, 89, -46, etc. They can be positive or negative, but they cannot contain a decimal point. Variables of the integer type can hold values up to around 2.1 million, and variables of the long integer type can hold values of around 9 million billion. Integers use the int class, and long integers use the long class.

Floating point numbers are numbers containing a decimal point. They use the float class.

Complex numbers are numbers not represented by any of the types above. Unless you are doing advanced mathematics, you will unlikely ever use this type.


Strings are just groups of characters, usually words. In our first program, 'Hello World' is a string. The quotes indicate it is a string.

In Python you can use either single or double quotes to denote a string. If you want to use a single quote in a string denoted by single quotes, insert a back slash (\) before the quote. This is called escaping, and works for double quotes in double-quoted strings too. Unfortunately using a back slash as an escape character means that if you want to use a back slash in your text you need to escape it with another back slash (\\).

If you have a long piece of text you want to write out, you can use a multiline string. Multiline strings are declared by using 3 quotes on either side of the string. These strings can then stretch over multiple lines, and any tabs or lines will be preserved. You can type both single and double quotes in these strings without needing to escape them.

Python also has special types of strings. The strings we have been dealing with so far are encoded strings. They use the encoding type of the file to determine which encoding they should be.

Python also has raw strings, where the escape characters mentioned above are not escaped. This is useful in regular expressions where you have to escape some characters, and then you'd need to escape your escape characters again. To make a string into a raw string, simply add an "r" to the front, like so: r'This is my string'

In addition to raw strings, Python also has unicode strings. These are particularly special strings because they allow you store non-English characters, like Chinese or Japanese, in your string. Unicode allows your application to be translated. Unicode strings are prefixed with a "u", like so: u'This is my string'

There may be times when you want to insert a special character which is not on your keyboard, or you don't want to make a string unnecessarily long. In these cases you can use various escape sequences (or escape characters) to add these special characters. For instance, if you want to add a tab character you can use \t, or if you need to specifically insert a Windows-style new line you can use \r and \n together, like so: \r\n. To insert unicode characters into a unicode string you can use the unicode escape characters: \u8634

One important thing to note about Python strings is that they are immutable. That means that you cannot change them. This may sound like a serious limitation, but it isn't really. Often, when adjusting strings in Python, you simply make new versions of the strings without even appearing to. The only time you might seriously run into this limitation is if you want to adjust only one particular character in a string.


Objects in programming terms are special items with properties and methods attached to them. An easy way to understand objects is to think of an apple. An apple has properties (it has a colour, seeds, flesh, a skin) and methods (it can grow).

In Python everything is an object. Even strings, numbers and functions.


A class is an abstract template of an object. If an object has properties, a class describes what properties an object has. An easy way to understand classes is to think of them as blueprints to a house. They are simply the designs, not the actual house. And just like you can build multiple houses from one set of blueprints, so you can create multiple object instances of a class.

Functions & Methods

A function is a block of code that performs a particular task. Functions can take one or more inputs and can output results. A method is a function that lives inside a class.


One particular point you really need to know and understand is indentation.

Rather than using curly braces or begin and end keywords, Python uses indentation to denote blocks of code. This is very important to remeber, as you may struggle to figure out why some code isn't working, and it is because you have not indented the code properly.


We've gone over a few concepts, but you may come across some of these words while getting into Python.

  • Identifier: identifiers name things, like variables, functions and classes.
  • Data types: integers, strings, floats, classes, etc are various types of data.
  • Keywords: special words that provide functionality and program flow.
  • Decorators: special functions that add functionality to other functions.


That's all for this lesson. We have gone through a lot of new functions and features, but if you still don't understand everything then please feel free to post a comment asking for further explanation.

Update: Part 3


comments powered by Disqus