basic_python/intro.rst
changeset 49 5b5c728cd47f
parent 41 b81255d096ab
child 50 0985027f4c8c
equal deleted inserted replaced
43:d42ceb4ad9fa 49:5b5c728cd47f
    89 interpreter is one of the most integral features of Python. The prompt obtained
    89 interpreter is one of the most integral features of Python. The prompt obtained
    90 when the interactive interpreter is similar to what is shown below. The exact
    90 when the interactive interpreter is similar to what is shown below. The exact
    91 appearance might differ based on the version of Python being used. The ``>>>``
    91 appearance might differ based on the version of Python being used. The ``>>>``
    92 thing shown is the python prompt. When something is typed at the prompt and the
    92 thing shown is the python prompt. When something is typed at the prompt and the
    93 enter key is hit, the python interpreter interprets the command entered and
    93 enter key is hit, the python interpreter interprets the command entered and
    94 performs the appropriate action.
    94 performs the appropriate action. All the examples presented in this document are
       
    95 to be tried hands on, on the interactive interpreter.
    95 
    96 
    96 ::
    97 ::
    97 
    98 
    98   Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49) 
    99   Python 2.5.2 (r252:60911, Oct  5 2008, 19:24:49) 
    99   [GCC 4.3.2] on linux2
   100   [GCC 4.3.2] on linux2
   194 ::
   195 ::
   195   
   196   
   196   This example is to show that unlike in C or C++ there is no limit on the
   197   This example is to show that unlike in C or C++ there is no limit on the
   197   value of an integer.
   198   value of an integer.
   198 
   199 
       
   200 Try this on the interactive interpreter:
       
   201 ``import this``
       
   202 
       
   203 *Hint: The output gives an idea of Power of Python*
       
   204 
   199 *ipython* - An enhanced interactive Python interpreter
   205 *ipython* - An enhanced interactive Python interpreter
   200 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   206 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   201 
   207 
   202 The power and the importance of the interactive interpreter was the highlight
   208 The power and the importance of the interactive interpreter was the highlight
   203 of the previous section. This section provides insight into the enhanced
   209 of the previous section. This section provides insight into the enhanced
   275       return gcd(y, x%y)
   281       return gcd(y, x%y)
   276   
   282   
   277     print gcd(72, 92)
   283     print gcd(72, 92)
   278 
   284 
   279 To run the script, open the shell prompt, navigate to the directory that 
   285 To run the script, open the shell prompt, navigate to the directory that 
   280 contains the python file and run `python <filename.py>` at the prompt ( in this 
   286 contains the python file and run ``python <filename.py>`` at the prompt ( in this 
   281 case filename is gcd.py )
   287 case filename is gcd.py )
   282 
   288 
   283 **Running the python script**
   289 **Running the python script**
   284 ::
   290 ::
   285   
   291   
   287   4
   293   4
   288   $ 
   294   $ 
   289 
   295 
   290 Another method to run a python script would be to include the line
   296 Another method to run a python script would be to include the line
   291 
   297 
   292 `#! /usr/bin/python`
   298 ``#! /usr/bin/python``
   293 
   299 
   294 at the beginning of the python file and then make the file executable by 
   300 at the beginning of the python file and then make the file executable by 
   295 
   301 
   296 $ chmod a+x *filename.py*
   302 $ chmod a+x *filename.py*
   297 
   303 
   338 Long numbers are the same as integers in almost all aspects. They can be used in
   344 Long numbers are the same as integers in almost all aspects. They can be used in
   339 operations just like integers and along with integers without any distinction.
   345 operations just like integers and along with integers without any distinction.
   340 The only distinction comes during type checking (which is not a healthy practice).
   346 The only distinction comes during type checking (which is not a healthy practice).
   341 Long numbers are tucked with a trailing 'L' just to signify that they are long.
   347 Long numbers are tucked with a trailing 'L' just to signify that they are long.
   342 Notice that in the example just lng at the prompt displays the value of the variable
   348 Notice that in the example just lng at the prompt displays the value of the variable
   343 with the 'L' whereas `print lng` displays without the 'L'. This is because print 
   349 with the 'L' whereas ``print lng`` displays without the 'L'. This is because print 
   344 formats the output before printing. Also in the example, notice that adding an 
   350 formats the output before printing. Also in the example, notice that adding an 
   345 integer to a long does not give any errors and the result is as expected. So for
   351 integer to a long does not give any errors and the result is as expected. So for
   346 all practical purposes longs can be treated as ints.
   352 all practical purposes longs can be treated as ints.
   347 
   353 
   348 Eg 8:
   354 Eg 8:
   407   later stage be used as a float variable as well.
   413   later stage be used as a float variable as well.
   408 
   414 
   409 Strings
   415 Strings
   410 ~~~~~~~
   416 ~~~~~~~
   411 
   417 
       
   418 Strings are one of the essential data structures of any programming language.
       
   419 The ``print "Hello, World!"`` program was introduced in the earlier section, and
       
   420 the *"Hello, World!"* in the print statement is a string. A string is basically 
       
   421 a set of characters. Strings can be represented in various ways shown below:
       
   422 
       
   423 ::
       
   424 
       
   425   s = 'this is a string'              # a string variable can be represented using single quotes
       
   426   s = 'This one has "quotes" inside!' # The string can have quotes inside it as shown
       
   427   s = "I have 'single-quotes' inside!"
       
   428   l = "A string spanning many lines\
       
   429   one more line\
       
   430   yet another"                        # a string can span more than a single line.
       
   431   t = """A triple quoted string does  # another way of representing multiline strings.
       
   432   not need to be escaped at the end and
       
   433   "can have nested quotes" etc."""
       
   434 
       
   435 Try the following on the interpreter:
       
   436 ``s = 'this is a string with 'quotes' of similar kind'``
       
   437 
       
   438 **Exercise: How to use single quotes within single quotes in a string as shown 
       
   439 in the above example without getting an error?**
       
   440 
       
   441 String operations
       
   442 -----------------
       
   443 
       
   444 A few basic string operations are presented here. 
       
   445 
       
   446 **String concatenation**
       
   447 String concatenation is done by simple addition of two strings.
       
   448 
       
   449 ::
       
   450 
       
   451   >>> x = 'Hello'
       
   452   >>> y = ' Python'
       
   453   >>> print x+y
       
   454   Hello Python
       
   455 
       
   456 *Try this yourself:*
       
   457 
       
   458 ::
       
   459   
       
   460   >>> somenum = 13
       
   461   >>> print x+somenum
       
   462 
       
   463 The problem with the above example is that here a string variable and an integer
       
   464 variable are trying to be concantenated. To obtain the desired result from the 
       
   465 above example the str(), repr() and the `` can be used.
       
   466 
       
   467 **str()** simply converts a value to a string in a reasonable form.
       
   468 **repr()** creates a string that is a representation of the value.
       
   469 
       
   470 The difference can be seen in the example shown below:
       
   471 
       
   472 ::
       
   473   
       
   474   >>> str(1000000000000000000000000000000000000000000000000L)
       
   475   '1000000000000000000000000000000000000000000000000'
       
   476   >>> repr(1000000000000000000000000000000000000000000000000L)
       
   477   '1000000000000000000000000000000000000000000000000L'
       
   478 
       
   479 It can be observed that the 'L' in the long value shown was omitted by str(), 
       
   480 whereas repr() converted that into a string too. An alternative way of using 
       
   481 repr(value) is ```value```. 
       
   482 
       
   483 A few more examples:
       
   484 ::
       
   485   
       
   486   >>> x = "Let's go \nto Pycon"
       
   487   >>> print x
       
   488   Let's go 
       
   489   to Pycon
       
   490 
       
   491 In the above example, notice that the '\n'(newline) character is formatted and 
       
   492 the string is printed on two lines. The strings discussed until now were normal 
       
   493 strings. Other than these there are two other types of strings namely, raw strings
       
   494 and unicode strings.
       
   495 
       
   496 **Raw strings** are strings which are unformatted, that is the backslashes(\) are 
       
   497 not parsed and are left as it is in the string. Raw strings are represented with
       
   498 an 'r' at the start of a string. 
       
   499 Let us look at an example
       
   500 
       
   501 ::
       
   502   
       
   503   >>> x = r"Let's go \nto Pycon"
       
   504   >>> print x
       
   505   Let's go \nto Pycon
       
   506 
       
   507 Note: The '\n' is not being parsed into a new line and is left as it is.
       
   508 
       
   509 *Try this yourself:*
       
   510 
       
   511 ::
       
   512   
       
   513   >>> x = r"Let's go to Pycon\"
       
   514 
       
   515 **Unicode strings** are strings where the characters are Unicode characters as 
       
   516 opposed to ASCII characters. Unicode strings are represented with a 'u' at the 
       
   517 start of the string.
       
   518 Let us look at an example:
       
   519 
       
   520 ::
       
   521   
       
   522   >>> x = u"Let's go to Pycon!"
       
   523   >>> print x
       
   524   Let's go to Pycon!
       
   525 
       
   526 Boolean
       
   527 ~~~~~~~
       
   528 
       
   529 Python also provides special Boolean datatype. A boolean variable can assume a 
       
   530 value of either *True* or *False* (Note the capitalizations). 
       
   531 
       
   532 Let us look at examples:
       
   533 
       
   534 ::
       
   535 
       
   536   >>> t = True
       
   537   >>> f = not t
       
   538   >>> print f
       
   539   False
       
   540   >>> f or t
       
   541   True
       
   542   >>> f and t
       
   543   False
       
   544 
       
   545 The **while** loop
       
   546 ~~~~~~~~~~~~~~~~~~
       
   547 
       
   548 The Python **while** loop is similar to the C/C++ while loop. The syntax is as
       
   549 follows:
       
   550 
       
   551 ::
       
   552 
       
   553   statement 0
       
   554   while condition:
       
   555     statement 1 #while block
       
   556     statement 2 #while block
       
   557   statement 3 #outside the while block.
       
   558 
       
   559 Let us look at an example:
       
   560 
       
   561 ::
       
   562   
       
   563