07-15-2008, 08:12 PM
This is going to be part 1 of a series. Part 2 and 3 shouldn't be out before next week so please be patient ;)
Python is a great language for new and experienced programmers. It has powerful features, an easy
syntax, and a kick ass name! I would suggest to any computer geek to learn it. It's the most
versatile and complete language you'll ever use, and you'll notice that after a couple hours of
programming.
Let's skip some theory and jump right into the juicy codes:
Yeah! That's it, a two* line program! Just to get this straight right away, the first line (i.e.
#!/usr/local/bin/python) is only needed on Unix like OSes and even then, it's not a must, simply a
convenience! For the second line, pretty easy to understand: it prints out what's in between the
quotess. Not to much to handle I hope. If it is get a book on Learning Visual Basics.. XD
Now just a couple lines of theory (Not too many, I know how much we all hate the theory)! Python is
an interpreted language. That means that when you execute a program written in python, the program
is actually a human readable text file. It the "interpreter's" job to parse your program and
execute it. To get python (for Linux or windows) check here: http://www.python.org/ ..
Now for the second super cool program:
Woow, that is some pretty leet code :)
Lets' check it out line by line:
We are makeing some variables named a and b. a = 4 and b = 5 ... Not to complicated. But then it
get's tough to read. Well, see the `print` prints out variable's values instead when they are not
in between the quotes. Meaning that in this case we should get:
Pretty cool no? Did you know that your computer was That good at math!? Yeah, me too actually. You
just got quite a couple new rules at once there! In python you can make strings (a string = a
programmers way to say a list of characters) by mixing numbers and English characters ( a, b, c, ...)!
Believe me this is a nice feature and it isn't that easy to do in every language!
Now time for a third example:
There you go.. Functions.. This is where a lot of new programmers have some problems: Understanding
functions. A function is a "sub-program". It's just a small piece of code that can be used over and
over in a program. It's actually exactly the same thing as a mathematical function ( y = mx + b )..
The functions output will depend on it's input. So in the function add_to_five(), if the input is 3
than the output is 8. If the input is 5, then the output will of course be 10.
Remember that in all programming languages, the different `input` options in functions are known as
parameters. Also that the variables inside the functions are only reachable from inside the function. That means that
I would NOT be able to add the line to the very end of the program:
print "the number is: "+number
because `number` is a local variable from the function add_to_five() and not a global variable..
This brings up a specialty of the python language: a functions code will be denoted by whitespace
(spaces or tabs) preceding that line. Python will know which line belongs to the function and
which line belongs to the main program depending on the whitespace before that line. You can use
whichever you prefer, one space, two spaces, a tab, up to you to decide but to make usable and
clean looking code use the same standard in the entire file (don't use two spaces for one function
and 3 tabs for a different function...). This is how code gets sloppy :)
So, the function add_to_five() is only made up of two lines:
If i was to write the function like this instead:
Then there would be a runtime error because python doesn't know where the function starts nor where
it ends.
Not only that but also, at the end of a functions declaration you need to put a colon ( : ). Pretty
easy to remember.
Just to reinforce what I'm trying to show you about functions here's is a python function that will
talk about you! That's right, your computer might know more about you than you think x_0
A new inside function: raw_input()!! `xyz = raw_input("Somthing ")` will promt the user "Something" and wait for the user to type something followed by enter. Whatever he/she types will be stored in the variable `xvz`.. You can add a raw_input() at the end of your python applications to stop them from opening and closing in a flash. Instead at the end of the execution of the program it will wait for the user to hit enter.
Here's another thing about functions in python: there doesn't have to be a return value. Like in
this program the function prints out lines to the screen but the function does not return a value to
the main program. In python you can return values to the main program by using the `return`
keyword. In that case the main program's output is also variable.
I know it is easy to get lost but to be honest, the only way to learn is to grab a text editor
(notepad.exe) and write a couple scripts your self.
Here are some exercises if you aren't inspired:
- write a program with a function that divides a number by two AND prints out the result in
English ( example out: 10 divided by two equals 5 )
- write a function like my tell_story() with a few more parameters and a better moral to the
story :P
Note that mathematical functions in python are pretty simple to remember:
+ : addition
- : subtraction
/ : division
* : multiplication
Now time to get serious and see some real programming logic: loops!
Loops are what saves so much of every programmers valuable time. They are the way to loop through a certain procedure
(or functions ^^ ) for example until a certain breakpoint was met. I know this sounds strange again but let's just get
dirty and jump into the examples:
That's it!?? Yep, that's all, folks.. How does it work: range() takes 2 parameters, the starting point and the ending
point + 1. That's right, the ENDING POINT + ONE..
Note that there are two types of comments in python:
-single line comments ( # you comment here )
-multi line comments ( """ your comment here """ )
Now it is going to loop through this and change it's value every
time: from 1, to 2, to 3, ... , to 10!
To make a loop from 5 to 20:
Pretty nice, more than simple enough.
Note that there are generally three types of loops in all programming languages:
FOR loops: you know from which point to which point you need
WHILE loop: As long as a condition is met, you loop
REPEAT loops: Repeat a loop until a condition is false.
The only difference between WHILE and REPEAT loops is that WHILE loops must check the condition before even starting
the loop whereas in REPEAT does the loop operation at least once then at the end checks the condition. Don't get it??
That's normal, just don't worry, you will understand when you need to :)
FOR and WHILE loops are the most important in python. To use WHILE loops, just use this syntax:
Something a bit more realistic:
This program loops through prompting the user "Enter your name: " until the user enters "japon".
Logical operations
This is another very common part of every programming language, it is even the base of boolean algebra. There are
`tests` and `operators`. A `test` is for example "if". Let me do a quick example:
Here a the list of comparison operators:
== : Is equal to
!= : Is NOT equal to (you can also use: <> )
> : Is greater than
< : Is less than
>= : Is greater than or equal to
<= : Is lesser than or equal to
So, `if name is equal to "japon"` is TRUE then the sub function afterwards is executed. Else it will not be executed.
There is one more nice little foothold before sticking it all together: If you do an "if" and it is FALSE than you can
instead run a different sub-program called : "ELSE".. I know you might be slacking now, after reading this article but
believe me, you are almost there!
Here is an example with "if" and "else":
That's how the if..else combo work. Now as the last part of this article, i'd like to show how to group conditions
together with logical operators: AND and OR. The idea is yet again simple and straight forward:
The AND and OR operators make it possible to blend together several tests at the same time.
Please comment what you liked and or didn't like, so that I know better for my future tutorials :)

Python is a great language for new and experienced programmers. It has powerful features, an easy
syntax, and a kick ass name! I would suggest to any computer geek to learn it. It's the most
versatile and complete language you'll ever use, and you'll notice that after a couple hours of
programming.
Let's skip some theory and jump right into the juicy codes:
Code:
#!/usr/local/bin/python
print "Hello, World"Yeah! That's it, a two* line program! Just to get this straight right away, the first line (i.e.
#!/usr/local/bin/python) is only needed on Unix like OSes and even then, it's not a must, simply a
convenience! For the second line, pretty easy to understand: it prints out what's in between the
quotess. Not to much to handle I hope. If it is get a book on Learning Visual Basics.. XD
Now just a couple lines of theory (Not too many, I know how much we all hate the theory)! Python is
an interpreted language. That means that when you execute a program written in python, the program
is actually a human readable text file. It the "interpreter's" job to parse your program and
execute it. To get python (for Linux or windows) check here: http://www.python.org/ ..
Now for the second super cool program:
Code:
#!/usr/local/bin/python
a = 4
b = 5
c = a + b
print "Let's do some math!"
print a + " plus " + b " equals ... " + cLets' check it out line by line:
We are makeing some variables named a and b. a = 4 and b = 5 ... Not to complicated. But then it
get's tough to read. Well, see the `print` prints out variable's values instead when they are not
in between the quotes. Meaning that in this case we should get:
Code:
Let's do some math!
4 plus 5 equals ... 9Pretty cool no? Did you know that your computer was That good at math!? Yeah, me too actually. You
just got quite a couple new rules at once there! In python you can make strings (a string = a
programmers way to say a list of characters) by mixing numbers and English characters ( a, b, c, ...)!
Believe me this is a nice feature and it isn't that easy to do in every language!
Now time for a third example:
Code:
#!/usr/local/bin/python
def add_to_five(number):
number = number + 5
return number
print add_to_five(3)
print add_to_five(5)There you go.. Functions.. This is where a lot of new programmers have some problems: Understanding
functions. A function is a "sub-program". It's just a small piece of code that can be used over and
over in a program. It's actually exactly the same thing as a mathematical function ( y = mx + b )..
The functions output will depend on it's input. So in the function add_to_five(), if the input is 3
than the output is 8. If the input is 5, then the output will of course be 10.
Remember that in all programming languages, the different `input` options in functions are known as
parameters. Also that the variables inside the functions are only reachable from inside the function. That means that
I would NOT be able to add the line to the very end of the program:
print "the number is: "+number
because `number` is a local variable from the function add_to_five() and not a global variable..
This brings up a specialty of the python language: a functions code will be denoted by whitespace
(spaces or tabs) preceding that line. Python will know which line belongs to the function and
which line belongs to the main program depending on the whitespace before that line. You can use
whichever you prefer, one space, two spaces, a tab, up to you to decide but to make usable and
clean looking code use the same standard in the entire file (don't use two spaces for one function
and 3 tabs for a different function...). This is how code gets sloppy :)
So, the function add_to_five() is only made up of two lines:
Code:
number = number + 5
return numberIf i was to write the function like this instead:
Code:
def add_to_five(number):
number = number + 5
return numberThen there would be a runtime error because python doesn't know where the function starts nor where
it ends.
Not only that but also, at the end of a functions declaration you need to put a colon ( : ). Pretty
easy to remember.
Just to reinforce what I'm trying to show you about functions here's is a python function that will
talk about you! That's right, your computer might know more about you than you think x_0
Code:
#!/usr/local/bin/python
def tell_story(uname, uage):
print uname + " is becoming a l33t programmer!"
print "by the time he is " + (uage + 5) + ", "
print "he will have already coded his own OS!"
name = raw_input("What is your name : ")
age = raw_input("How old are you : ")
tell_story(name, age)A new inside function: raw_input()!! `xyz = raw_input("Somthing ")` will promt the user "Something" and wait for the user to type something followed by enter. Whatever he/she types will be stored in the variable `xvz`.. You can add a raw_input() at the end of your python applications to stop them from opening and closing in a flash. Instead at the end of the execution of the program it will wait for the user to hit enter.
Here's another thing about functions in python: there doesn't have to be a return value. Like in
this program the function prints out lines to the screen but the function does not return a value to
the main program. In python you can return values to the main program by using the `return`
keyword. In that case the main program's output is also variable.
I know it is easy to get lost but to be honest, the only way to learn is to grab a text editor
(notepad.exe) and write a couple scripts your self.
Here are some exercises if you aren't inspired:
- write a program with a function that divides a number by two AND prints out the result in
English ( example out: 10 divided by two equals 5 )
- write a function like my tell_story() with a few more parameters and a better moral to the
story :P
Note that mathematical functions in python are pretty simple to remember:
+ : addition
- : subtraction
/ : division
* : multiplication
Now time to get serious and see some real programming logic: loops!
Loops are what saves so much of every programmers valuable time. They are the way to loop through a certain procedure
(or functions ^^ ) for example until a certain breakpoint was met. I know this sounds strange again but let's just get
dirty and jump into the examples:
Code:
#!/usr/local/bin/python
# This program will count to 10 and print everything out to the screen
# An example FOR loop:
for i in range(1, 11):
print iThat's it!?? Yep, that's all, folks.. How does it work: range() takes 2 parameters, the starting point and the ending
point + 1. That's right, the ENDING POINT + ONE..
Note that there are two types of comments in python:
-single line comments ( # you comment here )
-multi line comments ( """ your comment here """ )
Now it is going to loop through this and change it's value every
time: from 1, to 2, to 3, ... , to 10!
To make a loop from 5 to 20:
Code:
for i in range(5, 21):
# do something here x_0Pretty nice, more than simple enough.
Note that there are generally three types of loops in all programming languages:
FOR loops: you know from which point to which point you need
WHILE loop: As long as a condition is met, you loop
REPEAT loops: Repeat a loop until a condition is false.
The only difference between WHILE and REPEAT loops is that WHILE loops must check the condition before even starting
the loop whereas in REPEAT does the loop operation at least once then at the end checks the condition. Don't get it??
That's normal, just don't worry, you will understand when you need to :)
FOR and WHILE loops are the most important in python. To use WHILE loops, just use this syntax:
Code:
while (condition):
# do something cool hereSomething a bit more realistic:
Code:
finished = FALSE
while (not finished): # loops until "finished" = TRUE
name = raw_input("Enter your name : ")
if (name == "japon"):
finished = TRUEThis program loops through prompting the user "Enter your name: " until the user enters "japon".
Logical operations
This is another very common part of every programming language, it is even the base of boolean algebra. There are
`tests` and `operators`. A `test` is for example "if". Let me do a quick example:
Code:
#!/usr/local/bin/python
name = "apollo"
if (name == "japon"):
print "you are l33t"
if (name == "apollo"):
print "you are a child of japon's l33tness."Here a the list of comparison operators:
== : Is equal to
!= : Is NOT equal to (you can also use: <> )
> : Is greater than
< : Is less than
>= : Is greater than or equal to
<= : Is lesser than or equal to
So, `if name is equal to "japon"` is TRUE then the sub function afterwards is executed. Else it will not be executed.
There is one more nice little foothold before sticking it all together: If you do an "if" and it is FALSE than you can
instead run a different sub-program called : "ELSE".. I know you might be slacking now, after reading this article but
believe me, you are almost there!
Here is an example with "if" and "else":
Code:
#!/usr/local/bin/python
name = raw_input("Enter your name: ")
if (name == "japon"):
print "You are the king of the world"
else:
print "You suck"
print "Get a life"
print "^^ Just kiddin ;)"That's how the if..else combo work. Now as the last part of this article, i'd like to show how to group conditions
together with logical operators: AND and OR. The idea is yet again simple and straight forward:
Code:
#!/usr/local/bin/python
name = raw_input("Enter your name: ")
password = raw_input("Enter your password: ")
# first using an AND:
if (name == "japon") and (password == "I_PWN"):
print "correct username and password"
else:
print "you don't belong here, go away"
# now using an OR:
if (name == "chuck") or (name == "norris"):
print "You are my god ... plz don't kill me :'("The AND and OR operators make it possible to blend together several tests at the same time.
Please comment what you liked and or didn't like, so that I know better for my future tutorials :)
