The simplest program in Python consists of a line that tells the computer a command. Traditionally, the first program of every programmer in every new language prints "Hello, World!". Start up your favorite editor and save the following into a file:
print "Hello, World!"
To execute this program, save it with a suffix of '.py' (e.g., HelloWorld.py) and type 'python' and the filename in a shell like this:
The output is predictable:
> python HelloWorld.py
If you prefer to execute it by its name, instead of as an argument to the Python interpreter, put a "bang" line at the top. Include the following on the first line of the program, substituting the absolute path to the Python interpreter for /'path/to/python':Be sure to give change the permission on the file to allows execution, if necessary for your operating system.
Now, let's take this program and embellish it a bit.
import re, string, sys
Then let's define the addressee and the punctuation for the output. These are taken from the first two command line arguments:
greeting = sys.argv addressee = sys.argv punctuation = sys.argv
Here, we give greeting the value of the first command-line argument of the program. The first word that comes after the program's name when the program is executed will be assigned using the sys module. The second word (addressee) is sys.argv, etc. (the program's name itself is sys.argv).
From this, we can create a class called Felicitations:
class Felicitations(object): def __init__(self): self.felicitations = [ ] def addon(self, word): self.felicitations.append(word) def printme(self): greeting = string.join(self.felicitations[0:], "") print greeting
The class is based off of another type of object called 'object'. The first method is mandatory if you want the object to know anything about itself (instead of being a brainless mass of functions and variables; the class must have a way of referring to itself. The second method simply adds the value of word to the Felicitations object. Finally, this class has the ability to print itself via a method called 'printme'.
In Python, indentation is important. Every nested block of commands must be indented the same amount. Python has no other way to differentiate between nested and non-nested blocks of commands.
Now, we can make a function which calls the last method of the class:
def prints(string): string.printme() return
Next, we define two more functions. These illustrate how to pass arguments to and how to receive output from functions. The strings in parentheses are arguments on which the function depends. The value returned is signified in the 'return' statement at the end.
def hello(i): string = "hell" + i return string def caps(word): value = string.capitalize(word) return value
The first of these functions takes an argument 'i' which is later concatentated on to the base 'hell' and returned as a variable named 'string'. As we shall see in the main() function, this variable is hardwired in the program as 'o', but you could easily make it user-defined by using sys.argv or similar.
The second function is used to capitalize the parts of the output. It takes one argument, the phrase to be capitalized, and returns it as a value 'value'.
Next, define a main() function:
def main(): salut = Felicitations() if greeting != "Hello": cap_greeting = caps(greeting) else: cap_greeting = greeting salut.addon(cap_greeting) salut.addon(", ") cap_addressee = caps(addressee) lastpart = cap_addressee + punctuation salut.addon(lastpart) prints(salut)
Several things happen in this function:
- We create an instance of the 'Felicitations' class and call it 'salut'. This allows us to access the parts of 'Felicitations' as they exist in 'salut'.
- Next, if 'greeting' does not equate to the string "Hello", then, using function caps() we capitalize the value of 'greeting' and assign it to 'cap_greeting'. Otherwise, 'cap_greeting' is assigned the value of 'greeting'. If this seems tautological, it is, but it is also illustratory of conditional statements in Python.
- Whatever the outcome of the if...else statements, the value of 'cap_greeting' is added onto the value of 'salut', using class object's append method.
- We then append a comma and a space to salut in preparation for the addressee.
- Next, the value of 'addressee' is capitalized and assigned to 'cap_addressee'.
- The values of 'cap_addressee' and 'punctuation' are then concatentated and assigned to 'lastpart'.
- The value of 'lastpart' is then appended to the content of 'salut'.
- Finally, the object 'salut' is sent to the 'prints' function to be printed to the screen.
Alas, we are not done yet. If the program were executed now, it would end with no output whatsoever. This is because the function main() is never called. Here is how to call main() when the program is executed:
if __name__ == '__main__': main()
Save the program as 'hello.py' (without the quotes). Now, you can start the program. Assuming the Python interpreter is in your execution path, you can type
"python hello.py hello world !" and you will be rewarded with the familiar output:Hello, World!
Having read through this rather brisk tour of Python, if you find you still do not understand how certain parts of the language work, I encourage you to read "Beginning Python" instead.