Not logged in
Talk
Contributions
Create account
Log in
Navigation
Main page
About
People
Publications
Teaching
Resources
Research Blog
Wiki Functions
Recent changes
Help
Licensing
Page
Discussion
Edit
View history
Editing
DS4UX (Spring 2016)/Working within loops
From CommunityData
Jump to:
navigation
,
search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
So far in this course, we've covered assigning to variables, addition, conditionals, and the special <code>.append()</code> function associated with lists. Solving the [[DS4UX_(Spring_2016)/Day_2_coding_challenge|Day 2 Coding Challenges]] with the [[Wordplay]] 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: <syntaxhighlight lang="python"> names = ["graham", "eric", "terry g", "terry j", "john", "michael"] counter = 0 for name in names: counter = counter + 1 print(counter) </syntaxhighlight> Sure enough, that code will print "6" which is the number of things in the list. 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. 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 "t". 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 "t" like this: <syntaxhighlight lang="python"> names = ["graham", "eric", "terry g", "terry j", "john", "michael"] counter = 0 for name in names: if name[0] == "t": counter = counter + 1 print(counter) </syntaxhighlight> Now, this program will print out "2" (the number of names that start with "t") 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 <code>t_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"> names = ["graham", "eric", "terry g", "terry j", "john", "michael"] t_names = [] for name in names: if name[0] == "t": t_names.append(name) print(t_names) </syntaxhighlight> Run these little programs in Python (or save them to files and run them in your terminal) and try to use the concepts to make some progress on the [[DS4UX_(Spring_2016)/Day_2_coding_challenge|Day 2 Coding Challenges]]. [[Category:DS4UX (Spring 2016)]]
Summary:
Please note that all contributions to CommunityData are considered to be released under the Attribution-Share Alike 3.0 Unported (see
CommunityData:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
To protect the wiki against automated edit spam, we kindly ask you to solve the following CAPTCHA:
Cancel
Editing help
(opens in new window)
Tools
What links here
Related changes
Special pages
Page information