The Python Walkthrough Thread

Associate
Joined
2 Nov 2005
Posts
931
Location
Leicester
The Python Walkthrough Thread [08/05/2012 Phase Three Is Out]

This is a follow-up to this thread: http://forums.overclockers.co.uk/showthread.php?t=18373927

<Noticeboard>
Heard about Rasberry PI? Python will commonly be used to program it.
It is never too late to join, Please reply and tell me that your interested and keep me informed of your progress!
When you have completed a phase, write a reply here.
</Noticeboard>

A few people was asking to learn Python but lacking the time. So my idea is to get people signed up to this thread, and I will walk all participant's through learning python at a much slower pace. Pretty experimental, haven't seen a thread like this before!

If you already know some programming, you might want to skip a phase! Let me know what phase you are capable of completing. You can help out the beginners.
If your a complete beginner, this thread is ideal for you from the start.

I am going to do it in Phases (like chapters in a book but much more interactive),
Phase One: Figure out who wants to take part! Reply to register your interest.
Participant list (Will be updated, Current member count: 56):
fail at the internets Status: Unknown
Izi Status: Completed Phase Two
Oxy Status: I believe he knows enough to beat Phase Five.
themadhaggis Status: Completed Phase Three
dean_e_boy Status: Completed Phase Three
cotton31 Status: Completed Phase Three
Rizo-UK Status: Completed Phase Two
AHarvey Status: Completed Phase Three
Chaos Status: Completed Phase Three
d_brennen Status: Completed Phase Two
MarcLister Status: Completed Phase Two
Spils Status: Unknown
spenncerr Status: Completed Phase Three
visibleman Status: Completed Phase Two
IanAG Status: Completed Phase Two
Vauxmad Status: Completed Phase Two
Kingorgg Status: Completed Phase Two
JHeaton Status: Completed Phase Two
Pho Status: Completed Phase Three
ticklemebougy Status: Completed Phase Two
Locke Status: Completed Phase Two
Frapple Status: Completed Phase Two
ScoobyDoo Status: Completed Phase Two
kalvindeane Status: Completed Phase Two
badcompany Status: Completed Phase Two
obby_82 Status: Completed Phase Two
mortals Status: Knows enough to beat Phase Five
snowdog Status: Knows enough to beat Phase Three
hamesy Status: Completed Phase Three
kkelly Status: Completed Phase Three
funkyphantom Status: Completed Phase Two
rexehuk Status: Unknown
ChrissyT88 Status: Completed Phase Three
Yannic Status: Unknown
two00lbwaster Status: About to begin Phase Three
dubs Status: Completed Phase Two
feeddagoat Status: Knows enough to beat phase three or four
hyperfighting Status: Completed Phase Two
Dreyrden Status: Completed Phase Two
eric_knapper Status: Completed Phase Three
Ice Rich Status: Unknown
BaJ Status: Completed Phase Three
tomk11 Status: Knows enough to beat phase four or five
mikeyp123 Status: Completed Phase Two
manic111 Status: Completed Phase Two
Spectrump Status: Completed Phase Three
sHo0sH Status: Completed Phase Two
FBi7 Status: Completed Phase Three
AndrewNR Status: Completed Phase Three
Nitrojan Status: Unknown
PianoBasher Status: Just getting started.
Devrij Status: Completed Phase Three
feriso Status: Unknown
Sirrel Squirrel Status: Completed Phase Three
dekez Status: Completed Phase Three
Goose Status: Completed Phase Two

Phase Two (Post #21): Get people set up with the relevant software. Make sure everyone is set up before going onto next phase.
Phase Three: Teach basic syntax of python using simple examples, while explaining each example :)
Phase Three Point Five: Explain some stuff I missed during Phase Three.
Phase Four: Explain where to find more information about python, what modules are available, how to use commonly used modules that come with python.
Phase Five: Make a basic text-based game.
Phase Six: Try some basic web application development using bottlepy.
Phase Seven: Make a GUI application using pyqt4.
Phase Eight: Make a 2D game using pygame (You know, proper 2D like pacman!).
Phase Nine: Learn databases using SQLAlchemy.
Phase Ten: Learn some serious web application development using Pyramid and use the SQLAlchemy knowledge from Phase Nine! This phase is a beast!

I know some pretty cool networking stuff, perhaps we could network up a game. So I guess that could be Phase 11.

We are currently at Phase Three, See Post #107 below!
 
Last edited:
I am working through the Udacity course, head first programming with Python and making games with python but would still like to take part, if nothing else I can help with the bits I already know :D

I hope it's 3.2 and the Udacity course is using 2.6 and bouncing from one to another is a pita :p

Yes, I am teaching Python 3.2.. nearly got 10.. maybe one more before starting Phase Two.
I don't mind people joining half way though though, as long as they read the thread and catch up.
 
Last edited:
Ok, I think its time for Phase Two (See the OP). If I missed anyone, you can still reply to express your interest :)

Installing Python 3.2 for Windows and Mac
0. If you have a Mac, grab the Python 3.2 dmg. OSX comes with Python 2.x which is too old. I don't have a Mac but if you get stuck just fire a reply here! Hint: It puts its files at: /Library/Frameworks/Python.framework/Versions/3.2/Resources/Python.app/Contents/MacOS/Python
1. Head over to http://www.python.org/download/
2. Download and install the 32bit version of Python 3.2, do not get the 64bit one as it may break things later on.

Installing Python 3.2 for Linux
1. I assume you know how to use Linux somewhat, since you have it installed, so open up your terminal (look through the menus for a terminal application) and try some of these commands:
python3
python3.2
2. It should hopefully say Python 3.0, 3.1 or 3.2, if neither of these commands provides python 3 then you will have to install its package. Post a reply for more help.

Installing Aptana, Available for Windows, Linux and OSX!
0. Make sure you have installed Python first! See above.

1. Head over to http://www.aptana.com/products/studio3/download and download + install it.

2. Start it up, you always get this dialog, just click "Use this as default and do not ask again", then press Ok.
tut1.png


3. You should now be looking at this:
tut2.png


4. Go to File --> New --> PyDev Project
tut3.png


5. Fill in the project name, if you have any problems, try removing spaces or other weird characters. Set the Grammar Version to 3.0
tut4.png


6. Press the blue hyperlink near the bottom "Please configure an interpeter..."
tut5.png


7. Press Auto Config, press Ok to all the dialogs that come up. You can press Ok on this one too.
tut6.png


If your on a mac, You might have to set the interpreter manually, Try looking here:
/Library/Frameworks/Python.framework/Versions/3.2/Resources/Python.app/Contents/MacOS/Python

8. You should see your project come up on the left:
tut7.png


9. Right click on it, and go to New --> PyDev Module
tut8.png


10. Give the module a name, do not use spaces, use understore instead. Finish will be grayed out if you do it wrong.
tut9.png


11. Write your first hello world program as shown below and press Ctrl+F11 or the "green play button" as shown to run it. If you get a dialog come up, select "Python Run" and press Ok.
tut10.png


Report back when you have completed this or if you are experienced with Python, tell me this so I can make a note on the OP <-- Read Carefully!. I appreciate feedback too :)
 
Last edited:
I am interested in this. Also, I have completed Phase two. :)

Added you, I have added everyones "Status" to the OP, I need to keep this updated so I know what phase everyone is at (or to what point you understand). So can people reply and let me know!

It will time for another phase to begin, due to me having to spend some time writing it up and getting feedback from all the participant's.

Edit: I have added more phases! Reading them amazes me how much I know!
 
Last edited:
How long will you be waiting between phases, and will you be keeping all of the relevant info available? I'd love to do this, but may find it hard due to a lack of free time. :)

Yeah, every last scrap of information will be on this thread. Lack of time is not a very valid argument, you only need to follow the tutorials in this thread and this thread will deliberately move at a pretty slow pace. Tutorials will be kept as short as possible and will be spaced out, allowing for discussion. I recommend you get in early so you don't have to catch up.

I estimate phases might last as much as a week, depending on how speedy people reply to my posts. I think a hard limit might be beneficial since some may forget about this thread. I think I will let you guys have a say though. Personally I think 1 week sounds good, they need to be spaced out enough to fit all this learning around peoples busy lives and for me to have a break from writing tutorials.

Edit: Thanks for telling me that you have completed it. I will fix your statuses on the OP.
 
Last edited:
I want to thank everyone for taking part in Phase Two but can you please make sure you have Python 3.2 installed! I didn't make the instructions clear enough!
Due to the speed of the progress with Phase Two, I will try and expedite Phase Three, I will work on it over the weekend. The majority of people have now completed it. Phases will get a bit harder in future but I will be as detailed as possible. They are ordered by difficulty.

Python27 is bundled with OS X and all linux distributions I know about, but v3.2 certainly isn't. People would have to install that themselves WITHOUT replacing or removing the 2.7 install <- that's important.

For mac, there's an installer, so it's as simple as any other app. For linux, most distros will be able to do it all for you though Yast (or equivalent package manager). Else get the tarball and do it manually ... something like (with or without sudo for the last step, depending on permissions):
Code:
./configure
make
make altinstall
In both OS X and linux 'python' in the terminal will still open 2.7 even after installing 3.2, so 'python3' or 'python3.2' is the way forward.

Thank you so much for pointing this out, I have now edited the post and added an announcement to the OP.

I've completed Phase 2.

Can I suggest you edit your OP and add each "Phase" or Tutorial to it so that it's all in one place? Labeling it appropriately as we go along.

I'm not quite sure what you mean, I already have a list of each phase with a description. Do you want me to hyperlink to the corresponding post?
 
Last edited:
Ok, Just a status update, I was busy all Saturday driving around Derbyshire (looking at the awesome mountains and scenery) and spent Sunday working on my home server. I am free Monday, so I will have to sort out Phase Three then.

I know you all are anxious to recieve it :D Like I said, this will be quite a slow thread and this is a fairly long term project.

I have amended the OP with all new participant's and fixed various status's :)

Just wondering why we are using aptana and not a small install
Like geany?? Im currently using geany but have installed aptana to follow the tutorials but whats the main difference ?, benefit ?
I prefer the layout of geany, seems less cluttered

Your free to use whatever text editor you like, but you will have to run the python files manually from a terminal instead of pressing the pretty run button on the usual aptana. For most, a big program like Aptana is very intuitive to use and can detect and correct many common typo's.
 
Last edited:
Can I join in still ?
What is routine if we run into trouble ?

I know how to write basic apps in Java, C# and C++, however really only simple things, I struggle with anything that has more than a couple of Classes...

I have edited python code in the past for a game ( star trek bridge commander), but it's been ages and I can barely remember anything. Would love to learn the language now, especially considering I'll probably order 2 or 3 raspberry's when I get the chance :p ( one as a download and file server, one for media use with XBMC, and one perhaps for fun/experiments).

Consider me joining from phase 4 :).

Yes, you can join in.

If someone runs into trouble, just reply to this thread and I or someone else will be happy to help.

Nice, you can offer advice during phase 3.

Phase two complete.

Also you've missed me off your participant list ;)

I have added you now.
 
FYI, I am working on Phase 3 now.

Yup, so you've got 3.2 installed correctly.

Try and configure it manually:
Interpreter - Python -> New ->
Path as: /Library/Frameworks/Python.framework/Versions/3.2/Resources/Python.app/Contents/MacOS/Python
The part after 'Python.app' is necessary, but you won't be able to get to it through the file selection window that comes up, as it's within the Python package contents. Append it yourself manually.

From there, it should auto-detect the appropriate imports. If not, then post back.

PS - I use Eclipse, from which Aptana is based, so I assume the menus are the same ???

Yes, they are almost identical but Aptana has been tweaked for Python programming, so it comes with all the needed plugins etc.

Ok, I have found interpreter-Python, and then I click 'NEW' It then asks me for an Interpreter name, and an Interpreter executable? Am I on the right tracks?
Thanks

Just put anything you like for interpreter name such as "Python 3.2" and then browse to the path A.N.Other mentioned. And yes, you are on the right tracks.

Count me in please OP? Skype is: rexehuk

Have you done Phase Two? I have added myself to your Skype.
 
Last edited:
when it's ready I would imagine ;)

Pretty much bang on, I don't want to release a half baked tutorial. I have been very busy in the past fortnight and I have not had much time to work on it. Its in a thread format so I (and others) can fit it around our lives. Some uni deadlines have passed and things seem to be settling down now and I am writing it up now. If all goes well, should be out the door in a couple days. I am committed to this thread and I have not abandoned it.

I have managed to get it planned out in my head so its easy to follow.
 
Last edited:
Sorry for delay but PHASE THREE IS FINALLY HERE!

Before we can start, I need to explain Python's unusual but very clean way of doing things. It is one of the reasons I love this language. Whenever you encounter an if, for, while, class, or any statement that has a colon : at the end of it; you need to indent the following code that belongs to it with four spaces.

Pressing the Tab key in Aptana will do this for you automatically, and sometimes its really clever and adds in the indent while you are typing out the code, so keep a sharp eye on the screen while you are typing your code.
Another thing you need to keep an eye out for when writing code is the syntax highlighting, syntax highlighting is the pretty colouring in the screenshots below; if the highlighting looks a bit out of whack then you have probably made a typo.

The following screenshots should make this pretty self explanatory (I will explain what if, for, while, class and so on does later):
The little dots (
dots.jpg
) represent spaces and the funny characters at the end (
enter.jpg
) means I pressed the Enter key. You will see these throughout this Phase.

untitled.jpg


Heres the same example written in PHP instead of Python, notice how it uses these { curly braces } to denotate blocks of code. This style of code is common in many other languages including C, C++, C#, Java, Action Script, and so on. These languages do not depend on indentation since the computer can easily find the opening and closing curly braces, so in the wrong hands the code can easily end up looking a utter mess :(

untitled_2.jpg


Another gotcha is that Python is case sensitive, so try to keep things lowercase to avoid any confusion (this applies to many other languages too!).

I have written 8 examples for you to get your head around, they are ordered by difficulty. If you get stuck (perhaps because I didn't explain it good enough!), post a reply.
As a last resort, reveal the spoilers to see how the example works.

1. IF Statements:

If statements are how computers make decisions.
a) Make a new (appropriately named) python module using the instructions from Phase Two.
b) Type this code out in that Module.
c) Run it.
d) Cross examine the code and the output.
e) Modify the code and see if it alters the results.

untitled_3.jpg


(According to line numbers on screenshot)
Line 7-9: Set the numbers used in example.
Line 11-12: If a (5) is equal to c (5), output the result.
Line 14-15: If a (5) is less than b (10), output the result.
Line 17-18: If b (10) is more than a (5), output the result.
Line 20-21: If b (10) is more than or equal to a (5), output the result.
Line 23-26: If a (5) is more than or equal to b (10), output the result. If that is not true, it will output the opposite result!

2. Assigning Variables:

No spoilers on this one, its pure explanation.
Line 7: I define a integer. An integer is a whole number like 1, 2, 3. A similar data type is float which is 3.14, 7.68, 1.23, etc.
Line 9: I define a string, which can be a mix or letters and numbers. If you put a number in a string, you cannot do mathematics with it.
Line 11: I concatenate
(join strings together) 3 strings. First I start with "hi", then I add the number 3 to it (I use a function called str to convert the integer to a string), then I add "there" to the end of it. You can see the result by typing in the example.
Line 13: I define another integer.
Line 15-17: I display the resulting strings and integers and display their data types.
Line 18: I do some basic maths on a and d.

Python is clever, it automatically figures out the correct data type behind the scenes.

untitled_4.jpg


3. Loops (For and While Statements):

Some tasks can be repetitive and you may want to do the same thing a number of times. There is an easier way than typing the same code out several times.

There are two statements to solve this problem, for and while. A while loop is usually used when you are not 100% sure when the end of the loop
will arrive, a for loop does the opposite and usually finishes after a pre-determined amount of iterations.
While means: While this is true, loop round and round, its a bit like an if statement that's repeated over and over. For means: for a_new_variable_containing_current_item in a list of items, I use the range function to create that list of items in the example below.

a) Make a new (appropriately named) python module using the instructions from Phase Two.
b) Type this code out in that Module.
c) Run it.
d) Cross examine the code and the output.
e) Modify the code and see if it alters the results.
f) You might want the spoiler on this one if its hard to understand.

Line 8-9: Loop through numbers 0-9, All programming languages are 0 based, so confusingly 10 means 0-9.
Line 12-13: Same thing but loop from 4-9, since you told it to start from 4, it actually starts from 4.
Line 17-18: Loop through a bunch of items in a list and print out each item. The variable i is created and it is set to the current item on each iteration.
Line 21-24: Emulate the above for loops with a while loop. I define a variable as 1, and then on line 22; I have added a condition that will stop the loop if i exceeds 10. On line 23, I increment i by one on every iteration and print it out on line 24.
Line 27-32: Make a infinite loop, I do this by permanently setting the condition to True on the while loop (Line 28). Doing this will mean that every time it loops, it will always find a positive result no matter what causing it to loop forever. To save CPU and your own sanity (since abusing this can cause the program to freeze up!), I provided a way to escape the loop on lines 31-32.

untitled_5.jpg


4. Lists:

Lists have many uses, and they are pretty much what they sound like, a list of stuff. They can contain absolutely anything!

For example,
a) You can loop through them using a for loop, as demonstrated below.
b) You can convert a string to a list, so the list will look like ['h', 'e', 'l', 'l', 'o] and then splice that list to get ['e', 'l', 'l] for example. I will show an example of this in Phase Three Point Five.
c) You can pick out individual items from the list, as demonstrated below.

OK, lets begin:
a) Make a new (appropriately named) python module using the instructions from Phase Two.
b) Type this code out in that Module.
c) Run it.
d) Cross examine the code and the output.
e) Modify the code and see if it alters the results. Try expanding it by manipulating l2.
f) A quick note: List indexes are used to specify what item you would like to retrieve, they start from 0, 0 being the first item, -1 being the last.
g) No spoiler, I will just explain it! See below:

Line 7: This is a list containing 5 items, 3 integers and 2 strings.
Line 8: I said you could put anything in a list, so what I have done is put 3 lists within a list. Each list within that list contains 2 items. A nested list!
Line 9: I use list index 0 to get the first item and then I print it.
Line 11: I use list index -1 to get the last item and then I print it.
Line 13: I do the same thing as before but I manually find the last item.
Line 15: We have already done for loops, I loop through each item in the list.
Line 18: This prints out the entire contents of l2, you should play with this, see if you can pick out an individual item!

untitled_6.jpg


5. Dictionaries:

In a real dictionary, You have words and definitions. Every word in the dictionary has to be unique, but if two words have the same definition that would not matter too much.
Python has something similar to this, the "words" are called keys, and the "definitions" are called values.
It is used for mapping, just like how you map keyboard keys in a video game. You could use it as a basic login system for example, with the key being the user name and the value being the password.
They have tons of other uses and you will inevitably need one.

Lines 7-11 does roughly the same thing as lines 14-15 but lines 7-11 is built up, bit by bit.

OK, lets begin:
a) Make a new (appropriately named) python module using the instructions from Phase Two.
b) Type this code out in that Module.
c) Run it.
d) Cross examine the code and the output.
e) Modify the code and see if it alters the results.
f) No spoiler, I will just explain it! See below:

Line 7: Defines a empty dictionary.
Line 8: Define a new key called "hello" and give it a value "world"
Line 9: Define a new key called "world" and give it a value "hi"
Line 10: Modify the value of the key "hello" and give it a new value "there"
Line 14: Be a cheater and just define the whole lot at once :-(

untitled_7.jpg


6. Simple Functions:

If you have invented a complicated maths formula and you want to use it over and over, in lots of parts of your code, you will need a function.
Otherwise you will be typing the same long winded code several times. Plus it just makes your code look cleaner and more structured.

A function is a lot like a hungry human, we have input (mouth), processing (stomach), output (backside).

To define a function, we use def as shown below. It has a colon on the end so you have to indent it like a for statement.
You can give them any arbitrary name, don't use spaces though (use underscores).

The input of a function is the (what) at the end of Line 7. Input is optional, as you will find out later.
The processing is "Hello %s" % what on line 8.
To output what I processed, I use a return statement on line 8.
To keep the code short; I put the processing and output on the same line, you can of course put the result in a new variable if you have longer code.

untitled_9.jpg


7. Nested Functions:

This one is just silly, its rare to see this actually being done in the real world. I just made it to show you how flexible Python can be.

I have nested a function within another function, the structure of the code looks like this:
--> Function hello_world()
-->--> Function hello()
-->--> Function world()
Due to hello() and world() being nested, it is not possible to access hello() and world() from line 14, these functions are only valid from within hello_world() which is lines 8-12.
This is due to something called "scope", which you can research as you become a more experienced programmer.

Now to run through the code (as usual try it, etc):
Line 7: Define a new function called hello_world
Line 8-9: Define a function within hello_world called world, make the function output a string called "World"
Line 10-11: Define a function within hello_world called hello, make the function output a string called "Hello"
Line 12: Combine the results from functions hello() and world(), and use them as hello_world()'s result.


untitled_10.jpg


8. Classes:
This has to be the hardest thing for me to explain, I have left the worst till last. When you build a very large program you will want to make it more modular and group lots of functions together, this is where classes come in. Its like a container for functions and variables.

One use could be to define the attributes of a car, you could have a variable that's contained within the class to define its horsepower and functions to get and set that value. You could also define other stuff about the car such as its wheel and engine size, all contained from within the same class.

So the basic features of a class are:
a) A constructor function which sets some initial/setup data to prepare the class for use. This is lines 8-9 on the example below.
b) They usually have some sort of variable stored within them, I have self.data, again this is first used on line 9 on the example below.
c) A class is sometimes created to make something tedious easier to use, and it will have many functions related to solving this problem.

Run and read the code below and see if you can make any sense from its results. I will explain in detail and there is no spoiler below due to it being a tough one.

Line 7: I define a new class called mycls
Line 8-9: This is a special function called a constructor which I described earlier. It sets the initial value of a class variable called "data" (a variable which is stored inside the class). The actual initial value is passed in on line 15, but this function is needed to process it.
Line 10-11: This is a function called "get_data" which outputs the current value of the class variable "data".
Line 11-12: This is a function called "set_data" which sets the current value of the class variable "data".
Line 15: I have now finished defining the class and we need to do some testing on it. On this line I initialise the class and set its class variable to "hello world".
Line 16: I pull out the value of the variable "data" which is stored within the class. We set this on line 15.
Line 17: We give the class variable "data" a new value, "bye"
Line 18: I pull out the value of the variable "data" which is stored within the class.

If you get stuck, you should post a reply to this thread.

untitled_11.jpg


A sharp eye will notice that there are some notable Python features that I have left out in this phase, and I may do a Phase Three Point Five which could explain some of the more advanced capabilities of Python, they can be fairly difficult to explain but they are very useful as your capabilities evolve as a Python Programmer.

Let me know when you have completed this phase (Post a reply!)
 
Last edited:
Furthermore, I have also updated the member list. I have to do it manually and it can be a big job when I am behind with it (~15 minutes work). If you see any errors on the member list, please let me know.

Also, If you inevitably get stuck on Phase Three; don't bash your head on your desk; just post back here and someone will give you some help.

Can I sign up? I know I'm late :(

Completed phase 2 though, will do phase 3 soon :)

I have added you.
 
Last edited:
I would be interested in feedback on how Phase Three is written, I am not very good at writing long essays and stuff. So I might need a few pointers on how to clean it up. I will now send out trust messages to people that have expressed interest when a new phase is out (seems to be a good idea since its working!).
 
Last edited:
instead of sending trust messages, why don't people just subscribe to the thread? it'd save Rich extra work at the end of the day :)

That would be ideal, I should also leave it a few days before sending out trust messages, to make sure that people are coming back to check the threads progress. I cant send trust messages to everyone, since not everyone has it turned on.

Also, a bit of news. I have added some more formatting to phase three, it should be a bit easier to read now.
 
Last edited:
Everyone! Can you please also try out Phase Three? If you have done it, post a reply here! See post #107 above.

Phase Three Point Five and Four will be coming soon!

Hi Rich,

Is it too late to register my interest? Would love to participate.

Didn't even know python existed until the weekend just gone. Udacity was featured on BBC Click so I joined. Working thru the modules.

No other coding experience apart from crude HTML and BASIC when I was at school about 20 years ago!

Cheers,

FB..

Ah good, I was hoping someone would spot this thread! Its been quiet since Phase Three came out.
No, its not too late. I accept newcomers at any time.
I have added you to the OP. This thread is perfect for complete beginners.

Too late to join? No programming exp here but really need to start doing something constructive with my spare time!
Phase Two completed

As before, I accept newcomers at any time. I have added you to the OP.
 
Last edited:
Back
Top Bottom