Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 20th, 2005, 4:57 AM   #1
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 3 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
Post [tutorial] Python for programming beginners

--=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:
 print 3

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:
print 4 + 5
Output:

Example:
print 8 / 2
Output:

Example:
print 6 * 6
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:
my_variable = 2

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:
my_variable = 5

Here we set it to 5.

We can print the variable.

Example:
print my_variable
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:
Hello!

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:
Number: 98
Bigger than 5
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:
Number: 5
Number is 5

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:
["Bertie", "Bob", "Joe"]

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:
["Mac", "Unix", "Linux"]

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
__________________
Join us at #programmingforums @ irc.freenode.net!

My software never has bugs. It just develops random features.
coldDeath is offline   Reply With Quote
Old Nov 20th, 2005, 5:38 AM   #2
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 3 Polyphemus_ is on a distinguished road
Very nice tutorial
Polyphemus_ is offline   Reply With Quote
Old Nov 20th, 2005, 5:57 AM   #3
ivan
Professional Programmer
 
ivan's Avatar
 
Join Date: Sep 2005
Location: serbia & montenegro
Posts: 482
Rep Power: 3 ivan is on a distinguished road
congratulations on writing that tutorial!
ivan is offline   Reply With Quote
Old Nov 20th, 2005, 7:55 AM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 8 Ooble is on a distinguished road
Looks pretty cool. I suggest you go into strings a little more though - things like slicing are so damn useful.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old Nov 21st, 2005, 10:31 PM   #5
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to Kilo
for some reason i feel pursuaded to use python for a few small ideas i have
__________________
"When in Rome, Do as the Romans Do"
"Beauty is in the eye of the BEER holder"
"Save your breath your going to need it for your blow up doll later"

SearchLores.org
Kilo is offline   Reply With Quote
Old Nov 28th, 2005, 12:47 PM   #6
HaCkeR
Hobbyist Programmer
 
HaCkeR's Avatar
 
Join Date: Nov 2005
Location: UK
Posts: 131
Rep Power: 0 HaCkeR is an unknown quantity at this point
Send a message via AIM to HaCkeR Send a message via MSN to HaCkeR
Talking

Yea nice tutorial.
all thought im new (very 5th day programming) i picked up an error in your
while loop.
coldDeath's
#This prints the numbers 1 - 10
x = 1
while (x < 11):
	print x
this puts you in an infinite loop put this instead
#This prints the numbers 1 - 10
x = 0
while (x < 10):
    x = x + 1
    print x
this will print the number 1 - 10 not get you stuck in a loop.

sorry cold i do pick out these things.

but nice tut
HaCkeR is offline   Reply With Quote
Old Nov 28th, 2005, 12:59 PM   #7
coldDeath
Expert Programmer
 
coldDeath's Avatar
 
Join Date: Aug 2005
Location: UK
Posts: 862
Rep Power: 3 coldDeath is on a distinguished road
Send a message via AIM to coldDeath Send a message via Yahoo to coldDeath
Good stuff Hacker

#This prints the numbers 1 - 10
x = 0
while (x < 10):
    x += 1
    print x

You can use x+=1 instead of x=x+1 : )
__________________
Join us at #programmingforums @ irc.freenode.net!

My software never has bugs. It just develops random features.
coldDeath is offline   Reply With Quote
Old Nov 29th, 2005, 5:32 AM   #8
HaCkeR
Hobbyist Programmer
 
HaCkeR's Avatar
 
Join Date: Nov 2005
Location: UK
Posts: 131
Rep Power: 0 HaCkeR is an unknown quantity at this point
Send a message via AIM to HaCkeR Send a message via MSN to HaCkeR
i guess you could use that but i prefer other way
HaCkeR is offline   Reply With Quote
Old Nov 30th, 2005, 1:46 PM   #9
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile

At a boy hacker!

Readability is more important then obfuscation!
If you want to muddle your code, switch to Perl!
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Nov 30th, 2005, 4:17 PM   #10
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 8 Ooble is on a distinguished road
Yeah, it's kinda useless now, but what if your variable's called sqrtLineCount or something?
sqrtLineCount = sqrtLineCount + 1
sqrtLineCount += 1
Which do you prefer?
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:25 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC