Step 1 ~ Create Directory
Open a terminal window, create a project directory and change to that directory

mkdir ~/python-tutorials/ cd ~/python-tutorials/

Step 2 ~ Create Python File
Create a python file called tutorial-01.py and open it for editing. You can use vi, pico, nano, TextWrangler, whatever. We are going to edit and save, keeping the file open to continue editing. The terminal window will be used to run the file using python from the command line.

Step 3 ~ Hello World
In the file, create a function definition called hi() that prints "Hello World" to the screen.

# Step 3 def hi(): ''' function hi: no parameters ''' print( "Hello World\n") return # This runs the code hi()

Step 4 ~ Things To Note

  1. Python uses indents for program flow. So anything indented is part of the closest line not indented.
    • So print is under the def hi()
    • So is return
  2. Comments are after the hash character '#'
  3. The first line under a function is a place to put text for documentation. Use 3 tics (') and multiple lines are ok
  4. The hi() by itself on the same indentation as def actually runs the hi function. This is used so we can run the program via the command line.

Step 5 ~ Run Hello World
Save the file. And then type:

python tutorial-01.py [Enter] Hello World

Step 6 ~ Imports
Now let's open a feed, read the data and print it to the screen. First we need some functions to open external links. This is accomplished through the import statement. So at the top of our tutorial-01.py file, add the following statement which will load urlopen into our space:

# Beginning of file from urllib2 import urlopen

Step 7 ~ print_contents(url)
Now let's define a function that takes an argument, url, opens it, reads in the data and prints that data to the screen. Add this function to your code:

# Step 7 def print_contents(url): ''' function print_contents: url is the url to download and print ''' f = urlopen(url) contents = f.read() f.close() print( contents[:512] ) # [:512] limits the number of characters return

Step 8 ~ print_contents('')
At the end of our file add an example to test print_contents:

print_contents('http://www.google.com/trends/hottrends/atom/hourly') # End of file

Step 9 ~ run print_contents
Save the file. And re-run from the command line:

python tutorial-01.py [Enter] Hello World <?xml version="1.0" encoding="UTF-8"?> <feed xmlns="http://www.w3.org/2005/Atom" version="1.0" xml:lang="en"> <title type="text">Google Hot Trends</title> <subtitle type="text">What are people searching for on Google today?</subtitle> ...

Step 10 ~ Complete File
# Beginning of file from urllib2 import urlopen # Step 3 def hi(): ''' function hi: no parameters ''' print( "Hello World\n") return # This runs the code hi() # Step 7 def print_contents(url): ''' function print_contents: url is the url to download and print ''' f = urlopen(url) contents = f.read() f.close() print( contents[:512] ) # [:512] limits the number of characters return print_contents('http://www.google.com/trends/hottrends/atom/hourly') # End of file


     __  __    __    _  _  ____  _____  _  _    _____  ____   ___ 
    (  \/  )  /__\  ( \( )(_  _)(  _  )( \( )  (  _  )(  _ \ / __) 
     )    (  /(__)\  )  (  _)(_  )(_)(  )  (    )(_)(  )   /( (_-.
    (_/\/\_)(__)(__)(_)\_)(____)(_____)(_)\_)()(_____)(_)\_) \___/