Laran Evans
I develop software people love to use.
  • Home
  • About
  • Contact
  • Resume
  • Testimonials
Skip to content
  • Entrepreneurship
    • Leadership
    • Project Management
    • Team Development
    • Time Management
  • Motorcycles
    • Restoration Projects
  • Puzzles
  • Software Architecture
    • Cloud Computing
  • Software Development
    • Algorithms
    • CSS
    • Java
    • Javascript
    • MySQL
    • PHP
    • Python
    • Ruby and Rails
    • System Administration
« Modeling Relationships with Google AppEngine
Great source for sample code »

Learning Python: Hour 1

By laran | Published: 2009/03/24

General

Everything(!) is an object

if __name__ == ‘__main__’:

Collections

Dictionaries use squiqqly brackets

Lists use square brackets

Tuples use parens

Dictionaries

Basic syntax is

  • foo{‘bar’:'baz’}
  • foo['bar']
  • del foo['bar'] to delete
  • foo.clear() to clear
  • foo.keys() returns a list (need the parens)
  • foo.values() returns a list

Lists

Basic syntax is

  • li = ['a','b','c','d','e']
  • li[0]
  • li[-1] is OK (gets the last element in the list)
Slicing
  • li[1:3] is ['b','c']
  • li[1:-1] is ['b','c','d']
    • So this is different that referencing an element just by index. It’s off by one. It means more of a “leave out the last x elements”
  • li[0:3] is ['a','b','c']
  • li[:-1] is OK
  • li[:] returns a copy of li, same values but a different instance
Adding elements
  • li.append(value), li.extend(list of values), li.insert(index, value)
  • append vs. extend
    • extend joins the lists. append just adds a value to the end of the list
Searching
  • li.index(value)
  • value in li (‘a’ in li returns True/False)
Removing elements
  • li.remove(value)
  • li.pop()
List arithmetic
  • ['a','b'] * 3 == ['a','b','a','b','a','b']
  • ['a','b'] + ['c'] = ['a','b','c']
    • concatenation, can only be done with lists, can’t add a single value, have to add another list

Tuples (immutable lists)

  • no methods (append(), remove(), index() are all invalid)
  • can’t be modified once defined
  • in CAN be used on a tuple
    • ‘a’ in (‘a’,'b’,'c’) is OK (returns True)
  • faster (lookup-wise) than lists
  • can be used as a dictionary key (lists can’t)
  • used in string formatting

Simultaneous multi-value assignment:

  • v = (‘a’,'b’,'c’)
  • (x,y,z) = v
    • x is ‘a’, y is ‘b’, z is ‘c’

Consecutive value assignment

  • (MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY) = range(7)
    • MONDAY is 0, TUESDAY is 1, etc.

String formatting

  • print “Hello %s” % (‘world’,)
  • Second argument must be a tuple
This entry was posted in Python and tagged Programming, tutorial. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.
« Modeling Relationships with Google AppEngine
Great source for sample code »

Post a Comment

Click here to cancel reply.

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • About Me

    I've got a masters degree in computer science and over 10 years of experience building web-based systems using Java/J2EE, Ruby, Rails and PHP. I'm a strong believer in the effectiveness of Agile Methods. Read more »

  • Subscribe

  • Table of Contents
    • General
    • Collections
      • Dictionaries
      • Lists
        • Slicing
        • Adding elements
        • Searching
        • Removing elements
        • List arithmetic
      • Tuples (immutable lists)
      • Simultaneous multi-value assignment:
      • Consecutive value assignment
      • String formatting
  • Similar Posts
    • Djammin' with Django
    • Getting Started With Django 1.0 on Google AppEngine
    • Resolve "ImportError: No module named antlr3"
    • Great source for sample code
  • From Around the Web

      Shared Items
    • Recent Posts

      • The carbs are clean!
      • 80 pages of Ruby on Rails Performance Optimization Tips
      • Ruby Garbage Collection In-Depth
      • Binary Logic Basics
      • Kuali in Nacubo Magazine
    • Older Posts By Month

      Let's Talk

      Ask a question below. You'll be prompted for your name and email after you click the "Ask" button.

    Know more. Accomplish more. Succeed.