basic_python/list_tuples.rst
changeset 12 ea1bc776f495
parent 11 edd18b1f5cb8
child 46 b4d0089294b9
equal deleted inserted replaced
11:edd18b1f5cb8 12:ea1bc776f495
   395 
   395 
   396 The syntax used here is very clear from the way it is written. It can be 
   396 The syntax used here is very clear from the way it is written. It can be 
   397 translated into english as, "for each element x in the list all_num, 
   397 translated into english as, "for each element x in the list all_num, 
   398 if remainder of x divided by 2 is 0, add x to the list."
   398 if remainder of x divided by 2 is 0, add x to the list."
   399 
   399 
       
   400 
       
   401 Tuples
       
   402 ------
       
   403 
       
   404 *Tuples* are sequences just like *Lists*, but they are immutable. In other
       
   405 words *Tuples* provides a way to represent a group of items, where the group
       
   406 of items cannot be changed in any way. The syntax of a *Tuple* is also very
       
   407 similar to *List*. A *Tuple* is represented with the list of items, called
       
   408 elements of the *Tuple* separated by comma, with the entire list being enclosed
       
   409 in parenthesis. It is not compulsory to use parenthesis around a *Tuple* but
       
   410 it may be necessary in some of the cases::
       
   411 
       
   412   >>> a = 1, 2, 3
       
   413   >>> a
       
   414   (1, 2, 3)
       
   415   >>> b = 1,
       
   416   >>> b
       
   417   (1,)
       
   418 
       
   419 It is interesting to note the second example. Just a value followed by a comma
       
   420 automatically makes that an element of a *Tuple* with only one element. It is
       
   421 also important to note that, irrespective of input having a parenthesis, the
       
   422 output always has a parenthesis.
       
   423 
       
   424 The first example is also known as *Tuple packing*, because values are being
       
   425 packed into a tuple. It is also possible to do *Tuple unpacking* which is more
       
   426 interesting. It is better to understand that by example. Say we have a 
       
   427 co-ordinate pair from which we need to separate x and y co-ordinates::
       
   428 
       
   429   >>> a = (1, 2)
       
   430   >>> x, y = a
       
   431   >>> x
       
   432   1
       
   433   >>> y
       
   434   2
       
   435 
       
   436 *Tuple unpacking* also has several other use-cases of which the most interesting
       
   437 one is to swap the values of two variables. Using programming languages like C
       
   438 would require anywhere around 10 lines of code and an extra temporary variable
       
   439 to do this (including all the #include stuff). Python does it in the most
       
   440 intuitive way in just one line. Say we want to swap the co-ordinates in the
       
   441 above example::
       
   442 
       
   443   >>> x, y = y, x
       
   444   >>> x
       
   445   2
       
   446   >>> y
       
   447   1
       
   448 
       
   449 Common Tuple Operations
       
   450 ~~~~~~~~~~~~~~~~~~~~~~~
       
   451 
       
   452 There is no need to introduce all the *Tuple* operations again, since *Tuples*
       
   453 support the following operations that *List* supports in exactly the same way:
       
   454 
       
   455   * Indexing
       
   456   * Concatenating
       
   457   * Slicing
       
   458   * Membership
       
   459   * Multiplication
       
   460   * Length, Maximum, Minimum
       
   461 
       
   462 The following examples illustrate the above operations::
       
   463 
       
   464   >>> a = (1, 2, 3, 4, 5, 6)
       
   465   >>> a[5]
       
   466   6
       
   467   >>> b = (7, 8, 9)
       
   468   >>> a + b
       
   469   (1, 2, 3, 4, 5, 6, 7, 8, 9)
       
   470   >>> a[3:5]
       
   471   (4, 5)
       
   472   >>> 5 in a
       
   473   True
       
   474   >>> c = (1,)
       
   475   >>> c * 5
       
   476   (1, 1, 1, 1, 1)
       
   477   >>> len(a)
       
   478   6
       
   479   >>> max(a)
       
   480   6
       
   481   >>> min(a)
       
   482   1
       
   483 
       
   484 However the following *List* operations are not supported by *Tuples* because
       
   485 *Tuples* cannot be changed once they are created:
       
   486 
       
   487   * Changing elements
       
   488   * Deleting elements
       
   489   * Assigning to slices
       
   490 
       
   491 Similarity to *Lists* leads to the questions like, why not *Lists* only? Why do
       
   492 we even want *Tuples*? Can we do the same with *Lists*? And the answer is **Yes**
       
   493 we can do it, but *Tuples* are helpful at times, like we can return Tuples from
       
   494 functions. They are also returned by some built-in functions and methods. And
       
   495 also there are some use cases like co-ordinate among other things. So *Tuples*
       
   496 are helpful.
       
   497 
       
   498 
       
   499 Conclusion
       
   500 ----------
       
   501 
       
   502 This section on *Lists* and *Tuples* introduces almost all the necessary 
       
   503 machinary required to work on *Lists* and *Tuples*. Topics like how to
       
   504 iterate through these data structures will be introduced in the later
       
   505 sections.
       
   506