WelcomeToPython

Differences between revisions 8 and 9
Revision 8 as of 2005-12-22 21:06:59
Size: 4962
Editor: 217
Comment: added another online book: byte of python
Revision 9 as of 2006-01-29 05:40:31
Size: 5432
Editor: toronto-HSE-ppp3985879
Comment: 1st example had docstring after other statements, 3rd used wrong terminology
Deletions are marked like this. Additions are marked like this.
Line 20: Line 20:
 print "We are inside the main function\n"
Line 24: Line 23:
 print "We are inside the main function\n"
Line 70: Line 70:
import car # car is a class representing a car import car # car is a module representing a car
Line 117: Line 117:
    >>> 7.0/3 # Unless there's a float involved in the operation
    2.3333333333333335
Line 147: Line 149:
There is a really excellent tutorial in the documentation that comes with Python:
 * [http://docs.python.org/tut/tut.html] (or install the -doc package, e.g. python2.4-doc and browse it locally, e.g. [file:///usr/share/doc/python2.4/html/index.html])
A variety of other resources for beginners are listed on the Python wiki
 * [http://wiki.python.org/moin/BeginnersGuide]

Welcome To Python: Dancing with snakes.

Background

  • An interpreted language created in the early 1990's by Guido van Rossum from the Netherlands.
  • A non profit organization called “Python Software Foundation” owns Python related intellectual property.
  • Carries a GPL-Compatible license.
  • For a nice short movie about Python click [http://www.ibiblio.org/pub/multimedia/video/obp/IntroducingPython.mpg here].

The Short Live By List

  • Everything is an object: functions are no exception.

    # this is a very small do-nothing program that shows off the docstring property of a function object.
    def main():
            """This is the main function of our program. It actually does nothing
            but demonstrating that functions in python are objects which can have
            properties, this is the docstring property."""
            print "We are inside the main function\n"
    
    
    if __name__ == "__main__": # checking if were executed standalone, rather then being used as a module.
            main()             # if we were using this file as a module, then
                               # __name__ != "__main__" and main() would never
                               # get executed unless we called from within our
                               # program. 
  • Try keep code readable, yet powerful.
  • Errors should never pass silently.
  • Let's talk high level, leave the dirty work for someone else.
  • Documentation is important enough – make it part of the language objects.
  • Good practices are encouraged, not enforced.
    • Python also supports non OO programming
      • Example

        # this functions tests to see if two people are the same.
        def areWeTheSame(me,you):
                """This function tests if two people are the same. usage: areWeTheSame(MYNAME,YOURNAME)"""
                if (me,you) == ("you","me"):
                        print "We are the same person \n"
                else:
                        print "We are not the same person\n"
        
        def main():
                areWeTheSame("me","you")
                areWeTheSame("you","me")
                areWeTheSame("me","me")
  • If you toss this code example into a file and try run it using the interpreter, nothing happens. Could you guess why?

The Newbie Experience

  • Adjusting to “Everything is an object”:
    • The "simple" explanation - it's code and data packaged together, allowing one to treat that entity as a black box that “just works”.
    • import car # car is a module representing a car
      
      if needToDriveFast:
         car.speedup(120)
      else
         car.slowdown(50)
  • Whitespace significance : getting consistent on the spaces
    • remember this will pay off _eventually_ , as code would have a consistent style thus making code blocks more easily noticable.
  • Running code: Why doesn't my program execute?
    • Although python supports non 00p programming, there is not special "main" function that is the first entry point for executing. Therefor there's a little trick

Common Uses

  • Glue Language
    • Writing shell scripts that are more easily managable and have better readability.
  • GUI development
    • example: GTK bindings for python allow complete gui app development.
  • Web Infrastructures
    • various web and related communication mediums are python powered:
      • Mailman.
      • Plone.
      • etc
  • Nice Calculator
    • Interactive interpreter sessions allows using it as a mathmatical calculator (as well as allowing to evaluate ideas and statements on the fly)
      • example

         >>> 2+2
            4
            >>> # This is a comment
            ... 2+2
            4
            >>> 2+2  # and a comment on the same line as code
            4
            >>> (50-5*6)/4
            5
            >>> # Integer division returns the floor:
            ... 7/3
            2
            >>> 7.0/3  # Unless there's a float involved in the operation
            2.3333333333333335
            >>> 7/-3
            -3
  • String processing.
  • Network Programming.
  • Operating system enhancments and interfacing.

Some Feature Highlights

  • High level language.
    • High level abstracted data types allow for rapid prototyping and
  • Facilitates code readability.
    • Identation rules makes it easier to distinguishes execution block, scope which results in less effort needed to browse the produced code.
    • Being a high level language, code is closer to natural language and thus is less cryptic compared to Perl and C.
  • Auto memory management and garbage
    • Variables and storage space just spring into existence when you first reference it.
    • There is no need to free memory of unused objects, this will happen magically.

There is a really excellent tutorial in the documentation that comes with Python:

A variety of other resources for beginners are listed on the Python wiki

CategoryDocumentation CategoryCleanup

WelcomeToPython (last edited 2008-08-06 16:36:19 by localhost)