basic-python.txt
changeset 78 099a2cc6c7d2
parent 73 9dffd11728d2
child 93 bdee3ead116d
equal deleted inserted replaced
77:6716a496dc05 78:099a2cc6c7d2
    40 
    40 
    41 Python also has Boolean as a built-in type .
    41 Python also has Boolean as a built-in type .
    42 
    42 
    43 Try it out just type ..
    43 Try it out just type ..
    44  t=True , note that T in true is capitalized .    
    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 
    45  
    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]='Capital 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