Diving Into Python: Chapter 7

Hello guys!
 
This article is the seventh part of this article series. This article explains lists and tuples in Python, the related operations and the functioning in detail using several examples and graphics. So let's explore it.
 
Getting Theme
 
To get the theme of Python, kindly go through my previous articles:
Lists
 
Lists are one of the most valuable Data Types in programming language and so they are in Python. One can easily see that lists are somehow similar to arrays but still, there are a huge difference between both of these data types, which are that an array contains homogeneous (of the same data type) items whereas lists contain heterogeneous (of various data types) items. This can also be considered to be an advantage of lists over arrays.
 
Now let's get back to lists. Lists contain items separated the commas and enclosed in brackets "[ ]". The values stored in a list can be accessed using "[:]" (depending on their respective index).
 
Structure
 
A simple structure of accessing elements using indexes is:
 
Print list[index]
 
Example
 
Now let's go through some examples.
  1. list = ['Flipkart''Amazon''SnapDeal''Myntra''Junglee']    
  2.    
  3. # Print Complete List    
  4.     
  5. print (list) 
Output- ['Flipkart', 'Amazon', 'SnapDeal', 'Myntra', 'Junglee']
  1. # Print Element at Index 0    
  2.     
  3. print (list[0])   
    Output- Flipkart
    1. # Prints Element from Index 1 to 3    
    2.     
    3. print (list[1:4])   
    Output- ['Amazon', 'SnapDeal', 'Myntra']
    1. # Prints Element Starting from Index 2    
    2.     
    3. print (list[2:])   
      Output- ['SnapDeal', 'Myntra', 'Junglee']
       
      (Now let's have a look at some of the operations over lists).
       
      Output Window
       
       
      List | Concatenation
       
      You can concatenate elements of two lists using the "+" operator as we do in strings.
       
      For Example:
      1. list1 = ['Flipkart''Amazon''SnapDeal']    
      2.     
      3. list2 = ['Myntra''Junglee']    
      4.    
      5. # Concatenating two Lists    
      6.     
      7. print (list1 + list2)   
        Output- ['Flipkart', 'Amazon', 'SnapDeal', 'Myntra', 'Junglee']
         
        Output Window
         
         
        List | Repetition
         
        For repeating a list's elements twice or more we can use the "*" operator in the same fashion as we do in strings.
         
        For Example:
        1. list1 = ['Flipkart''Amazon''SnapDeal']    
        2.     
        3. list2 = ['Myntra''Junglee']    
        4.    
        5. # Repetition in Lists    
        6.     
        7. print (2 * list2)   
          Output-  ['Myntra', 'Junglee', 'Myntra', 'Junglee']
           
          Output Window
           
           
          Tuples
           
          A tuple is another data type in Python that is similar to a list in some contexts. A tuple also contains a set of elements separated by a comma and enclosed between a pair of brackets "()".
           
          The values stored in a list can be accessed using "[:]" the same as a list depending on their respective index.
           
          Structure
           
          A simple structure of accessing elements using indexes is:
           
          print tuple[index]
           
          Example
           
          Let's go through some examples to have a better understanding:
          1. tuple = ('Flipkart''Amazon''SnapDeal''Myntra''Junglee')    
          2.    
          3. # Print Complete Tuple    
          4.     
          5. print (tuple)   
            Output- ('Flipkart', 'Amazon', 'SnapDeal', 'Myntra', 'Junglee')
            1. # Print Element at Index 0    
            2.     
            3. print (tuple [0])   
              Output- Flipkart
              1. # Prints Element from Index 1 to 3    
              2.     
              3. print (tuple[1:4])  
              Output- ('Amazon', 'SnapDeal', 'Myntra')
              1. # Prints Element Starting from Index 2    
              2.     
              3. print (tuple[2:])   
                Output- ('SnapDeal', 'Myntra', 'Junglee')
                 
                (Now let's move to some operations.)
                 
                Output Window
                 
                 
                Tuple | Concatenation
                 
                You can concatenate elements of two tuples using the "+" operator as we do in strings as well as in lists.
                 
                For Example:
                1. tuple1 = ('Flipkart''Amazon''SnapDeal')    
                2.     
                3. tuple2 = ('Myntra''Junglee')    
                4.    
                5. # Concatenating two Tuples    
                6.     
                7. print (tuple1 + tuple2)   
                  Output-  ('Flipkart', 'Amazon', 'SnapDeal', 'Myntra', 'Junglee')
                   
                  Output Window
                   
                   
                  Tuple | Repetition
                   
                  For repeating a list's elements twice or more we can use the "*" operator in the same fashion as we were doing in strings as well as in lists.
                   
                  For Example:
                  1. tuple1 = ('Flipkart''Amazon''SnapDeal')    
                  2.     
                  3. tuple2 = ('Myntra''Junglee')    
                  4.    
                  5. # Repetition in Tuples    
                  6.     
                  7. print (2 * tuple2)   
                  Output-  ('Myntra', 'Junglee', 'Myntra', 'Junglee')
                   
                  Output Window
                   
                   
                  PinPoints
                   
                   
                  Guys, perhaps you are thinking that lists and tuples look similar in function and are wondering how to differentiate them.
                  Let me provide you some pinpoints to get a clear view of them:
                  • Elements in Lists are enclosed between the brackets []
                  • Elements in Tuples are enclosed between parentheses ()
                  • Size and elements in a list can be changed
                  • Size and elements in a tuple can't be changed (Read-Only*)
                  (Still, if you guys didn't get the proper theme of tuples then don't worry, in my next article I'll explain tuples in greater detail.)
                   
                  Guidelines from my Side 
                   
                  The following are some guidelines from my side:
                  • Do as much code as you can.
                  • Code anything you want, that is the best way to learn
                  • Don't just study things, try to learn them.
                  • Work on your concepts
                  • Work on the fundamentals of any technology or stuff you want to learn.
                  Moto
                  “Keep calm and code Python”.
                   
                  I tried to make this an interesting and interactive article and I hope you guys like it, meanwhile if you have any suggestions then your welcome.
                   
                  Until the next part, keep sharing!
                   
                  Happy Learning!


                  Similar Articles