WelcomeToPython

Revision 4 as of 2005-11-15 17:17:34

Clear message

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():
            print "We are inside the main function\n"
            """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."""
    
    
    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 support also non 00p programming
      • Example

        # this functions tests to see if two people are the same.
        def areWeTheSame(me,you):
                """This functino tests of 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 class 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/-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.

CategoryDocumentation CategoryCleanup