Sunday, 9 September 2012

A guide to your first scripts in python

Hello and welcome to my first blog-worthy post. In this post you will learn how to make simple python scripts to run on the command line. So lets get started - lets download python. You can find the download for python here. Find the download applicable to your opperating system then download and install.

So now, you should have python sitting right at the root directory of your C drive. To start writing scripts you have to use a script editor (you can use notepad if you want to). I suggest you use the official python editor, IDLE, which comes packedged with python. To launch IDLE you can search for it on your computer or by going into the recently installed programs.

So now you're ready to type some code! In IDLE, go to 'File' then 'New Window' then another window should appear. To make your first srcipt type in:

print "Hello, World"

To run this script go to 'Run' then 'Run Module' (it will then promt you to save).
NOTE: Remember to add '.py' onto the end of your filename.
The output should be somthing like this:

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
Hello, World
>>> 

If you have that written on the window of the python shell then thats a 'Well Done' from me! So say we want to make it do somthing, like say hello to people, we will have to get input. To achive this you will have to use the 'raw_input()' function
like this:

name = raw_input("Whats your name? ")
print "Hello, " + name

if you run that you should end up with somthing a little like this:

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
Whats your name? Monty
Hello, Monty
>>> 

The only problem with that is, that it could be more user-friendly if the input was on the next line. To do this all you have to do is to get input on the next line like this:

print "Whats your name?"
name = raw_input("")
print "Hello, " + name

Then, like magic, the output is:

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
Whats your name?
Monty
Hello, Monty
>>> 

So whats the next step? Different people say different things; I say lets learn to Read and Write files. So, to do that we have to use the 'open()' function. Lets make a file reader which displays text from a file.
Take a look at the code, below:

file = open("file.txt", "r")
text = file.read()
print text

So lets test it out! Make a file in the same folder as the python program called 'file.txt' and fill it with whatever text you want!. In my case, thats a shopping list. Take a look at the result here:

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
Shopping List:

Eggs
Milk
Butter
Bread
Juice
Fruit
>>> 

Now, what if you want to read from a url? you have to use urllib2. Have a look at the code below:

from urllib2 import urlopen
list = urlopen("http://montyanderson1.coolpage.biz/shoppinglist.txt").read()
print list

No comments:

Post a Comment