--=Python Beginners Tutorial=--
[NOTE]This tutorial will only teach the very basics[/NOTE]
Introduction
In this tutorial I assume you are new to computer programming. I will attempt to teach you the basics of a language called Python. I also assume you will be using Windows or a Unix like operating system.
Firstly we will start by finding out what Python is.
It is:
+ A scripting language (often compared to Perl, Scheme and TCL)
+ It is interpreted
+ It is Object Oriented
+ It is multi-platform and portable (it will run on more than one operating system eg: Windows, *nix, Mac, Amiga etc.)
+ It has clear easy to read syntax
+ It has a lot of extra modules to help you perform tasks (for example the PyGame module allows you to create games)
+ It is very powerful yet useable
+ It has many GUI (Graphical User Interface) toolkit, such as Tkinter (standard Python GUI toolkit, ugly looking and not very nice to code), PyQt (You have to pay to use Qt on Windows, but this will change in Qt4), PyGTK, WxPython (great multiplatform toolkit) etc.
These all add together to make the great language it is today.
Some Python facts:
Python was created by Guido Von Rossum in 1991.
It was based on an interpreted language called ABC.
Its name comes from Guidos favorite show, "Monty Pythons Flying Circus".
The Website:
http://python.org is the official python website and it contains many useful bits and bob, such as a list of existing modules, documentation, essays from Guido, downloads, FAQs and various guides and tutorials.
Getting Python
Python is free! great!
It can run on any operating system that has a C compiler!
Head over to
http://python.org/2.4.2/ to check out the download page.
For Windows users:
Download Python-2.4.2.msi from that page. Download link:
http://python.org/ftp/python/2.4.2/python-2.4.2.msi
You can just run the step by step installer and it will install itself easily!
Once the installation is complete you should see Python on your start menu, if you are used to Windows and you like GUI programs, you can use IDLE which is in the Python section of your start menu. You can also use the MS-DOS prompt and your favorite editor (vi) to do this. To run a Python from the MS-DOS prompt, type "python programname.py".
For *nix users:
Python usually comes with most Linux distributions and other Unix like operating systems. You can check if it is installed by opening a terminal window and typing "python" if a little interpretor pops up with a ">>" prompt, then congrats, you have Python installed, press CTRL+D or CTRL+Z to close it.
If you don't have it installed, you can download python-2.4.2.tgz or python-2.4.2.tar.bz2. Download links:
http://python.org/ftp/python/2.4.2/Python-2.4.2.tgz and
http://python.org/ftp/python/2.4.2/Python-2.4.2.tar.bz2
Write your programs in your favorite editor (vi) and save them with the .py extension. You can run them with the command "python progname.py" or you can add "#! /usr/bin/python" to the top line of your python programs and make them executable with "chmod +x progname.py" and then run them with "./progname.py".
The Tutorial
Comments
Comments are lines of code ignored by python, they are useful for writing notes about your code and explaining different parts of your code.
A comment starts with a "#" and continues until the end of the line.
Example:
# I am a comment and I'm ignored by Python
I am not a comment because I didn't start with a "#"
It is good coding practice to put this comment in all of your python code:
Name:
Author:
Date:
Description:
Comment:
If I had made a little program I would include something like this:
Name: Hello World program
Author: ColdDeath
Date: 20/11/2005
Description: A program to display the text "Hello world!"
Comment: Works on all operating systems and versions of Python
I'm not going to include it in every piece of code I show you, but I expect you to put this little "Header Comment" in your files.
Print
The print command can be used to display text on the screen.
Here is an example:
It outputs:
So it is a basic command to display whatever text is after the word "print".
Basic operators
We can use some mathematical functions on numbers in python, here i will explain some basic ones.
We can use +, -, *, / they are: plus, minus, multiply and divide.
Example:
Output:
Example:
Output:
Example:
Output:
Variables and types
Variables are a key concept to programming. Think of them as a name tag to a certain piece of data.
Right I'll show you some code:
Here we set a variable. It is called my_variable and it is equal to the number 2.
Variable names can contain letters, numbers and underscores (_).
We can change what the variable is by just overwriting it:
Here we set it to 5.
We can print the variable.
Example:
Output:
Mathematical operators can be used on them as well.
Example:
number_1 = 6
number_2 = 2
print number_1 + number_2
Output:
OK so now we know how to use variables and numbers. Now lets look at strings.
Strings are basically words. Strings have single or double quote marks around them so that you can tell they are a string.
Example:
my_string = "Hello!"
print my_string
Output:
OK that is easy enough, there are a lot more details about strings and numbers and if i went into them now it would take too long.
User input
So now we can make variables and we can print strings and numbers, we can also do basic mathematical functions to numbers. Now lets do something a bit cooler, lets take some input from the user.
We can use input() to get input as a number, or raw_input() to get it as a string.
Example:
[code]my_variable = input("Enter a number please: ")
print "Your number multiplied by 2 is:", my_variable * 2
Output:
Enter a number please: 9
Your number multiplied by 2 is: 18
OK its looks harder than it is. Basically we take input format he user and store it as a variable. The prompt to type something in is show between the two brackets.
Syntax:
variable_name = input("Prompt for user input")
Here is an example a string:
my_string = raw_input("What is your name? ")
print "Your name is", my_string Output:
What is your name? ColdDeath
Your name is ColdDeath
The mighty if statement
If statements are very useful things. I think it is best shown in code form, so here it is:
Example:
number_1 = input("Type a number:")
if (number_1 > 5):
print "It was bigger than 5" Output:
Type a number: 8
It was bigger than 5
So basically it says : if a condition is correct continue the indented code
The conditions can contain the following:
< is less than
> is greater than
== is equal to
!= is not equal to
So we do: (variable1 (operator) variable2)
Syntax:
if (this_variable <operator> that_variable):
do this
Another Example:
name = raw_input("What is your name?")
if (name == "ColdDeath"):
print "hello ColdDeath" Output:
What is your name? ColdDeath
hello ColdDeath
Now there is something called "else" it is used if the if statement isn't correct.
Example:
num = input("Number: ")
if (num > 5):
print "Bigger than 5"
else:
print "Not bigger than 5" Output-1:
Output-2:
Number: 3
Not bigger than 5
There is also something called "elif" it means "else if", they are used when the first if statement section is incorrect.
Example:
num = input("Number: ")
if (num > 5):
print "Bigger than 5"
elif (num == 5):
print "Number is 5"
else:
print "Not bigger than 5" Output:
While loops
A loop is a continuous block of code that is repeated until something stops it. We will look into the "while loop".
Its syntax is:
while (condition):
do this
Example:
number = 1
while (number < 3): #while number is less than 3
print number #prints the number variable
number = number + 1 #number now equals itself + 1
#So number starts of as 1, and it is less than 3 so it goes through the loop and prints itself,
then it adds 1 to itself so it is now 2. Then it checks if it is less than 3, and it is so it
prints itself then adds 1, so it is now 3. It finds that it is not less than 3, so it doesn't
go through the loop again, so it "breaks" the loop
While loops are a hard concept to learn but are vital in becoming a good programmer.
Example:
#This prints the numbers 1 - 10
x = 1
while (x < 11):
print x
Lists
Lists are variables that can hold more than one piece of data.
They look like this:
name_list = ["Bertie", "Bob", "Joe"]
print name_list
Output:
We can store numbers or strings in lists.
We can add or remove items of the list.
Example:
operating_system_list = ["Windows", "Mac, "Unix"]
operating_system_list. remove("Windows")
operating_system_list.append("Linux")
print operating_system_list Output:
For Loops
For loops are vital to Python, they are used widely in a lot of programs. But the concept is hard to understand, but once you understand you will use them frequently.
Example:
name_list = ["Arevos", "Cerulean", "Sane"]
for name in name_list:
print "Hello", name
Output:
Hello Arevos
Hello Cerulean
Hello Sane
Right so lets have a look, we make a list, then we make the for loop. The syntax looks like this:
for (new variable name) in (list):
do this
Right, now concentrate!
We set the variable after the word "for"(in our case we call it "name") to the first string in the list which is "Arevos". Then we go to the next line which prints the "name" variable after the string "Hello". Then it goes to the top and sets "name" to the next item in the list, which is "Cerulean" then it prints "Hello Cerulean". Then it returns to the top and sets itself as "Sane" and prints it like the others. Then it gets to the top and realizes that there are no items left in the list so it "breaks" the loop.
OK, if you still don't understand, you can look at this:
http://www.python.org/doc/2.4.2/tut/...00000000000000 : it was written by Guido himself!
Furthermore
This is as much as I will show you in this tutorial, but there are plenty of other tutorials that continue from where I left off.
Here is a good list of them:
http://docs.python.org/tut/tut.html
http://www.devshed.com/c/b/Python
http://www.awaretek.com/tutorials.html
http://www.xml.com/pub/rg/XML_and_Python_Tutorials
http://www.programmingforums.org/fo...read.php?t=1289
http://www.bigwebmaster.com/Python/Tutorials
http://wiki.wxpython.org/index.cgi/Getting_20Started
http://www.programmingtutorials.com/python.aspx
http://www.freeprogrammingresources.com/python.html
http://www.awaretek.com/tutorials.html
http://honors.montana.edu/~jjc/easytut/easytut/
http://starship.python.net/crew/hinsen
http://www.python.org/doc/current/tut/tut.html
http://www.awaretek.com/tutorials.html
http://martin.f2o.org/python/tutorial
http://www.python.org/doc/Intros.html
http://diveintopython.org
http://www.wag.caltech.edu/home/rpm/python_course
http://www.xs4all.nl/~bsarempt/python/tutorial.html
http://www.hetland.org/python/instant-hacking.php
http://www.tutorialized.com/tutorials/Python/1
http://www.dickbaldwin.com/tocpyth.htm
http://www.cs.virginia.edu/~lab2q
http://www.uselesspython.com/tutorials.html
http://jmsoler.free.fr/didacticiel/...prog_python.htm
http://www.techiwarehouse.com/Pytho...n_Tutorial.html
http://www.freeprogrammingresources.com/python.html
[Source:
http://www.programmingforums.org/for...ead.php?t=5405 ]
The end!
Thank you for taking the time to read this tutorial, please leave a comment about it. And if you have got this far please consider learning more Python from a book or another source, as it is a truly great language. If you have any questions about Python at all, you can post in the programmingforums.org Python forums which is here:
http://www.programmingforums.org/for...splay.php?f=43 this is where one of our Python experts will help you
Good luck.
-ColdDeath