Tag Archive: Python


Running Python on GoDaddy


If you subscribe to GoDaddy hosting, you’ve likely noticed that they claim to support Python. However, if you are a noob to python and the web like myself, it’s unclear how to get started. So, below is a sample on how you can render a simple html page in Python under GoDaddy hosting.

1.) Create a new file with a .cgi extension (i.e. test.cgi – see: http://oscarvalles.com/test.cgi)
2.) Copy and paste the following text into your CGI file.

#!/usr/bin/python2.4
'''
This is a comment,
If you see this, CGI is not working
'''
print "Content-type: text/html\n\n"
print "<body bgcolor='000'>"
print "<font face='Courier New' color='white'>Test python page.  CGI extension </font>"
print "</body>"

Let the fun begin.

last character on a line is a backslash in python


When the last character in a line is a backslash in a Python program, what does that mean?

Answer.  The backslash is simply used to enable a carriage return like capability when writing your code, as in the example below.

 

print "Hello \ 

         World!"

would provide the same result as 

print "Hello World!"

 

For example, you may have a string of text that is too large to fit in one line such as a really long sentence. While the long sentences’ appearance may be suitable for your program output, it may not be easy to work within your IDE.  So like the example above, you use the single backslash at the end of the line to

allow you to go onto the next line and continue typing your code.

 

You will also find the backslash used in other instances of a Python program such as an escape character.  This will be within the string however, as opposed to the end of it.

 

Example:

 

str_ex = "\"Bonjour mes amies.\", she said"

print str_ex

 

The backslash is used to escape and display the double quotes surrounding Bonjour mes amies.  That means good day my friends in French.

 

Installing and getting started with Python on Linux (Ubuntu)


Lately, within the last year, I’ve been hearing a lot about Python in one form or another.  The things I’ve heard or read are good of course.  Among the things read and heard, the top three are that it is portable, fast and flexible.

Currently I don’t have too much time to spend exploring Python, however I am still naturally curious about Python as you are.   So I decided to install it and write about the ease of the installation and write a small program using Python.  

I hope this benefits others by providing a visual glance of what installing Python entails and how quick you can get a program up and running.  

My System Environment:

Ubuntu Linux (Version 9 / Jaunty)

Before I begin to demonstrate the installation process via screenshots, I want you all to know that you can accomplish the same task (probably) via the Add/Remove Programs interface in Ubuntu.   Simply click on applications and by default, it is the last option at the bottom.  Then search for Python, click on the check box and click the apply button to allow Ubuntu’s installation of Python.

The method below takes you through the installation option as if you downloaded the package from Python’s site and installed it via the terminal.

  1. Download the tarball or bzipped tarball file from Python.
  2. Unpack the contents to your Documents or any directory of your choice (you can do this by right clicking on the file and using the context menu to unpack the contents, or more savvy users can use their terminals.
  3. Open up your terminal window (Applications > Accessories > Terminal)
  4. Navigate to the directory where you unpacked the contents of your Python package
    Installing Python - Go to the directory with the unpacked files
  5. Make sure you are in the top level directory of the folder that you just finished unpacking.  When you type ls in your terminal, you should be seeing the same files and folders (more or less depending on the version you are installing)
    Top Level Directory of Python Folder
  6. Then type "./configure" without the quotes
    Installing Python - issuing configure command
  7. Next you’ll need to wait a few minutes while it finishes it’s process.  You should be seeing lots of scrolling text beginning with the word checking in every line.  If so, you are on the right track.
    Python is installing in Ubuntu - Linux Terminal Window
  8. After it completes, type sudo su (in Ubuntu or simply su in other distros) to get the root.  Then type "make install" without the quotes to complete the installation of Python on Linux.
    Make Install in Terminal Window  - Python Installation on Linux
  9. You should get the following text that shows the process is complete
    Installation of Python complete - Installs to your usr/local/bin
  10. Now you are ready to begin programming in Python.  To do this, let’s clear our Terminal screen by typing "clear" without the quotes.  Your terminal should then clear.  Next type the word "python" without quotes. You should get the following message: 
    [GCC 4.3.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
  11. Begin by typing v1 = 3 [Press Enter] v2 = 7 [Press Enter] print v1 + v2 [Press Enter]
    This should give you the output "10" on your terminal.
  12. The variables are still in place so now lets type the following:
    print v1, ‘ + ‘, v2, ‘ = ‘, v1 + v2 
    That should give you the following output: 
    3  +  7  =  10
    >>>
  13. To exit out you can type quit().  The screenshot below depicts what you should be seeing in your terminal.
    First Python program using the Linux Terminal

Although I am an fond of C#, I must comment on how neat Python is.  First of all, I did not have to declare the variable types for v1 or v2.  In the program they were used as they were supposed to be used, as integers.  I also read somewhere that you can mix number types (e.g. add integers and double types) something that is not always done in C# through implicit conversion but rather has to be done via casting.  Also, this Python program was pretty simple to code.  There was minimal text, no semicolon’s at the end of a line and less syntax to worry about.  A similar program written in C# would take more lines of code and syntax rules.  I have a favorable impression of Python so far.  It may be a programming language I get into in the future.