Python:hello
From Jonathan D. Lettvin
Hello
This page is intended to give the reader a very early start on python programming. Avoiding cleverness is a goal here.
It is customary to write programs to output "hello world" to illustrate how to write programs in various languages. See wikipedia
I took a slightly different approach here of using hello world as a deliverable message around which I illustrate various value-adds from using python in more and more sophisticated ways.
Interactive Python
Python is strictly an interpreter. You enter "python" on the command-line. The python prompt is usually ">>>". You leave the interpreter with "exit", or <Ctrl>d on Unix, or <Ctrl>z on Windows.
$ python >>> print "hello world" >>> exit $
From this point on, the '$' and '>>>' prompts will be eliminated and all that will be shown are script-file contents.
Value Add: Scripting and Comments
The first line of a script usually specifies the language interpreter. But a script can also be run by invoking: "python hello.py" substituting hello.py with your own script.
#!/usr/bin/env python # This is a comment. print "hello world"
Value Add: Documentation Strings
#!/usr/bin/env python """ Python has trivial scripting capability. Triple quoted strings like this enable multi-line strings to be defined. This one is known as __doc__. """ print "hello world" print __doc__
Value Add: Functions and Indenting
Python organizes code using indentation in place of enclosing characters. A block is all contiguous code at the same level of indent. Be careful with this, tabs cause problems when debugging python,
#!/usr/bin/env python
def hello(): # functions begin with the keyword "def"
"This string is known as hello.__doc__"
print "hello world" # code is indented
hello() # function calls are passingly similar to C++
print hello.__doc__ # Functions can be documented too.
Value Add: Formal Parameters
#!/usr/bin/env python
def hello(message):
print message
hello("hello world")
Value Add: Default Formal Parameters
#!/usr/bin/env python
"Parameters can have defaults and be over-ridden"
def hello(message = "hello world"):
print message
hello()
hello("hello Alain")
Value Add: Classes
#!/usr/bin/env python
"""
Classes encapsulate members and methods.
Here is just the output method and
overriding a default parameter.
"""
class Hello:
def output(self, message = "hello world"):
print message
# hello is made an instance of the Hello class
hello = Hello()
# The output method is used directly.
hello.output("hello skills")
Value Add: Classes with special methods
Classes enable a programmer to bundle related code and data into a neat "object". The class definition describes what can be in an object. A class instance specifies what the code and data are. Visibility is universal and the data sharing rules are completely different from C++. There is no such thing as data hiding.
#!/usr/bin/env python
class Hello(object):
def __init__(self, message = "hello world"):
self.message = message
def __call__(self):
print self.message
hello = Hello("hello class")
ciao = Hello()
hello()
print ciao.message
ciao.message = "Modified Hello"
ciao()
Value Add: Well-Formed Scripts
It is useful to know what comprises "best practice". Here is a script with many valued characteristics. It has a script __doc__ header with author information. It has a class __doc__ enabling code introspection. It has __doc__ strings for each method. It has a __main__ entry point. The class defines a constructor and functor. Describing the well-formed class is beyond the scope of this set of simple illustrations.
#!/usr/bin/env python
"""
hello6.py
Copyright(c) 2011 Jonathan D. Lettvin, All Rights Reserved.
Author: Jonathan D. Lettvin
Date: 20110406
contact: jlettvin@gmail.com
Comments like this enable a program to be self-documenting.
"""
class Hello(object):
"""
This is the class's documentation.
Note that all indentation must be identical at each level.
Python style guide can be found here:
http://www.python.org/dev/peps/pep-0008/
"""
def __init__(self, message = "hello world"):
"""
__init__ is the constructor, called when a class is instanced.
When a class is instanced such as " hello = Hello()"
the parentheses after the class name cause this method to be invoked.
This string is the constructor method's documentation.
This comment is known as Hello.__doc__.
"""
self.message = message
def __call__(self, message = None):
"""
__call__ is the functor, called when a class is used like a function.
When an instance is called such as " hello()"
the parentheses cause use of this method.
"""
if message:
print message
else:
print self.message
if __name__ == "__main__":
"Well structured scripts include a __main__"
print __doc__
print Hello.__doc__
print Hello.__init__.__doc__
print Hello.__call__.__doc__
hello = Hello("hello class")
hello()
hello("hello specific")
