sttp/basic_python/strings_dicts.rst
author amit@thunder
Tue, 02 Mar 2010 18:43:02 +0530
changeset 0 27e1f5bd2774
permissions -rw-r--r--
Using test review as the test directory for testing sees.hook for the review app
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
     1
=======
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
     2
Strings
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
     3
=======
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
     4
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
     5
Strings were briefly introduced previously in the introduction document. In this
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
     6
section strings will be presented in greater detail. All the standard operations 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
     7
that can be performed on sequences such as indexing, slicing, multiplication, length
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
     8
minimum and maximum can be performed on string variables as well. One thing to
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
     9
be noted is that strings are immutable, which means that string variables are
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    10
unchangeable. Hence, all item and slice assignments on strings are illegal.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    11
Let us look at a few example.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    12
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    13
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    14
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    15
  >>> name = 'PythonFreak'
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    16
  >>> print name[3]
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    17
  h
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    18
  >>> print name[-1]
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    19
  k
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    20
  >>> print name[6:]
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    21
  Freak
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    22
  >>> name[6:0] = 'Maniac'
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    23
  Traceback (most recent call last):
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    24
    File "<stdin>", line 1, in <module>
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    25
  TypeError: 'str' object does not support item assignment
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    26
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    27
This is quite expected, since string objects are immutable as already mentioned.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    28
The error message is clear in mentioning that 'str' object does not support item
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    29
assignment.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    30
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    31
String Formatting
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    32
=================
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    33
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    34
String formatting can be performed using the string formatting operator represented
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    35
as the percent (%) sign. The string placed before the % sign is formatted with 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    36
the value placed to the right of it. Let us look at a simple example.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    37
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    38
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    39
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    40
  >>> format = 'Hello %s, from PythonFreak'
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    41
  >>> str1 = 'world!'
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    42
  >>> print format % str1
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    43
  Hello world!, from PythonFreak
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    44
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    45
The %s parts of the format string are called the coversion specifiers. The coversion
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    46
specifiers mark the places where the formatting has to be performed in a string. 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    47
In the example the %s is replaced by the value of str1. More than one value can 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    48
also be formatted at a time by specifying the values to be formatted using tuples
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    49
and dictionaries (explained in later sections). Let us look at an example.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    50
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    51
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    52
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    53
  >>> format = 'Hello %s, from %s'
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    54
  >>> values = ('world!', 'PythonFreak')
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    55
  >>> print format % values
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    56
  Hello world!, from PythonFreak
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    57
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    58
In this example it can be observed that the format string contains two conversion 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    59
specifiers and they are formatted using the tuple of values as shown.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    60
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    61
The s in %s specifies that the value to be replaced is of type string. Values of 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    62
other types can be specified as well such as integers and floats. Integers are 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    63
specified as %d and floats as %f. The precision with which the integer or the 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    64
float values are to be represented can also be specified using a **.** (**dot**)
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    65
followed by the precision value.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    66
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    67
String Methods
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    68
==============
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    69
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    70
Similar to list methods, strings also have a rich set of methods to perform various
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    71
operations on strings. Some of the most important and popular ones are presented
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    72
in this section.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    73
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    74
**find**
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    75
~~~~~~~~
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    76
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    77
The **find** method is used to search for a substring within a given string. It 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    78
returns the left most index of the first occurence of the substring. If the 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    79
substring is not found in the string then it returns -1. Let us look at a few 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    80
examples.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    81
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    82
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    83
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    84
  >>> longstring = 'Hello world!, from PythonFreak'
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    85
  >>> longstring.find('Python')
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    86
  19
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    87
  >>> longstring.find('Perl')
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    88
  -1
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    89
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    90
**join**
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    91
~~~~~~~~
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    92
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    93
The **join** method is used to join the elements of a sequence. The sequence 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    94
elements that are to be join ed should all be strings. Let us look at a few 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    95
examples.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    96
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    97
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    98
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
    99
  >>> seq = ['With', 'great', 'power', 'comes', 'great', 'responsibility']
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   100
  >>> sep = ' '
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   101
  >>> sep.join(seq)
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   102
  'With great power comes great responsibility'
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   103
  >>> sep = ',!'
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   104
  >>> sep.join(seq)
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   105
  'With,!great,!power,!comes,!great,!responsibility'
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   106
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   107
*Try this yourself*
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   108
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   109
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   110
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   111
  >>> seq = [12,34,56,78]
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   112
  >>> sep.join(seq)
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   113
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   114
**lower**
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   115
~~~~~~~~~
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   116
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   117
The **lower** method, as the name indicates, converts the entire text of a string
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   118
to lower case. It is specially useful in cases where the programmers deal with case
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   119
insensitive data. Let us look at a few examples.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   120
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   121
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   122
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   123
  >>> sometext = 'Hello world!, from PythonFreak'
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   124
  >>> sometext.lower()
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   125
  'hello world!, from pythonfreak'
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   126
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   127
**replace**
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   128
~~~~~~~~~~~
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   129
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   130
The **replace** method replaces a substring with another substring within
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   131
a given string and returns the new string. Let us look at an example.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   132
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   133
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   134
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   135
  >>> sometext = 'Concise, precise and criticise is some of the words that end with ise'
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   136
  >>> sometext.replace('is', 'are')
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   137
  'Concaree, precaree and criticaree are some of the words that end with aree'
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   138
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   139
Observe here that all the occurences of the substring *is* have been replaced,
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   140
even the *is* in *concise*, *precise* and *criticise* have been replaced.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   141
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   142
**split**
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   143
~~~~~~~~~
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   144
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   145
The **split** is one of the very important string methods. split is the opposite of the 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   146
**join** method. It is used to split a string based on the argument passed as the
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   147
delimiter. It returns a list of strings. By default when no argument is passed it
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   148
splits with *space* (' ') as the delimiter. Let us look at an example.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   149
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   150
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   151
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   152
  >>> grocerylist = 'butter, cucumber, beer(a grocery item??), wheatbread'
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   153
  >>> grocerylist.split(',')
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   154
  ['butter', ' cucumber', ' beer(a grocery item??)', ' wheatbread']
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   155
  >>> grocerylist.split()
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   156
  ['butter,', 'cucumber,', 'beer(a', 'grocery', 'item??),', 'wheatbread']
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   157
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   158
Observe here that in the second case when the delimiter argument was not set 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   159
**split** was done with *space* as the delimiter.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   160
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   161
**strip**
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   162
~~~~~~~~~
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   163
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   164
The **strip** method is used to remove or **strip** off any whitespaces that exist
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   165
to the left and right of a string, but not the whitespaces within a string. Let 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   166
us look at an example.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   167
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   168
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   169
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   170
  >>> spacedtext = "               Where's the text??                 "
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   171
  >>> spacedtext.strip()
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   172
  "Where's the text??"
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   173
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   174
Observe that the whitespaces between the words have not been removed.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   175
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   176
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   177
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   178
  Note: Very important thing to note is that all the methods shown above do not
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   179
        transform the source string. The source string still remains the same.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   180
	Remember that **strings are immutable**.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   181
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   182
Introduction to the standard library
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   183
====================================
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   184
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   185
Python is often referred to as a "Batteries included!" language, mainly because 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   186
of the Python Standard Library. The Python Standard Library provides an extensive
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   187
set of features some of which are available directly for use while some require to
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   188
import a few **modules**. The Standard Library provides various built-in functions
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   189
like:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   190
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   191
    * **abs()**
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   192
    * **dict()**
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   193
    * **enumerate()**
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   194
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   195
The built-in constants like **True** and **False** are provided by the Standard Library.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   196
More information about the Python Standard Library is available http://docs.python.org/library/
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   197
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   198
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   199
I/O: Reading and Writing Files
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   200
==============================
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   201
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   202
Files are very important aspects when it comes to computing and programming.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   203
Up until now the focus has been on small programs that interacted with users
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   204
through **input()** and **raw_input()**. Generally, for computational purposes
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   205
it becomes necessary to handle files, which are usually large in size as well.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   206
This section focuses on basics of file handling.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   207
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   208
Opening Files
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   209
~~~~~~~~~~~~~
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   210
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   211
Files can be opened using the **open()** method. **open()** accepts 3 arguments
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   212
out of which 2 are optional. Let us look at the syntax of **open()**:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   213
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   214
*f = open( filename, mode, buffering)*
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   215
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   216
The *filename* is a compulsory argument while the *mode* and *buffering* are 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   217
optional. The *filename* should be a string and it should be the complete path
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   218
to the file to be opened (The path can be absolute or relative). Let us look at
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   219
an example.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   220
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   221
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   222
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   223
  >>> f = open ('basic_python/interim_assessment.rst')
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   224
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   225
The *mode* argument specifies the mode in which the file has to be opened.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   226
The following are the valid mode arguments:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   227
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   228
**r** - Read mode
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   229
**w** - Write mode
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   230
**a** - Append mode
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   231
**b** - Binary mode
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   232
**+** - Read/Write mode
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   233
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   234
The read mode opens the file as a read-only document. The write mode opens the
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   235
file in the Write only mode. In the write mode, if the file existed prior to the
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   236
opening, the previous contents of the file are erased. The append mode opens the
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   237
file in the write mode but the previous contents of the file are not erased and
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   238
the current data is appended onto the file.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   239
The binary and the read/write modes are special in the sense that they are added
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   240
onto other modes. The read/write mode opens the file in the reading and writing
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   241
mode combined. The binary mode can be used to open a files that do not contain 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   242
text. Binary files such as images should be opened in the binary mode. Let us look
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   243
at a few examples.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   244
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   245
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   246
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   247
  >>> f = open ('basic_python/interim_assessment.rst', 'r')
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   248
  >>> f = open ('armstrong.py', 'r+')
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   249
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   250
The third argument to the **open()** method is the *buffering* argument. This takes
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   251
a boolean value, *True* or *1* indicates that buffering has to be enabled on the file,
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   252
that is the file is loaded on to the main memory and the changes made to the file are 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   253
not immediately written to the disk. If the *buffering* argument is *0* or *False* the 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   254
changes are directly written on to the disk immediately.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   255
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   256
Reading and Writing files
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   257
~~~~~~~~~~~~~~~~~~~~~~~~~
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   258
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   259
**write()**
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   260
-----------
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   261
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   262
**write()**, evidently, is used to write data onto a file. It takes the data to 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   263
be written as the argument. The data can be a string, an integer, a float or any
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   264
other datatype. In order to be able to write data onto a file, the file has to
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   265
be opened in one of **w**, **a** or **+** modes.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   266
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   267
**read()**
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   268
----------
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   269
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   270
**read()** is used to read data from a file. It takes the number of bytes of data
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   271
to be read as the argument. If nothing is specified by default it reads the entire 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   272
contents from the current position to the end of file.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   273
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   274
Let us look at a few examples:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   275
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   276
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   277
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   278
  >>> f = open ('randomtextfile', 'w')
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   279
  >>> f.write('Hello all, this is PythonFreak. This is a random text file.')
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   280
  >>> f = open ('../randomtextfile', 'r')
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   281
  >>> f = open ('../randomtextfile', 'r')
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   282
  >>> f.read(5)
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   283
  'Hello'
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   284
  >>> f.read()
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   285
  ' all, this is PythonFreak. This is a random text file.'
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   286
  >>> f.close()
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   287
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   288
**readline()**
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   289
--------------
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   290
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   291
**readline()** is used to read a file line by line. **readline()** reads a line
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   292
of a file at a time. When an argument is passed to **readline()** it reads that
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   293
many bytes from the current line.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   294
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   295
One other method to read a file line by line is using the **read()** and the 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   296
**for** construct. Let us look at this block of code as an example.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   297
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   298
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   299
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   300
  >>> f = open('../randomtextfile', 'r')
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   301
  >>> for line in f:
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   302
  ...     print line
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   303
  ... 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   304
  Hello all!
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   305
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   306
  This is PythonFreak on the second line.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   307
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   308
  This is a random text file on line 3
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   309
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   310
**close()**
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   311
-----------
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   312
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   313
One must always close all the files that have been opened. Although, files opened
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   314
will be closed automatically when the program ends. When files opened in read mode
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   315
are not closed it might lead to uselessly locked sometimes. In case of files
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   316
opened in the write mode it is more important to close the files. This is because,
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   317
Python maybe using the file in the buffering mode and when the file is not closed
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   318
the buffer maybe lost completely and the changes made to the file are lost forever.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   319
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   320
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   321
Dictionaries
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   322
============
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   323
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   324
A dictionary in general, are designed to be able to look up meanings of words. 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   325
Similarly, the Python dictionaries are also designed to look up for a specific
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   326
key and retrieve the corresponding value. Dictionaries are data structures that
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   327
provide key-value mappings. Dictionaries are similar to lists except that instead
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   328
of the values having integer indexes, dictionaries have keys or strings as indexes.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   329
Let us look at an example of how to define dictionaries.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   330
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   331
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   332
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   333
  >>> dct = { 'Sachin': 'Tendulkar', 'Rahul': 'Dravid', 'Anil': 'Kumble'}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   334
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   335
The dictionary consists of pairs of strings, which are called *keys* and their
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   336
corresponding *values* separated by *:* and each of these *key-value* pairs are
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   337
comma(',') separated and the entire structure wrapped in a pair curly braces *{}*.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   338
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   339
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   340
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   341
  Note: The data inside a dictionary is not ordered. The order in which you enter
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   342
  the key-value pairs is not the order in which they are stored in the dictionary.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   343
  Python has an internal storage mechanism for that which is out of the purview 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   344
  of this document.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   345
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   346
**dict()**
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   347
~~~~~~~~~~
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   348
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   349
The **dict()** function is used to create dictionaries from other mappings or other
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   350
dictionaries. Let us look at an example.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   351
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   352
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   353
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   354
  >>> diction = dict(mat = 133, avg = 52.53)
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   355
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   356
**String Formatting with Dictionaries:**
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   357
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   358
String formatting was discussed in the previous section and it was mentioned that
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   359
dictionaries can also be used for formatting more than one value. This section 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   360
focuses on the formatting of strings using dictionaries. String formatting using
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   361
dictionaries is more appealing than doing the same with tuples. Here the *keyword*
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   362
can be used as a place holder and the *value* corresponding to it is replaced in
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   363
the formatted string. Let us look at an example.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   364
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   365
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   366
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   367
  >>> player = { 'Name':'Rahul Dravid', 'Matches':133, 'Avg':52.53, '100s':26 }
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   368
  >>> strng = '%(Name)s has played %(Matches)d with an average of %(Avg).2f and has %(100s)d hundreds to his name.'
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   369
  >>> print strng % player
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   370
  Rahul Dravid has played 133 with an average of 52.53 and has 26 hundreds to his name.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   371
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   372
Dictionary Methods
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   373
~~~~~~~~~~~~~~~~~~
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   374
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   375
**clear()**
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   376
-----------
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   377
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   378
The **clear()** method removes all the existing *key-value* pairs from a dictionary.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   379
It returns *None* or rather does not return anything. It is a method that changes
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   380
the object. It has to be noted here that dictionaries are not immutable. Let us 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   381
look at an example.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   382
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   383
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   384
  
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   385
  >>> dct
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   386
  {'Anil': 'Kumble', 'Sachin': 'Tendulkar', 'Rahul': 'Dravid'}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   387
  >>> dct.clear()
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   388
  >>> dct
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   389
  {}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   390
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   391
**copy()**
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   392
----------
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   393
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   394
The **copy()** returns a copy of a given dictionary. Let us look at an example.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   395
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   396
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   397
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   398
  >>> dct = {'Anil': 'Kumble', 'Sachin': 'Tendulkar', 'Rahul': 'Dravid'}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   399
  >>> dctcopy = dct.copy()
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   400
  >>> dctcopy
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   401
  {'Anil': 'Kumble', 'Sachin': 'Tendulkar', 'Rahul': 'Dravid'}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   402
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   403
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   404
**get()**
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   405
---------
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   406
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   407
**get()** returns the *value* for the *key* passed as the argument and if the
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   408
*key* does not exist in the dictionary, it returns *None*. Let us look at an
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   409
example.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   410
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   411
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   412
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   413
  >>> print dctcopy.get('Saurav')
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   414
  None
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   415
  >>> print dctcopy.get('Anil')
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   416
  Kumble
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   417
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   418
**has_key()**
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   419
-------------
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   420
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   421
This method returns *True* if the given *key* is in the dictionary, else it returns 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   422
*False*.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   423
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   424
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   425
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   426
  >>> dctcopy.has_key('Saurav')
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   427
  False
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   428
  >>> dctcopy.has_key('Sachin')
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   429
  True
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   430
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   431
**pop()**
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   432
---------
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   433
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   434
This method is used to retrieve the *value* of a given *key* and subsequently 
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   435
remove the *key-value* pair from the dictionary. Let us look at an example.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   436
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   437
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   438
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   439
  >>> print dctcopy.pop('Sachin')
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   440
  Tendulkar
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   441
  >>> dctcopy
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   442
  {'Anil': 'Kumble', 'Rahul': 'Dravid'}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   443
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   444
**popitem()**
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   445
-------------
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   446
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   447
This method randomly pops a *key-value* pair from a dictionary and returns it.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   448
The *key-value* pair returned is removed from the dictionary. Let us look at an
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   449
example.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   450
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   451
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   452
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   453
  >>> print dctcopy.popitem()
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   454
  ('Anil', 'Kumble')
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   455
  >>> dctcopy
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   456
  {'Rahul': 'Dravid'}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   457
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   458
  Note that the item chosen is completely random since dictionaries are unordered
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   459
  as mentioned earlier.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   460
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   461
**update()**
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   462
------------
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   463
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   464
The **update()** method updates the contents of one dictionary with the contents
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   465
of another dictionary. For items with existing *keys* their *values* are updated,
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   466
and the rest of the items are added. Let us look at an example.
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   467
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   468
::
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   469
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   470
  >>> dctcopy.update(dct)
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   471
  >>> dct
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   472
  {'Anil': 'Kumble', 'Sachin': 'Tendulkar', 'Rahul': 'Dravid'}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   473
  >>> dctcopy
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   474
  {'Anil': 'Kumble', 'Sachin': 'Tendulkar', 'Rahul': 'Dravid'}
27e1f5bd2774 Using test review as the test directory for testing sees.hook for the review app
amit@thunder
parents:
diff changeset
   475