Python With Spyder 14: If Statements

This is the 14th 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 the “if” statement in Python. “If” statements are, of course, extremely important in programming languages as they are the most direct way to implement conditional logic. And it is the ability for programs to make “decisions” based on such conditional logic that allows program to perform non-trivial tasks.

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 27 minutes long.

PWS14-DSSThumb

Video Index: For Loops

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

Time Topic
00:00Title slide
00:06Introduction
00:48Comments between three double quotes (“””)
00:58The syntax of the “if” statement in Python
02:00The “elif” statement
02:42The “else” statement
03:18More than one “elif” statement allowed
03:42Good coding practices focus on readable code.
03:59Comparison to the if() function in Excel
05:16The examples of “if” statements in this video build on the GetItems() function from the last video.
05:40List data for the examples: Names of the highest paid CEOs in 2014.
06:15Define the function GetItems() in the console
06:45Example of running GetItems()
07:16Reminder: Python list indices start at 0
08:09Review of how the GetItems() function works.
08:59Need to check for valid function arguments.
10:04Need to check that the first function argument is a list.
10:20Use of the “if” statement to check the argument
10:22Use of isinstance() built-in function to check an objects type.
10:47Logical “not”
10:55isinstance() syntax
11:21Use print statement to print an error message.
11:40Return and empty list.
11:52Check that second argument to GetItems() is a list.
12:36Need to check for valid indices in IndexList argument.
13:15Need to first check that index is an integer.
13:30Use “if” statement and isinstance() to check that index is an interger.
13:50Use “else” statement to print a warning.
14:04Append the value of None to the returned list.
14:37Check that the index is value for the GetItems() argument InputList.
14:46The usual range of indices for a list is 0 to n-1 (n = length of the list).
15:00Negative indices are possible.
15:18Use len() built-in function to same the length of InputList.
15:48Use “if” statement to check the index value.
16:26Use “else” statement to print a warning and return None.
17:00Change the name of the functions GetItems() to GetListItems()
17:33Spyder editor warns of a syntax error.
18:19Run code cell with cntr-Enter to define the function GetListItems() and the test case.
18:46Test list checking for the second argument by using a string.
19:22Test list checking for the first argument using a dictionary.
20:39Test checking for valid index values with an out-of-range positive index
21:21Test negative indices with a valid example.
21:41Test negative index that is out of range.
22:10Test empty list for the first argument.
23:03Test with single integer as the second argument.
24:17Modify the function to handle as single integer index value
26:06Re-run the function definition in the console
26:17Re-run the test case with a single integer index.
26:26Summary and conclusion

Leave a Comment

Time limit is exhausted. Please reload CAPTCHA.