Python 123 introduction

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeremy Sanders

    Python 123 introduction

    Here is a brief simple introduction to Python I wrote for a computing course
    for graduate astronomers. It assumes some programming experience. Although
    it is not a complete guide, I believe this could be a useful document for
    other groups to learn Python, so I'm making it available for others to
    download, and modify for their own needs (some of the content is site
    specific).

    HTML version:

    Postscript LaTeX output:

    PDF LaTeX output:

    LaTeX source:


    Jeremy

    --
    Jeremy Sanders

  • dakman@gmail.com

    #2
    Re: Python 123 introduction

    This is great! A excellent tutorial for somone who has prior experience
    in programming and is starting out in python. My friend keeps wanting
    me to teach him python, I think this would be the perfect link for him.
    Thanks.


    Jeremy Sanders wrote:
    Here is a brief simple introduction to Python I wrote for a computing course
    for graduate astronomers. It assumes some programming experience. Although
    it is not a complete guide, I believe this could be a useful document for
    other groups to learn Python, so I'm making it available for others to
    download, and modify for their own needs (some of the content is site
    specific).
    >
    HTML version:

    Postscript LaTeX output:

    PDF LaTeX output:

    LaTeX source:

    >
    Jeremy
    >
    --
    Jeremy Sanders
    http://www.jeremysanders.net/

    Comment

    • Jeremy Sanders

      #3
      Re: Python 123 introduction

      dakman@gmail.co m wrote:
      This is great! A excellent tutorial for somone who has prior experience
      in programming and is starting out in python. My friend keeps wanting
      me to teach him python, I think this would be the perfect link for him.
      I'm glad you think it is useful. It needs a bit of cleaning up as it assumes
      things such as python being in /usr/local/bin... I may try to improve this
      later.

      Jeremy

      --
      Jeremy Sanders

      Comment

      • skip@pobox.com

        #4
        Re: Python 123 introduction


        dakmanThis is great! A excellent tutorial for somone who has prior
        dakmanexperienc e in programming and is starting out in python. My
        dakmanfriend keeps wanting me to teach him python, I think this would
        dakmanbe the perfect link for him.

        I'm not trying to minimize Jeremy's efforts in any way, but how is his
        tutorial a significant improvement over the original
        (http://www.python.org/doc/current/tut/)?

        Skip

        Comment

        • Jeremy Sanders

          #5
          Re: Python 123 introduction

          skip@pobox.com wrote:
          I'm not trying to minimize Jeremy's efforts in any way, but how is his
          tutorial a significant improvement over the original
          (http://www.python.org/doc/current/tut/)?
          It's not intended as a replacement, but what I wanted to do was write a
          quick 2 hour course for people to work through. It overlaps quite a bit
          with the tutorial, but I tried to minimise any detail.

          I just publicised it in case anybody wanted something similar.

          Jeremy

          --
          Jeremy Sanders

          Comment

          • Fredrik Lundh

            #6
            Re: Python 123 introduction

            skip@pobox.com wrote:
            I'm not trying to minimize Jeremy's efforts in any way, but how is his
            tutorial a significant improvement over the original
            (http://www.python.org/doc/current/tut/)?
            don't forget the community-enhanced edition over at:

            pytut.infogami. com

            </F>

            Comment

            • John Salerno

              #7
              Re: Python 123 introduction

              skip@pobox.com wrote:
              dakmanThis is great! A excellent tutorial for somone who has prior
              dakmanexperienc e in programming and is starting out in python. My
              dakmanfriend keeps wanting me to teach him python, I think this would
              dakmanbe the perfect link for him.
              >
              I'm not trying to minimize Jeremy's efforts in any way, but how is his
              tutorial a significant improvement over the original
              (http://www.python.org/doc/current/tut/)?
              >
              Skip
              well, not to minimize the efforts of guido's tutorial, but sometimes it
              seems less of a tutorial and more of a summary of python's features, so
              it never hurts to have things explained in a different way...

              Comment

              • Bruno Desthuilliers

                #8
                Re: Python 123 introduction

                Jeremy Sanders a écrit :
                Here is a brief simple introduction to Python I wrote for a computing course
                for graduate astronomers. It assumes some programming experience. Although
                it is not a complete guide, I believe this could be a useful document for
                other groups to learn Python, so I'm making it available for others to
                download, and modify for their own needs (some of the content is site
                specific).
                May I emit some observations and suggest a couple of corrections ?

                """
                To make it executable type chmod +x test.py, then run it by typing its
                name, test.py on the unix prompt
                """

                Unless the current directory is in the path, this won't work:
                bruno@bibi ~ $ cat toto.py
                #!/usr/bin/python
                print 'hello'
                bruno@bibi ~ $ chmod +x toto.py
                bruno@bibi ~ $ toto.py
                -bash: toto.py: command not found
                bruno@bibi ~ $ ./toto.py
                hello
                bruno@bibi ~ $



                ">>a+b # the value is printed at the prompt"
                s/value/result/

                "Numbers can be integers (int, whole numbers) or floating point (float)"
                s/Numbers/Numeric objects/

                "Strings are collections of characters"
                s/Strings/String objects/

                "Lists are collections of any types of variable (even lists)"
                List objects are ordered collections of any type of objects (even other
                lists)

                "Tuples are like lists but they cannot be changed"
                s/Tuples/Tuple objects/

                <side-note>
                Semantically, a tuple is more a kind of record - a dict indexed by
                position - than an immutable list. That is: lists are homogenous ordered
                collections of arbitrary length. Neither the length of the collection
                nor the position of an object in it have special meaning. While tuples
                are fixed-length heterogenous ordered structures where both the number
                of items and their positions are meaningfull. Canonically, a DB table
                can be represented as a list of tuples.
                </side-note>

                "Files correspond to files on the disk"
                File objects correspond to OS files.
                >>import sys
                >>type(sys.stdi n)
                <type 'file'>

                """
                Note that immutable objects (like numbers, strings or tuples) do not
                have this property.
                >>a = 10 # makes a point to object 10
                """

                NB : here '10' is not the id of the object, it's its value. So it should
                be: # makes name 'a' point to an int object

                """
                >>b = a # makes b point to object 10
                >>a = 11 # makes a point to object 11
                >>print b # prints 10
                """

                Hem... This has nothing to do with ints being immutables:

                a = [1] # makes 'a' point to a list
                b = a # makes 'b' points to the same object
                a = [1] # makes 'a' points to *another* list
                print "a is b ? %s" % (a is b)

                """
                In Python subroutines, procedures and functions are basically the same thing
                """

                NB : The type is 'function'. They *always* return something
                (implicitely, the None object).

                "None is a special value meaning ``nothing''"
                s/value/object/

                "You can test whether something is None by using is None"
                There's always only one single None object, so you can test whether
                something is None by using 'is None'.

                """
                a = ['foo', 'fred', 42]
                for i in a:
                print i
                """

                Traditionaly, identifier 'i' is used as the current index in C-like
                loops. Using it in this context might be a bit confusing :

                a = ['foo', 'fred', 42]
                for obj in a:
                print obj

                """
                As an aside, there is a shortcut version of loops called a list
                comprehension which is very convenient:
                """

                List comps are a "shortcut" for building lists. They are not a "shortcut
                version of loops".

                """
                filename = 'stupid.dat'
                try:
                f = open(filename)
                except IOError: # the file did not open
                print "The filename", filename, "does not exist!"
                """

                Actually, the file may exist, but the program may not be able to open it
                for other reasons...

                filename = 'stupid.dat'
                try:
                f = open(filename)
                except IOError, e: # the file did not open
                print "could not open file %s : %s" % (filename, e)



                My 2 cents...

                Comment

                Working...