An Introduction to Python Programming #1

Python is a high level programming language useful for, but not limited to, everyday tasks. Python is quick to pick up and easy to understand. Many of python’s statements are very English like so there is not much confusion. Lets take a look at pythons basic print statement, we are going to be writing the traditional Hello World program first, and then we will take a look at some other things.

First off, you will need to install python. Using your preferred method of installation install the package python. Now fire up your favourite editor and lets get started.

Now, the obligatory Hello World program just prints the words Hello, World! to stdout which is usually the terminal.

To print to stdout python uses a print statement which takes the syntax of

print string

a string is just a bunch of characters. Now, to be able to run python programs directly you need to tell the computer what type of program you have written. We do this using what is called a ‘shebang’ line, named because the first two characters of the shebang line are #! the shebang line for a python program looks like this

#! /usr/bin/env python

this tells the computer where the interpreter for your program, is stored. Go ahead and add that as the first line in your file. From this point on and in any subsequent python tuts I write here, I will assume that the shebang line has already been entered.

Now we get to the business part of our first program, the print statement, which should look like this.

print "Hello, World!"

Thats it, its that simple. Go ahead and add that line to your file, on a seperate line from the shebang line. Now save the file as “hello.py” (remember this is case sensitive) then, in the terminal, cd to the directory where you saved the file and run this command

./hello.py

You should now see “Hello, World!” printed on the terminal, if you do, well done you have successfully written your first python program. If you dont see those words or if you get an error message, re read what I have written and see if you made a mistake, if you didnt just leave a comment with the error message and whatever was in your file and I’l try and help get it sorted.

Now that we have had a look at printing a few words to the screen, lets look at printing variables. A variable is like a container that stores whatever you tell it to. Lets rewrite our Hello World program to use a variable. When you need to make a variable in python you use the = operator to assign it a value like this

hello = "Hello, World!"

This stores the string “Hello, World!” in the variable hello. Now we can simply reference the variable name without any quotes in our print statement and we will get the contents of the variable printed, so construct your print statement like so

print hello

Save the file under hello2.py and run it in the same way as you did before (using the new filename). With any luck you should see the same output as before which is great! Well done!

Now you may be thinking that using a variable has added an extra line to our program unnecessarily but all will become aparent by the end of this post. So we now know a bit about the print statement and variables, now lets look at user input using the raw_input() function. This function takes user input and can store it in a variable for printing/operating on. The raw_input() function takes the following syntax

variable = raw_input("Prompt to be displayed: ")

then, whatever the user enters gets stored in the variable. So lets rewrite out Hello World program to use this method of printing a string. Enter the following into a file named hello3.py

string = raw_input("Input a string please: ")
print "Your input was: %s" % string

Now, allow me to quickly explain a few things about that print statement that we have not seen before. The %s means that we want a variable of type string to be entered at that location. After the end quote we tell python that we want to tell it about an interpolated variable by using the % on its own followed by the variable name that we want entered.

With a bit of luck, you should now have written your first 3 python programs. In my next few python posts il be talking about loops and conditionals which really help structure your programs. I hope this was helpful to you, thanks for reading.