Main.Lists History

Hide minor edits - Show changes to markup

February 19, 2013, at 02:53 PM MST by 129.82.44.223 -
Added line 4:
Added line 27:

numbers = [17, 123]

Added line 46:
  1. now, what do you think about this one:
Changed line 52 from:

vocabulary = ["ameliorate", "castigate", "defenestrate"]

to:

words = ["ameliorate", "castigate", "defenestrate"]

Changed lines 54-55 from:
    print vocabulary[i]
to:
    print words[i],len(words[i])
Changed lines 58-60 from:

for word in vocabulary :

    print word
to:

for word in words :

    print word, len(word)
Added line 63:

vocabulary = ["ameliorate", "castigate", "defenestrate"]

Changed lines 100-104 from:
  1. You can use a slice as an index for del:

a_list = ['a', 'b', 'c', 'd', 'e', 'f'] del a_list[1:5] print a_list

to:
  1. comment: I don't recommend using the del operator - it's easy to make mistakes.
February 15, 2010, at 12:53 PM MST by 10.84.44.68 -
Added lines 1-102:

(:source lang=python:)

"""Lists http://openbookproject.net/thinkcs/python/english2e/ch09.html """

  1. A list is an ordered set of values, where each value is identified by an index.
  2. The values that make up a list are called its elements.
  3. Lists are similar to strings, which are ordered sets of characters, except that
  4. the elements of a list can have any type.
  5. use the bracket notation to create a list:

vocabulary = ["ameliorate", "castigate", "defenestrate"] numbers = [17, 123] empty = []

  1. The elements of a list don't have to be the same type:

mixed_list = ["hello", 2.0, 5, [10, 20]]

  1. Elements of a list are accessed using the bracket operator, the same way the characters
  2. of a string.
  3. Remember that the indices start at 0:

print numbers[0]

  1. the index can't be larger than the length of the list - 1:

print numbers[2]

  1. Any integer expression can be used as an index:

print numbers[9-8]

  1. but the expression needs to be an integer value:

print numbers[1.0]

  1. If an index has a negative value, it counts backward from the end of the list:

print numbers[-1] print numbers[-2]

print numbers[-3]

  1. it is common to iterate through the elements of a list using a for loop:
  2. (note the useage of len() to obtain the length of a list)

vocabulary = ["ameliorate", "castigate", "defenestrate"] for i in range(len(vocabulary)) :

    print vocabulary[i]
  1. or:

for word in vocabulary :

    print word
  1. list membership is determined using the in operator:

'castigate' in vocabulary 'word' in vocabulary

  1. list operations
  2. the + operator concatenates lists:

a = [1, 2, 3] b = [4, 5, 6] c = a + b print c

[0] * 4

  1. Slices
  2. The slice operations we saw with strings also work on lists:

a_list = ['a', 'b', 'c', 'd', 'e', 'f'] a_list[1:3] a_list[:4] a_list[3:] a_list[:]

  1. Unlike strings, lists are mutable, which means we can change their elements
  2. using the bracket operator:

fruit = ["banana", "apple", "quince"] fruit[0] = "pear" fruit[-1] = "orange"

  1. del removes an element from a list:

a = ['one', 'two', 'three'] del a[1] print a

  1. You can use a slice as an index for del:

a_list = ['a', 'b', 'c', 'd', 'e', 'f'] del a_list[1:5] print a_list

(:sourceend:)