Python With Spyder 12: Dictionaries

This is the 12th 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 dictionaries in Python. Dictionaries are Python’s implementation of the idea of key-value pairs.

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

PWS12-DSSThumb

Next Video: For Loops

Video Index: Dictionaries

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

Time Topic
00:00Title slide
00:06Introduction
00:44Creating a dictionary using curly brackets {}.
01:01Key-value pairs separated by the colon (:).
02:06Keys and values do not have to be strings.
02:20Keys and values can be any Python object.
02:27Keys must be unique.
02:40Using a duplicate key overwrites earlier value.
03:05Run the Python code to create the dictionary.
02:12Cells of code in the editor (review)
03:38Run the code in a cell using cntrl-Enter.
03:47Make the Python console larger.
04:06Print out the dictionary in the Python console.
04:37Reference the value corresponding to a key.
05:04Execute a line of code using F9.
05:22Assign a new value corresponding to a key in the dictionary.
06:36Assign a value to a new key not already in the dictionary created a new key-value pair.
07:31Dictionaries in Python do not have an order.
07:51Cannot reference dictionary items using an index.
08:31Dictionaries cannot be sorted.
09:04Second way to create a dictionary starting with an empty dictionary and adding key-value pairs.
09:40Create an empty dictionary using curly brackets {}.
09:53Load key-value pairs into the dictionary by assigning values to new keys.
10:30Virtually everything in Python is an object.
10:43Dictionaries are objects.
10:52The class constuctor for dictionaries in Python is dict()
10:57Create an empty dictionary using dict().
11:17As objects, dictionaries have methods.
11:31Two important dictionary methods are keys() and values()
11:47Example using the keys() method.
12:15The type returned by the keys() method is a list.
12:24Example using the values() method.
12:44The keys and values returned by the key() and values() methods are in the same order.
13:36The pop() method.
13:47pop() method example.
14:38The popitem() method.
14:59popitem() method example.
16:06The type of the key-value pair returned by the popitem() method is a tuple.
16:33Test for a key in the dictionary using the “in” keyword.
16:39Clear the console window using cntrl-L.
16:57Example of using “in” for a key that is in the dictionary.
17:37Example of using “in” for a key that is not in the dictionary.
18:16Using the dir() builtin function to list out all the attributes and methods of a dictionary.
18:35Attributes and methods beginning with a double underscore (__) are intended to be private.
19:05Getting information about dictionary methods using internet search.
19:24Python.org is the “official” web site for Python.
19:38Finding dictionary (mapping types) documentation in the “Builtin Types” documentation.
20:09Cannot create copies of dictionaries by assignming the dictionary name.
20:17Dictionary name assignment example.
21:24Example of creating a copy of a dictionary using dict().
22:39Use caution when using assignment with dictionary names.
23:22More complicated dictionary example using objects as values.
24:11Example builds on code from the last video: Python With Spyder 11: Inheritence.
24:16Review objects defined in PythonWithSpyder11.py
24:56Execute object definition code from PythonWithSpyder11.py
26:00Create a dictionary with objects as values.
27:49Print the dictionary (from the example with objects as values).
28:28Accessing an attribute of an object in the dictionary.
29:00“AttributeError when accessing an attribute not defined for an object in the dictionary.
29:26Second example of accessing attributes of objects in the dictionary.
30:04Summary of the dictionary example using objects.
30:44Conclusion

Next Video: For Loops

Spyder Broken in Anaconda Python (August 2015)

I regularly update my installation of Anaconda Python by running the Anaconda Python console and typing:

conda update anaconda

at the C:\Anaconda> prompt.

When I tried to run the Spyder IDE today, it died with the message:

…./anaconda/lib/python2.7/site-packages/IPython/qt.py:13: ShimWarning: The `IPython.qt` package has been deprecated. You should import from qtconsole instead.
“You should import from qtconsole instead.”, ShimWarning)

Apparently, according to the discussion here, the problem is a compatibility issue the new version 4.0 of iPython. The solution is to downgrade the iPython console to version 3.2.1:

Solution

Here is how you “downgrade” the iPython console to the needed version 3.2.1. At the Anaconda command prompt, enter

conda install ipython=3.2.1

Thus, with the install command of conda, you can control the version by using the “=” suffix as above.

This solved the problem for me! (Wasted quite a lot of my time, however.)

I imagine that this problem will be corrected fairly quickly. By the way, I am using Python 2.7. I don’t know if the problem also occurs in Python 3.

Python With Spyder 11: Objects Part 4 — Inheritance

This is the 11th in a series of videos providing a tutorial on Python 2.7 using Anaconda Python and the Spyder IDE.

This video is the fourth video introducing objects in Python. This video discusses the idea of inheritance.

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

PWS11-DSSThumb

Next Video: Dictionaries

Video Index: Objects Part 4 — Inheritance

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

Time Topic
00:00Title Slide
00:26Review example: supermarket products
03:12Define class for milk products
03:23A class with inheritance
03:56Non-dynamic attributes for every instance of a class
04:16Dynamically creating attributes using __init__()
04:41Arguments of __init__() begin with self and include arguments of parent class
05:10Spyder “trick:” split the editor window
06:03Default values for arguments and the value None
08:07Initialization derived from the parent class
09:01Initialization unique to the child class
10:05Using parent class methods
10:59How inheritance works and how names are resolved.
12:08Definition of a second child class inheriting from the Product class
14:48Summary of inheritance so far.
15:18Executing the class (object) definitions
15:34Spyder “trick:” removing the editor window split
15:53Cells in the Spyder editor: The code for each class is in a cell
16:03Execute the code in a cell using cntrl-enter
17:10Create two instances (milk and laudry detergent)
19:58Skipping arguments and using defaults: subsequent variables names must be specified
21:31Spyder: Move console to make it big
21:40Reference values in object attributes
22:25Reference object methods
22:50Error referencing a method
23:10Using dir() to see an object’s attributes and methods
25:20Summary of inheritance in Python

Next Video: Dictionaries

Python With Spyder 10: Objects Part 3 – Private Data and Encapsulation

This is the 10th 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 is the third of four videos introducing objects in Python. This video discusses private data and encapsulation.

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

PWS10-DSSThumb

Next Video: Inheritance

Video Index: Private Data and Encapsulation

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

Time Topic
00:05Introduction
00:41Review of the example
01:22Phone number example of private data
02:10Basic idea of encapsulation
02:42Begin phone number method example
02:58Phone number formats
03:42Private attribute __PhoneNumber
04:53SetPhone() and GetPhone() methods
05:42All methods get self as first argument
07:14“if” statement to test if the phone number is a character string
08:18Use of the “or” keyword in the example
09:37Use of the “in” keyword in the example
10:16Use of the “and” keyword in the example
11:05The line continuation character (“\”)
11:23Use of the “not” keyword in the example
11:45The return statement in the SetPhone() method
11:59Set or create the private attribute __PhoneNumber
13:46Using a “cell” in the editor and cntrl-enter to execute a block of code
14:33Create two instances
14:57Private data cannot be directly accessed
15:15Use the SetPhone() method to set the phone number
16:03Private attribute __PhoneNumber still cannot be directly accessed
16:17Read the phone number with the GetPhone() method
16:45Test the SetPhone() methods with incorrectly formatted phone numbers
18:04Summary of private data in Pythin objects
18:53Summary of Encapsulation
19:12Private methods (functions)

Next Video: Inheritance

Python With Spyder 9: Objects Part 2 – Attributes and Methods and a Few Spyder “Tricks”

This is the 9th 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 is the second of four videos introducing objects in Python. This video discusses attributes and methods and also demonstrates a few features of the Spyder IDE.

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

PWS9-DSSThumb

Next Video: Private Data and Encapsulation

Video Index: Attributes and Methods

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

Time Topic
00:00Title Slide
00:03Location of source code
00:06Review of supermarket product example
00:56Spyder “trick:” Connecting a console to the editor
02:20Spyder “trick:” Code cells in the editor (“#%%”)
03:20Spyder “trick:” Executing a code cell with the toolbar button
04:20Spyder “trick:” Executing a code cell with cntrl-Enter
04:42Spyder “trick:” Executing a code cell with shift-Enter
05:16Most object attributes created dynamically using __init__()
05:37Object attributes do not have to be created dynamically and can be defined for every instance
07:01Terminology: Attribute
07:41Review of __init__()
07:54__init__() is a private method
08:10“Public” methods
09:32Spyder “trick:” Comment and uncomment blocks of code (cntrl-1)
10:20Function definition and indentation for methods
10:41Method example: a Print() function
11:02The first argument of a method is self
11:23Special formatting symbols in strings: “\n” for newline, “\n” for tab
11:49String concatenation (joining) with “+”
12:52Python line continuation with “\” at the end of the line
13:27Convert a number to a striing with the built-in function str()
14:12Run the class definition code for the class Product
14:28Run the code to create two instances of Product
14:38Use the Print() method for the two instances
15:30The built-in function dir()
16:14Summary

Next Video: Private Data and Encapsulation

Python With Spyder 8: Python Objects Part 1

This is the 8th 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 is the first of four videos on introducing objects in Python. This video covers the basic idea of using the class statement to create objects in Python and then goes on to show how to create objects using the __init__( ) function to dynamically create object attributes.

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

PWS8-DSSThumb

Next Video: Attributes and Methods

Video Index: Python Objects Part 1

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

Time Topic
00:00Introduction and Continuation of the Example of Products in a Grcery Store
00:14The data for every product
01:21Create Object for the product Skim Milk
01:39The Python Class statement
01:51The Class Statement Ends in a Colon
02:12Specifying Attributes
04:32The Error Triangle in the Spyder Editor
05:31Executing the Class Statement
06:00Referenceing Object Attributes
07:17The Problem with the Manual Specification of Objects
07:52The Need for an Object Template Approach
08:31Discussion of the Need for Object Constructors
11:05Class Statement for an Object Constructor
11:17The __init__() function to initialize objects
11:47The Reference to Self In the __init__() function
12:41The colon (“:”) at the end of the __init__() function definition
12:58Indentation for the __init()__ function\
13:09Creating Variables using the –init__() function and self
15:07Creating an instance (using the __init__() function and self)
16:18Summary of Object Creation So Far
16:34Executing the Class Statement
16:52Creating an instance (using the __init__() function and self)
17:22Reference Object Attributes
18:08Write the code to creating a second instance
19:34Create the second instance
19:47Reference the Attribues of the Second Instance
20:15Summary of Object Creation So Far
21:37Self is Omitted from the Call to the Object Creator
22:13Summary

Next Video: Attributes and Methods

Python With Spyder 7: The Idea of Objects

This video is the 7th 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 idea of objects. No actual Python code is discussed in this video. The objective is only to frame the idea of an object as background for the subsequent videos which present the basics of objects in Python.

The video is about 16 minutes long.

PWS7-DSSThumb

Next Video: Python Objects Part 1

Video Index: The Idea of Objects

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

Time Topic
00:00Introduction
00:14Example: Products in a Grocery Store
00:24Common Characteristics of All Products
01:56Columns: One View of the Data
03:00Rows as Objects: A Second View of the Data
04:08The Variables for Different Items are Different
06:04Using a Huge Table
07:15Summary of the Most Basic Idea of Objects
07:34More Characteristics of the Concept of Objects: Inheritance, Methods, and Encapsulation
08:02Inheritance
09:47Attributes
11:01Methods
13:14Encapsulation
14:34Summary of Key Object Ideas
15:40Conclusion

Next Video: Python Objects Part 1

Python With Spyder 6: Lists Part 2

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

The video is about 19 minutes long.

PWS6-DSSThumb

Next Video: The Idea of Objects

Video Index: Lists Part 2

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

Time Topic
00:00Introduction
00:16Python lists can contain any valid object
00:30Define an example function
01:19Create complicated list as an example
02:38Extend list definition across multiple lines
03:31Editor syntax checking – trianlge icon next to code line
03:38Run code block
04:11Print out list
04:47Reference list items
05:27Reference letter in a list item that is a string using two indices
06:07Reference a list item that is a function and execute it
06:55Reference a list item that is a list, its elements and slices
08:07Summary of complicated lists and their indexing
08:55Functions associated with lists (list methods)
09:11String method “upper”
09:38String method “lower”
09:46Get a list of the available string methods using code completion
10:11String method “capitalize”
10:25Functions associated with lists (list methods)
10:35Create an example list
11:10Continue list definition across multiple lines in iPython console
12:16Find the functions associated with lists (list methods) using code completion
12:51List method “reverse”
13:29List method “sort” (using code completion)
15:01Sort list in reverse order
15:48Summary of the section of the video on list methods
16:33Code completion in the editor window in the Spyder IDE

Next Video: The Idea of Objects

Python With Spyder 5: Lists Part 1

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

The video is about 15 minutes long.

PWS5-DSSThumb

Next Video: Lists Part 2

Video Index: Lists Part 1

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

Time Topic
00:00Introduction
00:26Definition of a vector
01:35Definition of a matrix
02:42Definition of a list
03:13Create a list in Python
04:18Assign a list to a variable name
04:45Index a list
05:11Negative index numbers for a list
05:41Slicing lists
06:26Summary of the basics
06:49Assigning list to variables – unexpected behavior
08:04Variable names are pointers to the list
09:13Create and assign a copy of a list
10:36Lists are indexed with square brackets not round brackets
10:50“object is not callable” error
11:25“index out of range” error
11:51Assign list item values with square brackets
12:23Lists are mutable
12:48Strings are immutable
13:47Convert string to a list
14:43Conclusion

Next Video: Lists Part 2

Python With Spyder 4: Strings, Indexing, and Slicing

This is the 4th 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.

The topics covered in this video are strings, indexing, and slicing.

The video is about 13 minutes long.

PWS4-DSSThumb

Next Video: Lists Part 1

Video Index: Strings, Indexing, and Slicing

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

Time Topic
00:00 Introduction
00:27 Create a string using double quotes
01:06 String property length and built-in function len()
01:31 Create a string using single quotes
01:47 Concatenate strings with “+”
02:08 String indexing
02:29 String indexing begins at 0
02:41 Indexing is done with square brackets (“[]”)
03:14 Index of the last letter is the length minus 1
03:31 Error when index is equal to the length
03:59 Negative indexing
04:43 Summary of string indexing
05:11 Slicing – ranges of letters in strings
06:30 One-sided slicing – leaving out the left or right index of the slice
07:30 Mutable and immutable data
08:03 Strings are immutable
08:09 Cannot assign to string index
08:43 String methods (functions)
08:55 upper() method
09:13 split() method
09:52 Convert number to string with str()
10:46 Convert a string that is a real number to type float with float()
11:38 Convert a string that is an integer to type integer with int()
11:55 Conclusion

Next Video: Lists Part 1