Python Programming/Tuples and Sets

This lesson introduces Python tuples and sets by comparing them to lists and dictionaries.

Objectives and Skills
Objectives and skills for this lesson include:
 * Data Structures
 * Tuple
 * Sequences
 * Set

Readings

 * 1)  Data structure
 * 2) Python for Everyone: Tuples

Multimedia

 * 1) YouTube: Python for Informatics - Chapter 10 - Tuples

Data Structures
Python data structures include lists, dictionaries, tuples, and sets.

Output: list: [1, 2, 3] dictionary: {1: 'one', 2: 'two', 3: 'three'} tuple: (1, 2, 3) set: {1, 2, 3}

Data Types
The data types for a list, dictionary, tuple, and set, are 'list', 'dict', 'tuple', and 'set', respectively.

Output: list:  dictionary:  tuple:  set: 

Functions
The list, dict, tuple, and set functions may be used to create list, dictionary, tuple, and set objects, respectively.

Output: [1, 2, 3] {'three': 3, 'two': 2, 'one': 1} (1, 2, 3) {1, 2, 3}

Mutability
List, dictionary and set items are mutable. Tuple items are immutable.

Output: list: mutable dictionary: mutable tuple: immutable set: mutable

list: [0, 2, 3] dictionary: {1: 'zero', 2: 'two', 3: 'three'} tuple: (1, 2, 3) set: {0, 1, 2, 3}

Order
Lists and tuples maintain order. Dictionaries and sets are unordered.

Output: list: ['fish', 'dog', 'cat'] tuple: ('fish', 'dog', 'cat') dictionary: {'dog': 'Spot', 'fish': 'Bubbles', 'cat': 'Frisky'} set: {'dog', 'fish', 'cat'}

Duplication
Lists and tuples allow duplication. Dictionary and set items are unique.

Output: list: [1, 1, 1] tuple: (1, 1, 1) dictionary: {1: 'one'} set: {1}

Sets may be used to remove duplication found in other data structures.

Output: list: [1, 1, 1] set: {1} tuple: (1, 1, 1) set: {1}

Looping
List, dictionary, tuple, and set items may be accessed using a for loop.

Output: List: fish dog cat

Dictionary: cat fish dog

Tuple: fish dog cat

Set: cat fish dog

Index
List and tuple items may be accessed by index. Dictionary items are accessed by key. Set items cannot be accessed by index.

Output: list[0]: 1 list[1]: 2 list[2]: 3

dictionary[1]: one dictionary[2]: two dictionary[3]: three

tuple[0]: 1 tuple[1]: 2 tuple[2]: 3

'set' object does not support indexing

Tuple Assignment
Python supports having a tuple on the left side of an assignment statement. This allows you to assign more than one variable at a time.

Output: 1 2 2 1

Set Operations
Sets support logical set operations, including membership, subset, superset, union (|), intersection (&), and difference (-).

Output: Numbers: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} Even: {8, 10, 2, 4, 6} Odd: {9, 1, 3, 5, 7} 1 in numbers: True even.issubset(numbers): True numbers.issuperset(odd): True even | odd: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} even & odd: set numbers - even: {1, 9, 3, 5, 7}

Tutorials

 * 1) Complete one or more of the following tutorials:
 * 2) * TutorialsPoint
 * 3) ** Tuples
 * 4) * Wikibooks
 * 5) ** A Beginner's Python Tutorial/Tuples, Lists, Dictionaries

Practice

 * 1) Create a Python program that contains a dictionary of names and phone numbers. Use a tuple of separate first and last name values for the key field. Initialize the dictionary with at least three names and numbers. Ask the user to search for a phone number by entering a first and last name. Display the matching number if found, or a message if not found.
 * 2) Create a Python program that contains a dictionary of names and phone numbers. Use a tuple of separate first and last name values for the key field. Allow the user to enter names and numbers and add them to the dictionary. When the user is finished adding entries, display the names and numbers sorted in alphabetical order by last name and first name.
 * 3) Create a Python program that asks the user to enter a line of text. Use the string split method to parse the line and then display the list of words in alphabetical order with any duplicate words removed from the list. Use set to remove the duplicates.
 * 4) Create a Python program that asks the user to enter two sets of comma-separated values. Use the string split method to parse the line and then use the set function to covert the lists to sets. Demonstrate set theory for the two sets by displaying the two sets and their relationship to each other as subset, superset, union, intersection, and difference.

Data Structure Concepts

 * A data structure is a particular way of organizing data in a computer so that it can be used efficiently.
 * Data structures can be used to organize the storage and retrieval of information stored in both main memory and secondary memory.
 * The array and record data structures are based on computing the addresses of data items with arithmetic operations; while the linked data structures are based on storing addresses of data items within the structure itself.
 * An array is a number of elements in a specific order, typically all of the same type. Elements are accessed using an integer index to specify which element is required. Arrays may be fixed-length or resizable.
 * A linked list is a linear collection of data elements of any type, called nodes, where each node has itself a value, and points to the next node in the linked list.
 * A record (also called tuple or struct) is an aggregate data structure. A record is a value that contains other values, typically in fixed number and sequence and typically indexed by names. The elements of records are usually called fields or members.
 * A class is a data structure that contains data fields, like a record, as well as various methods which operate on the contents of the record.

Python Data Structures

 * Python data structures include lists, dictionaries, tuples, and sets.
 * Python lists are implemented internally as variable-length arrays, rather than linked lists.
 * Python dictionaries are implemented internally as resizable hash tables.
 * Python tuples are records.
 * Python sets are implemented internally as hash tables (like dictionaries) with optimizations that take advantage of the fact that the values are always None.
 * The data types for a list, dictionary, tuple, and set, are 'list', 'dict', 'tuple', and 'set', respectively.
 * The list, dict, tuple, and set functions may be used to create list, dictionary, tuple, and set objects, respectively.
 * List and dictionary items are mutable. Tuple and set items are immutable.
 * Lists and tuples maintain order. Dictionaries and sets are unordered.
 * Lists and tuples allow duplication. Dictionary and set items are unique.
 * Sets may be used to remove duplication found in other data structures.
 * List, dictionary, tuple, and set items may be accessed using a for loop.
 * List and tuple items may be accessed by index. Dictionary items are accessed by key. Set items cannot be accessed by index.
 * Python supports having a tuple on the left side of an assignment statement. This allows you to assign more than one variable at a time.
 * Sets support logical set operations, including membership, subset, superset, union, intersection, and difference.

Key Terms

 * comparable
 * A type where one value can be checked to see if it is greater than, less than, or equal to another value of the same type. Types which are comparable can be put in a list and sorted.


 * data structure
 * A collection of related values, often organized in lists, dictionaries, tuples, etc.


 * DSU
 * Abbreviation of “decorate-sort-undecorate”, a pattern that involves building a list of tuples, sorting, and extracting part of the result.


 * gather
 * The operation of assembling a variable-length argument tuple.


 * hashable
 * A type that has a hash function. Immutable types like integers, floats, and strings are hashable; mutable types like lists and dictionaries are not.


 * scatter
 * The operation of treating a sequence as a list of arguments.


 * shape (of a data structure)
 * A summary of the type, size, and composition of a data structure.


 * singleton
 * A list (or other sequence) with a single element.


 * tuple
 * An immutable sequence of elements.


 * tuple assignment
 * An assignment with a sequence on the right side and a tuple of variables on the left. The right side is evaluated and then its elements are assigned to the variables on the left.

Assessments

 * Flashcards: Quizlet: Python Lists and Tuples
 * Quiz: Quizlet: Python Lists and Tuples