Tutorial #4 : Explain All Data Types With Example


  • Numbers
         Number data types store numeric values. Number objects are created     
         When you assign a value to them.        
         For example −         Var1 =23         Var2 =10        
        You can also delete the number object by using the del statement.         
         For example −
         del Var 
         del Var1 , Var2


  • Strings
         Python allows for either pairs of single or double quotes. Subsets of strings 
       Can be taken using the slice operator ([ ] and [:] ) with indexes starting at 
       0 in the beginning of the string and working their way from -1 at the end.
       The plus (+) sign is the string concatenation operator and the asterisk (*) 
       Is the repetition operator. 

       For example −

       str = 'Hello World!'
      
       print str             #print complete string

       print str[0]        #print first position of string
       print str[2:5]     #print 3rd to 5th position of string
       print str[2:]      #print string from 3rd position  
       print str * 2
       print str + "TEST"

  • Lists
       A list contains items separated by commas and enclosed within square      
       Brackets ([]). The values stored in a list can be accessed using the slice 
       Operator ([ ] and [:]) with indexes starting at 0 in the beginning of the list 
       And working their way to end -1. The plus (+) sign is the list      
       Concatenation operator, and the asterisk (*) is the repetition operator. 

        For example −
        list = [ 'abcd', 786 , 2.23, 'john', 70.2 ]
        list2 = [ 'xyz', 810 ] 
        print list[0]        
        print list[2:5]     
        print list[2:]        
        print list2 * 2
        print str + list2

  • Tuples 
          A tuple consists of a number of values separated by commas. Lists are 
        Enclosed in brackets ( [ ] ) and their elements and size can be changed,           While tuples are enclosed in parentheses ( ( ) ) and cannot be updated.             Tuples can be thought of as read-only lists. 

        For example −
        Tuple = ( 'abcd', 786 , 2.23, 'john', 70.2 )

        Tuple2 = ( 'xyz', 810 )
        print Tuple[0]        
        print Tuple[2:5]     
        print Tuple[2:]        
        print Tuple2 * 2
        print str + Tuple2

  • Dictionary
        Dictionaries are enclosed by curly braces ({ }) and values can be assigned 
        And accessed using square braces ([]). 
  
        For example −
        dict = {}

        dict['one'] = "This is one"
        dict[2]     = "This is two"

        tinydict = {'name': 'john','code':6734, 'dept': 'sales'}


        print dict['one']       # Prints value for 'one' key
        print dict[2]           # Prints value for 2 key
        print tinydict          # Prints complete dictionary
        print tinydict.keys()   # Prints all the keys
        print tinydict.values() # Prints all the values



Comments