First you are going to need to go and get Python 2.7 (which is the current latest version for V2) [ LINK ]. Im not sure what operating system you are using (Im on linux) but this guide will work for all systems supported by python. So, download and install python and then open up the command prompt:
- Windows: Start -> Run -> Type "cmd"
- Linux: Applications -> Accessories -> Terminal
- Mac: I have no clue >> not a mac person
Running an application
To run an application in windows, it is as simple as browsing to the directory where the script is located (navigation is done by typing "cd <path>") and just typing out the file name.
In Linux there is a few ways to do this. You type "python <file name>". If want another way you can include this line "#!/usr/bin/python" at the top of your file, allow the program to be executed as a program (type "chmod +x <file name>") then you can just type the file name "./<file name>"
Just an FYI to you mac users out there, I'm gonna stop talking about it because i REALLY have no idea how the mac system is layed out or how to program in one xP so the best of luck to you and just keep reading; there shouldn't be that much of a difference.
Choosing your editor
When i program on Linux, i just use the built-in editor gedit; it has nice syntax highlighting and formats the text nicely (Although i do change the tab width from 8, to 4)
On windows, i use Notepad++, it is just like gedit, but has a lot more functions (such as macros... and more), you can get it here [ LINK ]
Before we actually start to code, you should know that python is whitespace sensitive. In most languages you define a block of code by surrounding it by brackets or braces. In python you increase and decrease the tab size (spaces work too).
Creating your first program
Now, open up your editor. Lets create the hello world program (as is customary when learning any new language)
print "Hello World"
Thats it; here is the break down of the code:
print - Print is a built in method that prints what ever is following to the terminal
"Hello world" - is a string, in between the quotes is defined as a string
Sounds easy enough right Lets try another way of printing to the screen
There are a few ways to print to the screen, this method prints without a new line at the end... if we wanted to add a new line we would add that at the end ( "Hello World\n" )
There are a few different ways that you can create a loop, but before that lets define what a loop is (and this is not a Websters definition, this is my own way of describing it xP). A loop is a quick and simple way of repeating a set number of tasks over and over again.
Here are a few sample loops, a # followed by text is a single lined comment, while ''' is a multi-line comment, you close it off with another '''
#A simple for loop with numbers
for x in range(5): #range(5) creates a list starting at 0, and ending at 4 (it has 5 items in it): > [0, 1, 2, 3, 4]
print x #We indent one so that we let python know this is part of the for block
#remove the indent, code after this is considered outside the block
print #prints a new line
#A simple loop using the list type
myList = ["one", "two", "three"] #This is how we define a variable
for listItem in myList:
print listItem #will go through each item in myList, and print the item
#Lets do a while loop
x = 0 #x is assigned a starting value of zero, it is an integer type
while x < 10: #We will continue this loop aslong as x has a value less than 10
x += 1 #Will add 1 to x each time it is called
Python has a very extensive library and it would be impossible for me to go over all of them in one simple tutorial. For a list of them all visit the python libary page [ LINK ], i will give examples on how to use a few that might be popular in this forum, but other than that just ask for clarification and i can try to do my best
This libary is used for opening web pages and other brickabrack... heres some simple examples on how to use it.
response = urllib2.open("http://www.google.com") #Opens the webpage
rawHtml = response.read() #Gets the html data
f = open('google.html', 'w') #Creates a new file called 'google.html'
f.write(rawHtml) #Writes the html data to the file
f.close() #Closes the file
If you want to know how to log into sites, save cookies and such, you'll have to wait... It is a MUCH harder process to do, it involves creating classes and functions. Not really suitable for this tutorial
Im not really sure what else i can put in here to help you guys out... based on the comments i receive ill continue to edit this file and update it. I guess this is a good starting point to get you use to the language...
Edited by Pyro699, 07 October 2010 - 10:28 PM.