Jump to content

Minor comment on the last episode: you 've touched on lists a couple of times, and on loops, but...


G+_Lee Crocker
 Share

Recommended Posts

Minor comment on the last episode: you've touched on lists a couple of times, and on loops, but failed to mention the obvious relationship between the two. Lists (and other types of collections that your language may have--Python also has dictionaries and sets, for example) are a natural way to hold a number of related objects, and a very natural thing to do in programming is to loop over a collection--that is, to do something for each member. Many languages have special features for doing that, so you don't need to manually create a counter and update it in a while loop. In Python, you just:

 

for item in myList:

print item

 

etc. You can still count a loop by number if you want to:

 

for num in range(6):

print num

 

will print 0...5, for example (note that it is not necessary to include the 0 start; that's the default, and leaving it out makes it clearer that the loop will run 6 times). But that shouldn't be your first choice for stepping over a collection.

Link to comment
Share on other sites

I was also a little uncomfortable about how the for loop was described.  He could have simply said that unlike other languages, the for loop in python loops over each item in just about any sequence including lists and strings.  This is more like how the foreach statement works in other languages.  All the range() function does is return a list of numbers to loop over. 

 

>>> range(5)

[0, 1, 2, 3, 4]

>>> range(1,5)

[1, 2, 3, 4]

>>> range(1,10,2)

[1, 3, 5, 7, 9]

>>> for letter in "Test": print letter

...

T

e

s

t

Link to comment
Share on other sites

Since Shannon's a beginner her code should be reviewed by an expert before it's presented so they don't commit the cardinal sin (that should get Padre's attention) of teaching incorrect coding. Other than that first program, I thought the show was much improved. The Hosts and the Expert seemed to finally be on the same page.

Link to comment
Share on other sites

The book "The Elements of Programming Style", by Brian W. Kernighan and P. J. Plauger uses that technique.   From: https://en.wikipedia.org/wiki/The_Elements_of_Programming_Style  "The book is built on short examples from actual, published programs in programming textbooks. This results in a practical treatment rather than an abstract or academic discussion. The style is diplomatic and generally sympathetic in its criticism, and unabashedly honest as well" Unfortunately, the actual computer languages in use in 1974 are no longer popular, so that limits the readability of that book today.  That Wikipedia article summarizes the points made in the book.

Link to comment
Share on other sites

 Share

×
×
  • Create New...