basic-python.txt
changeset 128 fa5c77536e4e
parent 127 76fd286276f7
child 129 dcb9b50761eb
child 146 b92b4e7ecd7b
equal deleted inserted replaced
127:76fd286276f7 128:fa5c77536e4e
     1 *Script
       
     2 
       
     3 
       
     4 *Hello and welcome to this tutorial on Basic Python using Python.
       
     5 
       
     6 This tutorial formally introduces Python as a language . Through this tutorial we will be able to understand Basic Data types like number , Boolean and strings .Some basic operators , simple input/output and basic conditional flow . 
       
     7 
       
     8 In numbers Python supports three kinds of data types ,
       
     9 
       
    10 floats,integers and complex numbers
       
    11 
       
    12 An integer can be defined as follows :
       
    13 a=13
       
    14 
       
    15 This make a an integer variable with value 13 .
       
    16 
       
    17 You can also type 9 around 20 times 
       
    18 
       
    19 a=99999999999999999999999 . as you can see Python does not have a limit on how long an integer has to be . Isn't that great . 
       
    20 
       
    21 Now will try a float.
       
    22 
       
    23 let's type 
       
    24 p=3.141592  if you type out p now you will notice that it is not absolutely equal to p you typed in . The reason for that is how a computer saves decimal values . 
       
    25 
       
    26 Apart from integer and float, Python has an in-built support for complex numbers. Now we try to assign a complex value to a variable .
       
    27 Type:
       
    28 c = 3+4j
       
    29 As you can see ,the notation for complex numbers is similar to the one used in electric engineering. 
       
    30 We will now try some operations on complex numbers . First we will try to get the absolute value of the complex number . For this we will use the abs built in function . For this do :
       
    31 abs in parenthesis c . 
       
    32 
       
    33 Do get the imaginary part of c you can do :
       
    34 
       
    35 c.imag
       
    36 
       
    37 and similarly for real part do :
       
    38 
       
    39 c.real
       
    40 
       
    41 Python also has Boolean as a built-in type .
       
    42 
       
    43 Try it out just type ..
       
    44  t=True , note that T in true is capitalized .    
       
    45   
       
    46 You can apply different Boolean operations on t now for example :
       
    47 
       
    48 
       
    49 f=not t , this saves the value of not t that is False in f. 
       
    50 
       
    51 We can apply other operators like or and and ,
       
    52 
       
    53 f or t gives us the value True while 
       
    54 f and t gives us the value false.
       
    55 
       
    56 You can use parenthesis for precedence , 
       
    57 
       
    58 Lets write some piece of code to check this out .
       
    59 
       
    60 a=False
       
    61 b=True
       
    62 c=True
       
    63 
       
    64 To check how precedence changes with parenthesis . We will try two expressions and their evaluation.
       
    65 
       
    66 do
       
    67 (a and b) or c 
       
    68  
       
    69 This expression gives the value True
       
    70 
       
    71 where as the expression a and (b or c) gives the value False .
       
    72 
       
    73 Now we will have a look at strings 
       
    74 
       
    75 type 
       
    76 w="hello"
       
    77 
       
    78 w is now a string variable with the value "hello"
       
    79 
       
    80 printing out w[0] + w[2] + w[-1] gives hlo if you notice the expression for accessing characters of a string is similar to lists . 
       
    81 
       
    82 Also functions like len work with strings just like the way they did with lists
       
    83 
       
    84 Now lets try changing a character in the string in the same way we change lists .
       
    85 
       
    86 type :
       
    87 w[0]='H'  
       
    88 
       
    89 oops this gives us a Type Error . Why? Because string are immutable . You can change a string simply by assigning a new element to it . This and some other features specific to string processing make string a different kind of data structure than lists .  
       
    90  
       
    91 Now lets see some of the ways in which you can modify strings and other methods related to strings .
       
    92 
       
    93 Type :
       
    94 
       
    95 a = 'Hello world' 
       
    96 
       
    97 To check if a particular string starts with a particular substring you can check that with startswith method
       
    98 
       
    99 a.startswith('Hell')
       
   100 
       
   101 Depending on whether the string starts with that substring the function returns true or false
       
   102 
       
   103 same is the case a.endwith('ld')
       
   104 
       
   105 a.upper()
       
   106  returns another string that is all the letters of given string capitalized
       
   107 
       
   108 similarly a.lower returns all small letters .
       
   109 
       
   110 Earlier we showed you how to see documentations of functions . You can see the documentation of the lower function by doing a.lower?
       
   111 
       
   112 You can use a.join to joing a list of strings to one string using a given string as connector . 
       
   113 
       
   114 for example 
       
   115 
       
   116 type :
       
   117 ', '.join(['a','b','c'])
       
   118 
       
   119 In this case strings are joined over , and space
       
   120 
       
   121 Python supports formatting values into strings. Although this can include very complicated expressions, the most basic usage is to insert values into a string with the %s placeholder. %d can be used for formatting things like integers and %f for floats
       
   122 
       
   123 
       
   124 Their are many other string formatting options you can look at http://docs.python.org/library/stdtypes.html for more information on other options available for string formatting.
       
   125 
       
   126 
       
   127 Operators ---- Probably can be a different chapter .
       
   128 
       
   129 We will start the discussion on operators first with arithmetic operators .
       
   130 
       
   131 % can be used for remainder for example
       
   132 
       
   133 864675 % 10 gives remainder 5 
       
   134 
       
   135 
       
   136 you can use 2 *'s for power operation 
       
   137 
       
   138 for example 4 ** 3 gives the result 64
       
   139 
       
   140 One thing one should notice is the type of result depends on the types of input for example :
       
   141 
       
   142 17 / 2 both the values being integer gives the integer result 2
       
   143 
       
   144 however the result when one or two of the operators are float is float for example:
       
   145 
       
   146 17/2.0 
       
   147 8.5
       
   148 17.0/2.0 
       
   149 8.5