Hey Hey Everyone,

So you want to learn Python.... Well so do I. I'm writing this tutorial as a learning aid for both you and me. I'm going to pass on describing Python to you and instead let Webopedia do it for me.

Source: Webopedia Term: Python

An interpreted, object-oriented programming language developed by Guido van Rossum. The name comes from one of van Rossum's favorite television shows, Monty Python's Flying Circus. Python is very portable since Python interpreters are available for most operating system platforms. Although Python is copyrighted, the source code is open source, and unlike GNU software, it can be commercially re-sold.
For those of you who do not have a Python interpreter, they are available on the net for download

Python.org Download Page
ActivePython from ActiveState

Now for the tutorial.

I'll try and make this as OS inspecific as possible. All these walk-throughs assume you have installed Python already.

Let's start with the most basic of programs. The obvious Hello World! Program.

********************
Step-by-Step Process
********************
1. Open your favourite editor (Vi, Pico, Notepad, Wordpad, Textpad, DOS Edit).
2. Type
Code:
print "Hello World!"
3. Save the script as helloworld.py
4. Open a command prompt and type python helloworld.py
5. Voila! Hello World! is displayed on the screen.

This simply demonstrates the print command in Python. Now let's look at variables and user input.

********************
Step-by-Step Process
********************
1. Open your favourite editor (Vi, Pico, Notepad, Wordpad, Textpad, DOS Edit).
2. Type
Code:
age = input("What is your age: ")
	print "So you are",age,"?"
3. Save the script as age.py
4. Open a command prompt and type python age.py
5. You will see an prompt which says What is your age:. Enter your age and press return. You will now see So you are 21 ?.

You will notice you do not have to declare variables in Python, simply reference them. In this example you also see how the input command is used. To input a value simply define a variable name for the value and then set it equal to the input command. The structure of the input command is
Code:
input("Question to prompt with")
. You also see how to output more than one string of data, using the , to concatenate the strings. However use of the comma also automatically inserts a space. You MUST remember that input only works with numbers

Now we will attempt a simple text calculator with 4 commands (add, subtract, multiply and divide).

********************
Step-by-Step Process
********************
1. Open your favourite editor (Vi, Pico, Notepad, Wordpad, Textpad, DOS Edit).
2. Type
Code:
calc = raw_input("Which function would you like to execute - [a]dd, [s]ubtract, [d]ivide, [m]ultiply: ")
firstnum = input("Enter the first number: ")
secondnum = input("Enter the second number: ")
if calc == "a" : print firstnum,"+",secondnum,"=",firstnum+secondnum
elif calc == "s" : print firstnum,"-",secondnum,"=",firstnum-secondnum
elif calc == "d" : print firstnum,"/",secondnum,"=",firstnum/secondnum
elif calc == "m" : print firstnum,"*",secondnum,"=",firstnum*secondnum
else : print "Error: Unknown Function Entered"
3. Save the script as calc.py
4. Open a command prompt and type python calc.py
5. You will see a prompt which says Which function would you like to execute - [a]dd, [s]ubtract, [d]ivide, [m]ultiply: . For the sake of demonstration enter a and press enter. You will then see Enter the first number: . Enter 5 and press enter. Now Enter the second number: " appears. Enter 7 and hit enter. The screen will now display 5 + 7 = 12. Feel free to experiment with other functions and numbers now.

You have now been introduced to the command raw_input. It fuctions the same way as input but is used for alphanumeric entries. You are also introduced to basic mathematical functions in python (+,-,/,*) and if statements. The syntax for an if statement is
Code:
 if (statement) : <code>
elif (statement) : <code>
else : <code>
One last example. We will attempt to incorporate a basic introduction to sockets into this example. *Crosses his fingers*

********************
Step-by-Step Process
********************
1. Open your favourite editor (Vi, Pico, Notepad, Wordpad, Textpad, DOS Edit).
2. Type
Code:
from socket import *
s= socket(AF_INET, SOCK_STREAM)
yourip = raw_input("Enter your IP Address: ")
yourport = input("Enter a Port Number to Listen on: ")
s.bind((yourip, yourport))
s.listen(1)
(incomingport, incomingaddress) = s.accept()
print "Connection Attempt from", incomingaddress
s.close
3. Save the script as server.py
4. Open a command prompt and type python server.py
5. You will see a prompt which reads Enter your IP Address: . Do as it says and press enter. You will then see a prompt which says Enter a Port Number to Listen on: . Answer the question and press enter. Telnet to the port you entered and wait a few seconds. Close the session and return to your python window. You will see the text Connection Attempt from <your ip, port used>.

In this example we played with sockets a little bit. The first task was to include the socket module. We did this using the statement
Code:
from socket import *
. We then defined the type of socket (AF_INET, SOCK_STEAM) and created a variable for the socket to be attached to (s). We could then issue our commands based on the varible s. We bound the socket to the ip and port you had previously entered with the bind command and then started listening on that port. Upon a connection attempt we accepted the connection, printed the intruders IP to the screen and closed the socket.


Anyways... Hope you find this helpful. I'll definately add another one if people want it, probably before the end of the weekend, as I have no plans. Anyways That's all for now, maybe more to come.


Peace

HT

PS - I have proofread this, but I'm sure i've missed stuff. If you notice anything please PM so I can fix it... Thanks.