statistics.txt
changeset 53 3d2c2c0bc3e2
parent 52 53700ad0e71e
child 58 2c4e318741cf
equal deleted inserted replaced
52:53700ad0e71e 53:3d2c2c0bc3e2
   117 Let's now start off with the code
   117 Let's now start off with the code
   118 
   118 
   119 We first create an empty dictionary
   119 We first create an empty dictionary
   120 
   120 
   121 science = {}
   121 science = {}
   122 now we read the record data one by one from the file sslc.txt
   122 now we read the records, one by one from the file sslc.txt
   123 
   123 
   124 for record in open('sslc.txt'):
   124 for record in open('sslc.txt'):
   125 
   125 
   126     we split the record on ';' and store them in a list by: fields equals record.split(';')
   126     we split each record on ';' and store it in a list by: fields equals record.split(';')
   127 
   127 
   128     now we get the region code of a particular entry by region_code equal to fields[0].strip.
   128     now we get the region code of a particular entry by region_code equal to fields[0].strip.
   129 The strip() is used to remove all leading and trailing white spaces from a given string
   129 The strip() is used to remove all leading and trailing white spaces from a given string
   130 
   130 
   131     now we check if the region code is already there in dictionary by typing
   131     now we check if the region code is already there in dictionary by typing
   133        when this statement is true, we add new entry to dictionary with initial value 0 and key being the region code.
   133        when this statement is true, we add new entry to dictionary with initial value 0 and key being the region code.
   134        science[region_code] = 0
   134        science[region_code] = 0
   135        
   135        
   136     Note that this if statement is inside the for loop so for the if block we will have to give additional indentation.
   136     Note that this if statement is inside the for loop so for the if block we will have to give additional indentation.
   137 
   137 
   138     we again come back to the older 'for' loop indentation and we again strip the string and to get the science marks by
   138     we again come back to the older, 'for' loop's, indentation and get the science marks by
   139     score_str = fields[6].strip()
   139     score_str = fields[6].strip()
   140 
   140 
   141     we check if student was not absent
   141     we check if student was not absent
   142     if score_str != 'AA':
   142     if score_str != 'AA':
   143        then we check if his marks are above 90 or not
   143        then we check if his marks are above 90 or not
   159 
   159 
   160 title('Students scoring 90% and above in science by region')
   160 title('Students scoring 90% and above in science by region')
   161 savefig('science.png')
   161 savefig('science.png')
   162 
   162 
   163 That brings us to the end of this tutorial. We have learnt about dictionaries, some basic string parsing and plotting pie chart in this tutorial. Hope you have enjoyed it. Thank you.
   163 That brings us to the end of this tutorial. We have learnt about dictionaries, some basic string parsing and plotting pie chart in this tutorial. Hope you have enjoyed it. Thank you.
       
   164 #slide of summary.