basic_python/intro.rst
changeset 65 0f25f22a2725
parent 52 9748190df418
child 67 5076574b7b83
equal deleted inserted replaced
53:76facb1dc81b 65:0f25f22a2725
   541   True
   541   True
   542   >>> f and t
   542   >>> f and t
   543   False
   543   False
   544 
   544 
   545 The **while** loop
   545 The **while** loop
   546 ~~~~~~~~~~~~~~~~~~
   546 ==================
       
   547 
   547 
   548 
   548 The Python **while** loop is similar to the C/C++ while loop. The syntax is as
   549 The Python **while** loop is similar to the C/C++ while loop. The syntax is as
   549 follows:
   550 follows:
   550 
   551 
   551 ::
   552 ::
   570     3
   571     3
   571     4
   572     4
   572     5
   573     5
   573 
   574 
   574 The **if** conditional
   575 The **if** conditional
   575 ~~~~~~~~~~~~~~~~~~~~~~
   576 ======================
   576 
   577 
   577 The Python **if** block provides the conditional execution of statements. 
   578 The Python **if** block provides the conditional execution of statements. 
   578 If the condition evaluates as true the block of statements defined under the if 
   579 If the condition evaluates as true the block of statements defined under the if 
   579 block are executed.
   580 block are executed.
   580 
   581 
   610          print n," is positive"
   611          print n," is positive"
   611          else:
   612          else:
   612          print n, " is 0"
   613          print n, " is 0"
   613 
   614 
   614 **raw_input()**
   615 **raw_input()**
   615 ~~~~~~~~~~~~~~~
   616 ===============
   616 
   617 
   617 In the previous example we saw the call to the raw_input() subroutine. 
   618 In the previous example we saw the call to the raw_input() subroutine. 
   618 The **raw_input()** method is used to take user inputs through the console.
   619 The **raw_input()** method is used to take user inputs through the console.
   619 Unlike **input()** which assumes the data entered by the user as a standard python
   620 Unlike **input()** which assumes the data entered by the user as a standard python
   620 expression, **raw_input()** treats all the input data as raw data and converts
   621 expression, **raw_input()** treats all the input data as raw data and converts
   683 
   684 
   684 Observe here that the variable *pal* is a string and hence integer operations
   685 Observe here that the variable *pal* is a string and hence integer operations
   685 cannot be performed on it. Hence the exception is raised.
   686 cannot be performed on it. Hence the exception is raised.
   686 
   687 
   687 **int()** method
   688 **int()** method
   688 ~~~~~~~~~~~~~~~~
   689 ================
   689 
   690 
   690 Generally for computing purposes, the data used is not strings or raw data but 
   691 Generally for computing purposes, the data used is not strings or raw data but 
   691 on integers, floats and similar mathematical data structures. The data obtained
   692 on integers, floats and similar mathematical data structures. The data obtained
   692 from **raw_input()** is raw data in the form of strings. In order to obtain integers
   693 from **raw_input()** is raw data in the form of strings. In order to obtain integers
   693 from strings we use the method **int()**. 
   694 from strings we use the method **int()**. 
   713   'Guido Van Rossum'
   714   'Guido Van Rossum'
   714   >>> numvar = int(stringvar)
   715   >>> numvar = int(stringvar)
   715 
   716 
   716 
   717 
   717 Functions in Python: **def**
   718 Functions in Python: **def**
   718 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   719 ============================
   719 
   720 
   720 *Functions* allow us to enclose a set of statements and call the function again
   721 *Functions* allow us to enclose a set of statements and call the function again
   721 and again instead of repeating the group of statements everytime. Functions also
   722 and again instead of repeating the group of statements everytime. Functions also
   722 allow us to isolate a piece of code from all the other code and provides the
   723 allow us to isolate a piece of code from all the other code and provides the
   723 convenience of not polluting the global variables.
   724 convenience of not polluting the global variables.
   807 
   808 
   808   >>> name = ['Mr.', 'Steve', 'Gosling']
   809   >>> name = ['Mr.', 'Steve', 'Gosling']
   809   >>> can_change(name)
   810   >>> can_change(name)
   810   >>> name
   811   >>> name
   811   ['Mr.', 'James', 'Gosling']
   812   ['Mr.', 'James', 'Gosling']
   812 
       
   813 If nothing is returned by the function explicitly, Python takes care to return
   813 If nothing is returned by the function explicitly, Python takes care to return
   814 None when the funnction is called.
   814 None when the funnction is called.
   815 
   815 
   816 Default Arguments
   816 Default Arguments
   817 -----------------
   817 -----------------