Python With Spyder 13: For Loops

This is the 13th in a series of videos providing a tutorial on Python 2.7 using Anaconda Python and the Spyder IDE. Click here to go to a “home page” for the video series.

This video introduces looping or iteration in Python using the “for” statement. Looping, of course, is one of the most important things in programming languages as many problems require iteration over an index or a set.

The source code used in this video can be found here. You can right click on the link and use “Save As” to save the file.

Note: The source code files are plain text files with a “.txt” extention. You will probably want to change the extensions to “.py” after you download them. If you do so, please be aware that if you have Python installed, the file will become executable, so that it will run if you click on it (accidentally or otherwise).

The video is about 34 minutes long.

PWS13-DSSThumb

Video Index: For Loops

Click on the topics below to jump to that location in the video.

Time Topic
00:00Title Slide
00:06Inrtoduction
00:23The idea of program flow control
01:12The syntax of the Python “for” statement
01:33Iterables
01:58The colon (“:”) in the for statement signals a following block of code
02:11The end of the block of code
02:21What the “for” statement does.
02:54Comparison of the Python “for” to other computer languages
04:18Data example: The names of the 200 highest paid CEOs in 2014
04:35CEO name list defined in a code cell
04:44Execution of the code in a cell
05:02Length of a list using built-in function len()
05:18List of CEO first names defined
05:45List of CEO last names defined
06:32First example: get an arbitrary set of list items — the GetItems() function
07:10How the GetItems() example function works.
08:29Create the GetItems() function with “def”
09:11Define and empty list for output
09:25The “for” statement in GetItems()
09:47The “for” statement ends with a colon (“:”)
10:15Summary of the GetItems() code.
10:43Return the output from GetItems()
11:00No error checking done! A bad practice!
11:42Run the code cell with cntrl-enter to define the GetItems() function
11:58Test the GetItems() function.
12:34Summary and review of the GetItems() function.
13:10Final example in this video will iterate over an index.
13:23Iterating over an index is consistent with earlier computer languages.
13:53Final example will compute a correlation.
14:03Example will correlation the length of the CEO first and last names.
14:22Review formulas for the sample correlation.
14:45The formula for the sample mean.
14:54The summation notation from mathematics.
15:25The formula for the sample standard deviation.
16:21The formula for the sample covariance.
17:18The formula for the sample correlation.
18:19Define the function mean() to compute the sample mean.
18:52The “for” loop in the mean() function.
19:18The “+=” notation.
20:12The “/=” notation.
20:53Define the function sd() to compute the sample standard deviation.
21:09Call the mean() function from the sd() function.
21:23The “for” loop in the sd() function.
22:39An index was not used in the mean() or sd() functions.
22:57Stepping through X and Y at the same time using an index.
23:33The range() function
25:30Defining the cor() function to compute the sample correlation.
26:53The “for” loop in the cor() function using the range() function and an index.
27:45Using the index to retrieve values from X and Y (subscripts for X and Y).
28:33Summary: Why the index approach is natural for two lists X and Y.
31:05Compute the length of a string with the len() function.
31:20Computing the length of the first names and last names using the map() function.
32:30Computing the correlation between the first name length and the last name length using cor(), map(), and len().
33:28Conclusion

2 thoughts on “Python With Spyder 13: For Loops

  1. Thanks Prof for your wonderful videos. I couldnt run the code for this exercise. It came back with the following error.

    “TypeError: object of type ‘map’ has no len()”

    How do i solve this. I’m using python 3.

    Kind Regards,

Leave a Comment

Time limit is exhausted. Please reload CAPTCHA.