basic-data-type/script.rst
changeset 362 a77a27916f81
parent 337 c65d0d9fc0c8
child 364 91d16630c90f
equal deleted inserted replaced
361:a28d592851b4 362:a77a27916f81
     1 .. Objectives
     1 .. Objectives
     2 .. ----------
     2 .. ----------
     3 
     3 
     4 .. Learn about Python Data Structures and Operators.(Remembering)
     4 .. At the end of this tutorial, you should know --
     5 .. Use them to do basic operations.(Applying)
     5 
       
     6 .. 1. Learn about Python Data Structures and Operators.(Remembering)
       
     7 .. #.Use them to do basic operations.(Applying)
     6 
     8 
     7 .. Prerequisites
     9 .. Prerequisites
     8 .. -------------
    10 .. -------------
     9 
    11 
    10 
    12 .. None
    11      
    13      
    12 .. Author              : Amit Sethi
    14 .. Author              : Amit Sethi
    13    Internal Reviewer   : 
    15    Internal Reviewer   : 
    14    External Reviewer   :
    16    External Reviewer   :
    15    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
    17    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
   257 
   259 
   258 Now we will have a look at strings
   260 Now we will have a look at strings
   259 
   261 
   260 type :: 
   262 type :: 
   261 
   263 
   262  In[]: greeting_string="hello"
   264   greeting_string="hello"
   263 
   265 
   264 
   266 
   265 greeting_string is now a string variable with the value "hello"
   267 greeting_string is now a string variable with the value "hello"
   266 
   268 
   267 {{{ Memory Aid Slide }}}
   269 {{{ Memory Aid Slide }}}
   268 
   270 
   269 Python strings can actually be defined in three different ways ::
   271 Python strings can actually be defined in three different ways ::
   270 
   272 
   271   In[]: k='Single quote'
   273    k='Single quote'
   272   In[]: l="Double quote contain's single quote"
   274    l="Double quote contain's single quote"
   273   In[]: m='''"Contain's both"'''
   275    m='''"Contain's both"'''
   274 
   276 
   275 .. #[Puneeth: Contain's? That's not a word!]
   277 .. #[Puneeth: Contain's? That's not a word!]
   276 
   278 
   277 Thus, single quotes are used as delimiters usually.
   279 Thus, single quotes are used as delimiters usually.
   278 
   280 
   284 
   286 
   285 The last in the list of sequence data types is tuple.
   287 The last in the list of sequence data types is tuple.
   286 
   288 
   287 To create a tuple we use normal brackets '(' unlike '[' for lists.::
   289 To create a tuple we use normal brackets '(' unlike '[' for lists.::
   288 
   290 
   289   In[]: num_tuple = (1, 2, 3, 4, 5, 6, 7, 8)
   291    num_tuple = (1, 2, 3, 4, 5, 6, 7, 8)
   290   
   292   
   291 Because of their sequential property there are certain functions and
   293 Because of their sequential property there are certain functions and
   292 operations we can apply to all of them.
   294 operations we can apply to all of them.
   293 
   295 
   294 
   296 
   295 
   297 
   296 The first one is accessing.
   298 The first one is accessing.
   297 
   299 
   298 They can be accessed using index numbers ::
   300 They can be accessed using index numbers ::
   299 
   301 
   300   In[]: num_list[2]
   302    num_list[2]
   301   In[]: num_list[-1]
   303    num_list[-1]
   302   In[]: greeting_string[1]
   304    greeting_string[1]
   303   In[]: greeting_string[3]
   305    greeting_string[3]
   304   In[]: greeting_string[-2]
   306    greeting_string[-2]
   305   In[]: num_tuple[2]
   307    num_tuple[2]
   306   In[]: num_tuple[-3]
   308    num_tuple[-3]
   307 
   309 
   308 
   310 
   309 Indexing starts from 0 from left to right and from -1 when accessing lists
   311 Indexing starts from 0 from left to right and from -1 when accessing lists
   310 in reverse. Thus num_list[2] refers to the third element 3. and greetings
   312 in reverse. Thus num_list[2] refers to the third element 3. and greetings
   311 [-2] is the second element from the end , that is 'l'.
   313 [-2] is the second element from the end , that is 'l'.
   312 
   314 
   313 
   315 
   314 
   316 
   315 Addition gives a new sequence containing both sequences ::
   317 Addition gives a new sequence containing both sequences ::
   316 
   318 
   317      In[]: num_list+var_list
   319       num_list+var_list
   318      In[]: a_string="another string"
   320       a_string="another string"
   319      In[]: greeting_string+a_string
   321       greeting_string+a_string
   320      In[]: t2=(3,4,6,7)
   322       t2=(3,4,6,7)
   321      In[]: num_tuple+t2
   323       num_tuple+t2
   322 
   324 
   323 len function gives the length ::
   325 len function gives the length ::
   324 
   326 
   325   In[]: len(num_list)
   327    len(num_list)
   326   In[]: len(greeting_string)
   328    len(greeting_string)
   327   In[]: len(num_tuple)
   329    len(num_tuple)
   328 
   330 
   329 Prints the length the variable.
   331 Prints the length the variable.
   330 
   332 
   331 We can check the containership of an element using the 'in' keyword ::
   333 We can check the containership of an element using the 'in' keyword ::
   332 
   334 
   333   In[]: 3 in num_list
   335    3 in num_list
   334   In[]: 'H' in greeting_string
   336    'H' in greeting_string
   335   In[]: 2 in num_tuple
   337    2 in num_tuple
   336 
   338 
   337 We see that it gives True and False accordingly.
   339 We see that it gives True and False accordingly.
   338 
   340 
   339 Find maximum using max function and minimum using min::
   341 Find maximum using max function and minimum using min::
   340 
   342 
   341   In[]: max(num_tuple)
   343    max(num_tuple)
   342   In[]: min(greeting_string)
   344    min(greeting_string)
   343 
   345 
   344 Get a sorted list and reversed list using sorted and reversed function ::
   346 Get a sorted list and reversed list using sorted and reversed function ::
   345 
   347 
   346   In[]: sorted(num_list)
   348    sorted(num_list)
   347   In[]: reversed(greeting_string)
   349    reversed(greeting_string)
   348 
   350 
   349 As a consequence of the order one we access a group of elements together.
   351 As a consequence of the order one we access a group of elements together.
   350 This is called slicing and striding.
   352 This is called slicing and striding.
   351 
   353 
   352 .. #[Puneeth: Fix the sentence above. ]
   354 .. #[Puneeth: Fix the sentence above. ]
   353 
   355 
   354 First Slicing 
   356 First Slicing 
   355 
   357 
   356 Given a list ::
   358 Given a list ::
   357 
   359 
   358   In[]:j=[1,2,3,4,5,6]
   360   j=[1,2,3,4,5,6]
   359 
   361 
   360 Lets say we want elements starting from 2 and ending in 5.
   362 Lets say we want elements starting from 2 and ending in 5.
   361 
   363 
   362 For this we can do ::
   364 For this we can do ::
   363 
   365 
   364   In[]: j[1:4]
   366    j[1:4]
   365 
   367 
   366 The syntax for slicing is, sequence variable name square bracket first
   368 The syntax for slicing is, sequence variable name square bracket first
   367 element index, colon, second element index. The last element however is not
   369 element index, colon, second element index. The last element however is not
   368 included in the resultant list::
   370 included in the resultant list::
   369 
   371 
   370 
   372 
   371   In[]: j[:4]
   373    j[:4]
   372 
   374 
   373 If first element is left blank default is from beginning and if last
   375 If first element is left blank default is from beginning and if last
   374 element is left blank it means till the end.
   376 element is left blank it means till the end.
   375 
   377 
   376 ::
   378 ::
   377 
   379 
   378  In[]: j[1:]
   380   j[1:]
   379 
   381 
   380  In[]: j[:]
   382   j[:]
   381 
   383 
   382 This effectively is the whole list.
   384 This effectively is the whole list.
   383 
   385 
   384 Striding is similar to slicing except that the step size here is not one.
   386 Striding is similar to slicing except that the step size here is not one.
   385 
   387 
   395 
   397 
   396 We have talked about many similar features of lists, strings and tuples.
   398 We have talked about many similar features of lists, strings and tuples.
   397 But there are many important features in lists that differ from strings and
   399 But there are many important features in lists that differ from strings and
   398 tuples. Lets see this by example.::
   400 tuples. Lets see this by example.::
   399 
   401 
   400   In[]: new_num_list[1]=9
   402    new_num_list[1]=9
   401   In[]: greeting_string[1]='k'
   403    greeting_string[1]='k'
   402 
   404 
   403 {{{ slide to show the error }}}
   405 {{{ slide to show the error }}}
   404 
   406 
   405 
   407 
   406 
   408 
   407 As you can see while the first command executes with out a problem there is
   409 As you can see while the first command executes with out a problem there is
   408 an error on the second one.
   410 an error on the second one.
   409   
   411   
   410 Now lets try ::
   412 Now lets try ::
   411 
   413 
   412   In[]: new_tuple[1]=5
   414    new_tuple[1]=5
   413 
   415 
   414 Its the same error. This is because strings and tuples share the property
   416 Its the same error. This is because strings and tuples share the property
   415 of being immutable. We cannot change the value at a particular index just
   417 of being immutable. We cannot change the value at a particular index just
   416 by assigning a new value at that position.
   418 by assigning a new value at that position.
   417 
   419 
   471 
   473 
   472   In: otherstring.split(',')
   474   In: otherstring.split(',')
   473 
   475 
   474 join function does the opposite. Joins a list to make a string.::
   476 join function does the opposite. Joins a list to make a string.::
   475 
   477 
   476   In[]:','.join['List','joined','on','commas']
   478   ','.join['List','joined','on','commas']
   477 
   479 
   478 Thus we get a list joined on commas. Similarly we can do spaces.::
   480 Thus we get a list joined on commas. Similarly we can do spaces.::
   479 
   481 
   480   In[]:' '.join['Now','on','spaces']
   482   ' '.join['Now','on','spaces']
   481 
   483 
   482 Note that the list has to be a list of strings to apply join operation.
   484 Note that the list has to be a list of strings to apply join operation.
   483 
   485 
   484 With this we come to the end of this tutorial .
   486 With this we come to the end of this tutorial .
   485 
   487