Sorry should have phrased that better. I've not looked at the market for a price since the Pi release when the only place to get them was ebay and they were going for £300+. I presume they are fully stocked now though?
Sorry should have phrased that better. I've not looked at the market for a price since the Pi release when the only place to get them was ebay and they were going for £300+. I presume they are fully stocked now though?
Count me in too please, I'm probably about phase 5 already but maybe only 4. I have written only short pieces of code in python and have no experience in any other language...
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 (
) represent spaces and the funny characters at the end (
) means I pressed the Enter key. You will see these throughout this Phase.
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
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.
(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.
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.
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!
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
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.
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.
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.
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!)
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.
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!).
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.
I did phase two at work on my linux machine, I am not at home on my mac. As I am running osx Lion, which of these should I be running?
Mac OS X 64-bit/32-bit Installer (3.2.3) for Mac OS X 10.6 and 10.7 [2] (sig). [You may need an updated Tcl/Tk install to run IDLE or use Tkinter, see note 2 for instructions.]
or
Mac OS X 32-bit i386/PPC Installer (3.2.3) for OS X 10.3 through 10.6 [2] (sig)
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.