Editing Community Data Science Course (Spring 2015)/Day 2 Followup
From CommunityData
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
Line 17: | Line 17: | ||
Sure enough, that code will print "4" which is the number of things in the list. Β | Sure enough, that code will print "4" which is the number of things in the list. Β | ||
The only thing | The only thing that's possibly new here is the line with <code>counter</code> in it twice. All we're doing in that line is just adding 1 to the variable <code>counter</code> and then replacing the old value with the new, slightly bigger, version. | ||
Of course, instead of that counter and <code>for</code> loop, we could have just used <code>len(names)</code> and it would have done the same thing! This is powerful because loops are much more flexible and combined with other things we've learned. For example, lets say I wanted to count the number of names that start with the letter "m". I could combine that counter above with an <code>if</code> statement that checks if the "0th" item in each name (i.e., the first letter) to see if it's "m" like this: | Of course, instead of that counter and <code>for</code> loop, we could have just used <code>len(names)</code> and it would have done the same thing! This is powerful because loops are much more flexible and combined with other things we've learned. For example, lets say I wanted to count the number of names that start with the letter "m". I could combine that counter above with an <code>if</code> statement that checks if the "0th" item in each name (i.e., the first letter) to see if it's "m" like this: |