Main.UsingObjects History

Hide minor edits - Show changes to markup

March 31, 2010, at 08:52 AM MST by 10.84.44.81 -
Deleted line 2:
Added lines 67-74:
  1. create a list of 2**i for all i that are less than n

def powers_of_2(n) :

    values = []
    for i in range(n) :
        values.append(2**i)
    return values
Changed line 88 from:

s.remove(x)

to:

s.remove(x)

Changed lines 94-95 from:

s.sort([cmp[, key[, reverse]]])

to:

s.sort()

Added lines 109-123:

def print_dictionary(d) :

    """print the elements of the dictionary"""
    for key in d :
        print key, d[key]
  1. Note that for key in d is the same as "for key in d.keys()"

def print_dictionary_sorted(d) :

    """print the elements of the dictionary in sorted order"""
    keys = d.keys()
    keys.sort()
    for key in keys :
        print key, d[key]
March 25, 2010, at 08:21 AM MST by 71.196.160.210 -
Added lines 1-125:

(:source lang=python:)

""" Using python objects - strings, lists, and dictionaries. """

  1. All python variables are "objects"
  2. An object combines data + functions (called methods) that can be applied
  3. to the object's data.
  4. Example: A string - its data are the characters that make up the string.

s = 'python is fun'

  1. An object's methods can be accessed using the dot notation:

s.capitalize()

  1. Think of a method as a message that tells an object to do something,
  2. like return a version of itself capitalized.
  3. Other string methods:
  4. count how many times a given string occurs within our string

s.count('is')

  1. find the location where a given string occurs in our string:

s.find('is')

  1. our familiar split function can be accessed as a method:

s.split()

  1. and join as well:

l = ['a', 'b', 'c', 'd'] ','.join(l)

  1. the alternative way of calling these methods is as functions in
  2. the string module:

import string string.capitalize(s) string.count(s, 'is') string.find(s, 'is')

  1. A complete list of the methods of Python strings can be found at
  2. the Python documentation:
  3. http://docs.python.org/library/stdtypes.html#string-methods
  4. Here list methods:

s = ['a', 'b'] x = 'c'

  1. add an element x to a list s:

s.append(x)

  1. can also be done as:

s = s + [x]

  1. and also

s[len(s):len(s)] = [x]

  1. now if you want to concatenate two lists:

s = ['a', 'b'] x = ['c', 'd'] s.extend(x)

  1. or as we learned it without the use of methods:

s = s + x

  1. or using slices:

s[len(s):len(s)] = x

  1. more methods:

s.count(x)

  1. the number of i's for which s[i] == x

s.index(x[, i[, j]])

  1. the smallest k such that s[k] == x and i <= k < j
  2. insert an item in a list:

s.insert(i, x)

  1. same as s[i:i] = [x]
  2. remove an item from a list

s.remove(x)

  1. same as del s[s.index(x)]
  2. reverse a list

s.reverse()

  1. sort a list

s.sort([cmp[, key[, reverse]]])

  1. see also:
  2. http://docs.python.org/library/stdtypes.html#mutable-sequence-types
  3. methods of Python dictionaries

d = {'key1':'value1', 'key2': 'value2'}

  1. extract all the key,value pairs

d.items()

  1. extract the keys

d.keys()

  1. extract the values

d.values()

  1. Constructing using new instances of a class
  2. Each class has a method for constructing a new instance of the class
  3. that method has the name of the class, and is called a constructor.
  4. Examples:

s = str(1) a = int(1)

  1. note: you can do s = str() and a = int(). What do you think
  2. the result would be?

l = list()

  1. here are some of the way you can create the dictionary {"one": 2, "two": 3}

dict(one=2, two=3) dict({'one': 2, 'two': 3}) dict(zip(('one', 'two'), (2, 3))) dict('two', 3], ['one', 2?)

(:sourceend:)