getting-started-with-lists/script.rst.orig
changeset 442 a9b71932cbfa
parent 418 8a42b4203f6d
equal deleted inserted replaced
441:430035b678f7 442:a9b71932cbfa
     1 <?xml version="1.0" encoding="utf-8" ?>
     1 .. Objectives
     2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     2 .. ----------
     3 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
     3 
     4 <head>
     4 .. By the end of this tutorial, you will be able to
     5 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     5 
     6 <meta name="generator" content="Docutils 0.6: http://docutils.sourceforge.net/" />
     6 .. Create Lists.
     7 <title></title>
     7 .. Access List elements.
     8 <style type="text/css">
     8 .. Append elemets to list
     9 
     9 .. Delete list elemets
    10 /*
    10 
    11 :Author: David Goodger (goodger@python.org)
    11 .. 1. getting started with ipython 
    12 :Id: $Id: html4css1.css 5951 2009-05-18 18:03:10Z milde $
    12 
    13 :Copyright: This stylesheet has been placed in the public domain.
    13 
    14 
    14 
    15 Default cascading style sheet for the HTML output of Docutils.
    15 .. Prerequisites
    16 
    16 .. -------------
    17 See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to
    17 
    18 customize this style sheet.
    18 ..   1. getting started with strings
    19 */
    19 ..   #. getting started with lists
    20 
    20 ..   #. basic datatypes
    21 /* used to remove borders from tables and images */
    21      
    22 .borderless, table.borderless td, table.borderless th {
    22 .. Author              : Amit 
    23   border: 0 }
    23    Internal Reviewer   : Anoop Jacob Thomas <anoop@fossee.in>
    24 
    24    External Reviewer   :
    25 table.borderless td, table.borderless th {
    25    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
    26   /* Override padding for "table.docutils td" with "! important".
    26 
    27      The right padding separates the table cells. */
    27 .. #[[Anoop: Slides contain only outline and summary
    28   padding: 0 0.5em 0 0 ! important }
    28 
    29 
    29 Script
    30 .first {
    30 ------
    31   /* Override more specific margin styles with "! important". */
    31  {{{ Show the slide containing title }}}
    32   margin-top: 0 ! important }
    32 
    33 
    33 Hello friends and welcome to the tutorial on getting started with
    34 .last, .with-subtitle {
    34 lists.
    35   margin-bottom: 0 ! important }
    35 
    36 
    36  {{{ Show the slide containing the outline slide }}}
    37 .hidden {
    37 
    38   display: none }
    38 In this tutorial we will be getting acquainted with a python data
    39 
    39 structure called lists.  We will learn ::
    40 a.toc-backref {
    40  
    41   text-decoration: none ;
    41  * How to create lists
    42   color: black }
    42  * Structure of lists
    43 
    43  * Access list elements
    44 blockquote.epigraph {
    44  * Append elements to lists
    45   margin: 2em 5em ; }
    45  * Delete elements from lists
    46 
    46 
    47 dl.docutils dd {
    47 List is a compound data type, it can contain data of other data
    48   margin-bottom: 0.5em }
    48 types. List is also a sequence data type, all the elements are in
    49 
    49 order and the order has a meaning.
    50 /* Uncomment (and remove this text!) to get bold-faced definition list terms
    50 
    51 dl.docutils dt {
    51 .. #[[Anoop: "all the elements are in order and **there** order has a
    52   font-weight: bold }
    52    meaning." - I guess something is wrong here, I am not able to
    53 */
    53    follow this.]]
    54 
    54 
    55 div.abstract {
    55 We will first create an empty list with no elements. On your IPython
    56   margin: 2em 5em }
    56 shell type ::
    57 
    57 
    58 div.abstract p.topic-title {
    58    empty = [] 
    59   font-weight: bold ;
    59    type(empty)
    60   text-align: center }
    60    
    61 
    61 
    62 div.admonition, div.attention, div.caution, div.danger, div.error,
    62 This is an empty list without any elements.
    63 div.hint, div.important, div.note, div.tip, div.warning {
    63 
    64   margin: 2em ;
    64 .. #[[Anoop: the document has to be continous, without any
    65   border: medium outset ;
    65    subheadings, removing * Filled lists]]
    66   padding: 1em }
    66 
    67 
    67 Lets now see how to define a non-empty list. We do it as,::
    68 div.admonition p.admonition-title, div.hint p.admonition-title,
    68 
    69 div.important p.admonition-title, div.note p.admonition-title,
    69      nonempty = ['spam', 'eggs', 100, 1.234]
    70 div.tip p.admonition-title {
    70 
    71   font-weight: bold ;
    71 Thus the simplest way of creating a list is typing out a sequence 
    72   font-family: sans-serif }
    72 of comma-separated values (items) between square brackets. 
    73 
    73 All the list items need not be of the same data type.
    74 div.attention p.admonition-title, div.caution p.admonition-title,
    74 
    75 div.danger p.admonition-title, div.error p.admonition-title,
    75 As we can see lists can contain different kinds of data. In the
    76 div.warning p.admonition-title {
    76 previous example 'spam' and 'eggs' are strings and 100 and 1.234 are
    77   color: red ;
    77 integer and float. Thus we can put elements of heterogenous types in
    78   font-weight: bold ;
    78 lists including list itself.
    79   font-family: sans-serif }
    79 
    80 
    80 .. #[[Anoop: the sentence "Thus list themselves can be one of the
    81 /* Uncomment (and remove this text!) to get reduced vertical space in
    81    element types possible in lists" is not clear, rephrase it.]]
    82    compound paragraphs.
    82 
    83 div.compound .compound-first, div.compound .compound-middle {
    83 Example ::
    84   margin-bottom: 0.5em }
    84 
    85 
    85       listinlist=[[4,2,3,4],'and', 1, 2, 3, 4]
    86 div.compound .compound-last, div.compound .compound-middle {
    86 
    87   margin-top: 0.5em }
    87 We access list elements using the index. The index begins from 0. So
    88 */
    88 for list nonempty, nonempty[0] gives the first element, nonempty[1]
    89 
    89 the second element and so on and nonempty[3] the last element. ::
    90 div.dedication {
    90 
    91   margin: 2em 5em ;
    91 	    nonempty[0] 
    92   text-align: center ;
    92 	    nonempty[1] 
    93   font-style: italic }
    93 	    nonempty[3]
    94 
    94 
    95 div.dedication p.topic-title {
    95 Following is an exercise that you must do. 
    96   font-weight: bold ;
    96 
    97   font-style: normal }
    97 %% %% What happens when you do nonempty[-1]. 
    98 
    98 
    99 div.figure {
    99 Please, pause the video here. Do the exercise and then continue.  
   100   margin-left: 2em ;
   100 
   101   margin-right: 2em }
   101 .. #[[Anoop: was negative indices introduced earlier, if not may be we
   102 
   102    can ask them to try out nonempty[-1] and see what happens and then
   103 div.footer, div.header {
   103    tell that it gives the last element in the list.]]
   104   clear: both;
   104 
   105   font-size: smaller }
   105 As you can see you get the last element which is 1.234.
   106 
   106 
   107 div.line-block {
   107 
   108   display: block ;
   108 In python negative indices are used to access elements from the end::
   109   margin-top: 1em ;
   109    
   110   margin-bottom: 1em }
   110    nonempty[-1] 
   111 
   111    nonempty[-2] 
   112 div.line-block div.line-block {
   112    nonempty[-4]
   113   margin-top: 0 ;
   113 
   114   margin-bottom: 0 ;
   114 -1 gives the last element which is the 4th element , -2 second to last
   115   margin-left: 1.5em }
   115 and -4 gives the fourth from last element which is first element.
   116 
   116 
   117 div.sidebar {
   117 We can append elements to the end of a list using append command. ::
   118   margin: 0 0 0.5em 1em ;
   118 
   119   border: medium outset ;
   119    nonempty.append('onemore') 
   120   padding: 1em ;
   120    nonempty
   121   background-color: #ffffee ;
   121    nonempty.append(6) 
   122   width: 40% ;
   122    nonempty
   123   float: right ;
   123    
   124   clear: right }
   124 Following are  exercises that you must do. 
   125 
   125 
   126 div.sidebar p.rubric {
   126 %% %% What is the syntax to get the element 'and' 
   127   font-family: sans-serif ;
   127 in the list,listinlist ?
   128   font-size: medium }
   128 
   129 
   129 
   130 div.system-messages {
   130 %% %% How would you get 'and' using negative indices?
   131   margin: 5em }
   131 
   132 
   132 Please, pause the video here. Do the exercise and then continue.  
   133 div.system-messages h1 {
   133 
   134   color: red }
   134 The solution is on your screen
   135 
   135 
   136 div.system-message {
   136 
   137   border: medium outset ;
   137 As we can see non empty appends 'onemore' and 6 at the end.
   138   padding: 1em }
   138 
   139 
   139 Using len function we can check the number of elements in the list
   140 div.system-message p.system-message-title {
   140 nonempty. In this case it 6 ::
   141   color: red ;
   141 	 
   142   font-weight: bold }
   142 	 len(nonempty)
   143 
   143 
   144 div.topic {
   144 
   145   margin: 2em }
   145 
   146 
   146 Just like we can append elements to a list we can also remove them.
   147 h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,
   147 There are two ways of doing it. One is by using index. ::
   148 h4.section-subtitle, h5.section-subtitle, h6.section-subtitle {
   148 
   149   margin-top: 0.4em }
   149       del(nonempty[1])
   150 
   150 
   151 h1.title {
   151 
   152   text-align: center }
   152 
   153 
   153 deletes the element at index 1, 'eggs' which is the second element of
   154 h2.subtitle {
   154 the list. The other way is removing element by content. Lets say one
   155   text-align: center }
   155 wishes to delete 100 from nonempty list the syntax of the command
   156 
   156 should be
   157 hr.docutils {
   157 
   158   width: 75% }
   158 .. #[[Anoop: let x = [1,2,1,3]
   159 
   159    	     now x.remove(x[2])
   160 img.align-left, .figure.align-left{
   160 	     still x is [2,1,3] so that is not the way to remove
   161   clear: left ;
   161 	     element by index, it removed first occurrence of 1(by
   162   float: left ;
   162 	     content) and not based on index, so make necessary
   163   margin-right: 1em }
   163 	     changes]]
   164 
   164 
   165 img.align-right, .figure.align-right {
   165 ::
   166   clear: right ;
   166 
   167   float: right ;
   167     nonempty.remove(100)
   168   margin-left: 1em }
   168 
   169 
   169 but what if there were two 100's. To check that lets do a small
   170 .align-left {
   170 experiment. ::
   171   text-align: left }
   171 
   172 
   172 	   nonempty.append('spam') 
   173 .align-center {
   173 	   nonempty
   174   clear: both ;
   174 	   nonempty.remove('spam') 
   175   text-align: center }
   175 	   nonempty
   176 
   176 
   177 .align-right {
   177 If we check now we will see that the first occurence 'spam' is removed
   178   text-align: right }
   178 thus remove removes the first occurence of the element in the sequence
   179 
   179 and leaves others untouched.
   180 /* reset inner alignment in figures */
   180 
   181 div.align-right {
   181 
   182   text-align: left }
   182 
   183 
   183 
   184 /* div.align-center * { */
   184 
   185 /*   text-align: left } */
   185 .. #[[Anoop: does it have two spams or two pythons?]]
   186 
   186 
   187 ol.simple, ul.simple {
   187 .. #[[Anoop: there are no exercises/solved problems in this script,
   188   margin-bottom: 1em }
   188    add them]]
   189 
   189 
   190 ol.arabic {
   190 Following are  exercises that you must do. 
   191   list-style: decimal }
   191 
   192 
   192 %% %% Remove the third element from the list, listinlist.   
   193 ol.loweralpha {
   193 
   194   list-style: lower-alpha }
   194 %% %% Remove 'and' from the list, listinlist.
   195 
   195 
   196 ol.upperalpha {
   196 Please, pause the video here. Do the exercise and then continue.  
   197   list-style: upper-alpha }
   197 
   198 
   198 
   199 ol.lowerroman {
   199 
   200   list-style: lower-roman }
   200 {{{Slide for Summary }}}
   201 
   201 
   202 ol.upperroman {
   202 
   203   list-style: upper-roman }
   203 In this tutorial we came across a sequence data type called lists. ::
   204 
   204 
   205 p.attribution {
   205  * We learned how to create lists.  
   206   text-align: right ;
   206  * How to access lists.
   207   margin-left: 50% }
   207  * Append elements to list.
   208 
   208  * Delete Element from list.  
   209 p.caption {
   209  * And Checking list length.
   210   font-style: italic }
   210  
   211 
   211 
   212 p.credits {
   212 
   213   font-style: italic ;
   213 {{{ show Sponsored by Fossee Slide }}}
   214   font-size: smaller }
   214 
   215 
   215 This tutorial was created as a part of FOSSEE project.
   216 p.label {
   216 
   217   white-space: nowrap }
   217 I hope you found this tutorial useful.
   218 
   218 
   219 p.rubric {
   219 Thank You
   220   font-weight: bold ;
   220 
   221   font-size: larger ;
   221 ..
   222   color: maroon ;
   222  * Author : Amit Sethi 
   223   text-align: center }
   223  * First Reviewer : 
   224 
   224  * Second Reviewer : Nishanth
   225 p.sidebar-title {
       
   226   font-family: sans-serif ;
       
   227   font-weight: bold ;
       
   228   font-size: larger }
       
   229 
       
   230 p.sidebar-subtitle {
       
   231   font-family: sans-serif ;
       
   232   font-weight: bold }
       
   233 
       
   234 p.topic-title {
       
   235   font-weight: bold }
       
   236 
       
   237 pre.address {
       
   238   margin-bottom: 0 ;
       
   239   margin-top: 0 ;
       
   240   font: inherit }
       
   241 
       
   242 pre.literal-block, pre.doctest-block {
       
   243   margin-left: 2em ;
       
   244   margin-right: 2em }
       
   245 
       
   246 span.classifier {
       
   247   font-family: sans-serif ;
       
   248   font-style: oblique }
       
   249 
       
   250 span.classifier-delimiter {
       
   251   font-family: sans-serif ;
       
   252   font-weight: bold }
       
   253 
       
   254 span.interpreted {
       
   255   font-family: sans-serif }
       
   256 
       
   257 span.option {
       
   258   white-space: nowrap }
       
   259 
       
   260 span.pre {
       
   261   white-space: pre }
       
   262 
       
   263 span.problematic {
       
   264   color: red }
       
   265 
       
   266 span.section-subtitle {
       
   267   /* font-size relative to parent (h1..h6 element) */
       
   268   font-size: 80% }
       
   269 
       
   270 table.citation {
       
   271   border-left: solid 1px gray;
       
   272   margin-left: 1px }
       
   273 
       
   274 table.docinfo {
       
   275   margin: 2em 4em }
       
   276 
       
   277 table.docutils {
       
   278   margin-top: 0.5em ;
       
   279   margin-bottom: 0.5em }
       
   280 
       
   281 table.footnote {
       
   282   border-left: solid 1px black;
       
   283   margin-left: 1px }
       
   284 
       
   285 table.docutils td, table.docutils th,
       
   286 table.docinfo td, table.docinfo th {
       
   287   padding-left: 0.5em ;
       
   288   padding-right: 0.5em ;
       
   289   vertical-align: top }
       
   290 
       
   291 table.docutils th.field-name, table.docinfo th.docinfo-name {
       
   292   font-weight: bold ;
       
   293   text-align: left ;
       
   294   white-space: nowrap ;
       
   295   padding-left: 0 }
       
   296 
       
   297 h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,
       
   298 h4 tt.docutils, h5 tt.docutils, h6 tt.docutils {
       
   299   font-size: 100% }
       
   300 
       
   301 ul.auto-toc {
       
   302   list-style-type: none }
       
   303 
       
   304 </style>
       
   305 </head>
       
   306 <body>
       
   307 <div class="document">
       
   308 
       
   309 
       
   310 <div class="section" id="objective-questions">
       
   311 <h1>Objective Questions</h1>
       
   312 <!-- A mininum of 8 questions here (along with answers) -->
       
   313 <ol class="arabic">
       
   314 <li><p class="first">How do you create an empty list?</p>
       
   315 <pre class="literal-block">
       
   316 empty=[]
       
   317 </pre>
       
   318 </li>
       
   319 <li><p class="first">What is the most important property of sequence data types like lists?</p>
       
   320 <p>The elements are in order and can be accessed by index numbers.</p>
       
   321 </li>
       
   322 <li><p class="first">Can you have a list inside a list ?</p>
       
   323 <p>Yes,List can contain all the other data types, including list.</p>
       
   324 <p>Example:
       
   325 list_in_list=[2.3,[2,4,6],'string,'all datatypes can be there']</p>
       
   326 </li>
       
   327 <li><p class="first">What is the index number of the first element in a list?</p>
       
   328 <p>0
       
   329 nonempty = ['spam', 'eggs', 100, 1.234]
       
   330 nonempty[0]</p>
       
   331 </li>
       
   332 <li><p class="first">How would you access the end of a list without finding its length?</p>
       
   333 <p>Using negative indices. We can the list from the end using negative indices.</p>
       
   334 <p>::
       
   335 nonempty = ['spam', 'eggs', 100, 1.234]
       
   336 nonempty[-1]</p>
       
   337 </li>
       
   338 <li><p class="first">What is the function to find the length of a list?</p>
       
   339 <p>len</p>
       
   340 </li>
       
   341 <li><p class="first">Delete the last element from list sq=[5,4,3,2,1,0]</p>
       
   342 <p>del(sq[-1])</p>
       
   343 </li>
       
   344 <li><p class="first">How many will you have to use remove function to remove all 6's from the given list sq=[2,5,6,7,6,4,6]?</p>
       
   345 <p>3</p>
       
   346 </li>
       
   347 </ol>
       
   348 </div>
       
   349 <div class="section" id="larger-questions">
       
   350 <h1>Larger Questions</h1>
       
   351 <!-- A minimum of 2 questions here (along with answers) -->
       
   352 <p>1. Add all elemets of seq1=['e','f','g','h']
       
   353 to the sequence seq=['a','b','c','d']</p>
       
   354 <ol class="arabic simple" start="2">
       
   355 <li>Delete all elements of seq1=[3,5,6] from sequence
       
   356 seq=[1,2,3,4,5,6,7,8,9]</li>
       
   357 </ol>
       
   358 </div>
       
   359 </div>
       
   360 </body>
       
   361 </html>