# 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

* Wikipedia: Data structure
* Python for Everyone: Tuples

## Multimedia

* YouTube: Python for Informatics - Chapter 10 - Tuples

## Examples
### Data Structures

Python data structures include lists, dictionaries, tuples, and sets.
Output:

### Data Types

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

### Functions

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

### Mutability

List, dictionary and set items are mutable. Tuple items are immutable.
Output:

### Order

Lists and tuples maintain order. Dictionaries and sets are unordered.
Output:

### Duplication

Lists and tuples allow duplication. Dictionary and set items are unique.
Output:
Sets may be used to remove duplication found in other data structures.
Output:

### Looping

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

### Index

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

### 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:

### Set Operations

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

## Activities
### Tutorials

* Complete one or more of the following tutorials:
** TutorialsPoint
*** Tuples
** Wikibooks
*** Wikibooks: A Beginner's Python Tutorial/Tuples, Lists, Dictionaries

### Practice

* 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.
* 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.
* 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.
* 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.

## Lesson Summary
### 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.

## Review Questions

* A data structure is _____. 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 _____. 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 _____; while the linked data structures are based on _____. 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 _____. Elements are accessed using _____. Arrays may be _____. 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 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 _____. The elements of records are usually called _____. 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 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 include _____. Python data structures include lists, dictionaries, tuples, and sets.
* Python lists are implemented internally as _____. Python lists are implemented internally as variable-length arrays, rather than linked lists.
* Python dictionaries are implemented internally as _____. Python dictionaries are implemented internally as resizable hash tables.
* Python tuples are _____. Python tuples are records.
* Python sets are implemented internally as _____. Python sets are implemented internally as dictionaries (hash tables) with keys and no values.
* The data types for a list, dictionary, tuple, and set, are _____. 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 _____. The list, dict, tuple, and set functions may be used to create list, dictionary, tuple, and set objects, respectively.
* List and dictionary items are _____. Tuple and set items are _____. List and dictionary items are mutable. Tuple and set items are immutable.
* Lists and tuples maintain _____. Dictionaries and sets are _____. Lists and tuples maintain order. Dictionaries and sets are unordered.
* Lists and tuples allow _____. Dictionary and set items _____. Lists and tuples allow duplication. Dictionary and set items are unique.
* Sets may be used to remove _____. Sets may be used to remove duplication found in other data structures.
* List, dictionary, tuple, and set items may be accessed using a _____ loop. List, dictionary, tuple, and set items may be accessed using a for loop.
* List and tuple items may be accessed by _____. Dictionary items are accessed by _____. Set items _____. 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 _____. 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 _____. Sets support logical set operations, including membership, subset, superset, union, intersection, and difference.

## Assessments

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

## See Also

* Python.org: Data Structures
* SoloLearn: Python
* Open Book Project: Python 3 - Tuples