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