Community Data Science Course (Spring 2015)/Day 2 Followup: Difference between revisions

From CommunityData
(Created page with "In the lecture we covered assigning to variables, addition, conditionals, and the special <code>.append()</code> function associated with lists. Sovling the Community Data S...")
 
No edit summary
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
In the lecture we covered assigning to variables, addition, conditionals, and the special <code>.append()</code> function associated with lists. Sovling the [[Community Data Science Course (Spring 2015)/Day 2 Coding Challenges|Day 2 Coding Challenges]] with the [[Baby names]] project is going to require you use not only use these things but put them together.
In the lecture on Monday, we covered assigning to variables, addition, conditionals, and the special <code>.append()</code> function associated with lists. Sovling the [[Community Data Science Course (Spring 2015)/Day 2 Coding Challenges|Day 2 Coding Challenges]] with the [[Baby names]] project is going to require you use not only use these things but put them together.


I wanted to show you some concrete examples of this. These aren't answers but hopefully you'll see how they might be applied to solve some of the problems.
I wanted to show you some concrete examples of this. These aren't answers but hopefully you'll see how they might be applied to make progress on some of the coding challenges.


Here's an example of a counter variable I can use to count the number of names in a list:
Here's an example of a counter variable I can use to count the number of names in a list that should be familiar from lecture:


<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
Line 15: Line 15:
</syntaxhighlight>
</syntaxhighlight>


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.
Sure enough, that code will print "4" which is the number of things in the list.  


Of course, instead of that counter and for loop, we could have just used <code>len(names)</code> and it would have done the same! The way this gets powerful is that we begin to combine this.
The only thing might seem 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.


For example, lets say I wanted to just count the number of names that start with the letter "m". I could combine that counter above with an if statement that checks if the "0th" item in each name (i.e., item in the list) is "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:


<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
Line 34: Line 34:
Now, this program will print out "2" (the number of names that start with "m") instead of "4".
Now, this program will print out "2" (the number of names that start with "m") instead of "4".


I can also use append to create a new list from within a for loop. So lets say I wanted to create a new list that contains only those "m" names. In this case, the code I'll write is very similar except I will replace the counter with an empty list (<code>[]</code>) and I'll append things to it every time there's a match. Here's an example:
Python can do anything from within a loop! For example, lets say I wanted to create a new list (I'll call the variable <code>m_names</code>) that contains only those names that start with "m". In this case, the code I'll write is going to similar to what we've already seen except I will replace the counter with an empty list (<code>[]</code>) and I'll append things to that list every time there's a match from within a loop. Here's an example:


<syntaxhighlight lang="python">
<syntaxhighlight lang="python">

Latest revision as of 04:19, 8 April 2015

In the lecture on Monday, we covered assigning to variables, addition, conditionals, and the special .append() function associated with lists. Sovling the Day 2 Coding Challenges with the Baby names project is going to require you use not only use these things but put them together.

I wanted to show you some concrete examples of this. These aren't answers but hopefully you'll see how they might be applied to make progress on some of the coding challenges.

Here's an example of a counter variable I can use to count the number of names in a list that should be familiar from lecture:

 names = ["mako", "mika", "william", "bettamax"]
 
 counter = 0
 for name in names:
     counter = counter + 1

 print(counter)

Sure enough, that code will print "4" which is the number of things in the list.

The only thing might seem new here is the line with counter in it twice. All we're doing in that line is just adding 1 to the variable counter and then replacing the old value with the new, slightly bigger, version.

Of course, instead of that counter and for loop, we could have just used len(names) 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 if statement that checks if the "0th" item in each name (i.e., the first letter) to see if it's "m" like this:

 names = ["mako", "mika", "william", "bettamax"]
 
 counter = 0
 for name in names:
     if name[0] == "m":
         counter = counter + 1

 print(counter)

Now, this program will print out "2" (the number of names that start with "m") instead of "4".

Python can do anything from within a loop! For example, lets say I wanted to create a new list (I'll call the variable m_names) that contains only those names that start with "m". In this case, the code I'll write is going to similar to what we've already seen except I will replace the counter with an empty list ([]) and I'll append things to that list every time there's a match from within a loop. Here's an example:

 names = ["mako", "mika", "william", "bettamax"]
 
 m_names = []
 for name in names:
     if name[0] == "m":
         m_names.append(name)

 print(m_names)

Run these little programs in ipython (or save them to files and run them in your terminal) and try to use the concepts to make some progress on the Day 2 Coding Challenges.