<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Chapter 12
Step1:  If you want to create a tuple with a single value, add a comma (,) after the value, but don’t add parenthesis
You can also use the built in function tuple
Step2:  Most list operators (e.g., bracket and slice) work on tuples
If you try to modify one of the elements, you get an error
Step3:  Tuple assignment
Often, you may want to swap the values of two variables
This is done conventionally using a temporary variable for storage
Step4:  Python allows you to do it using a tuple assignment
Step5:  On the left is a tuple of varaibles and on the right is a tuple of expressions
Note that the number on each side of the equality sign must be the same
The right side can be any kind of sequence (e.g., a string or list)
Step6:  Tuples as return values
A function can only return one value
However, if we make that value a tuple, we can effectively return multiple values
For example, the divmod function takes two (2) arguments and retunrs a tuple of two (2) values, the quotient and remainder
Step7:  Note the use of a tuple assignment
Here is how to build a function that returns a tuple
Step8:  Note that min and max are built-in functions
Variable-length argument tuples
All the functions we have built and used required a specific number of arguments
You can use tuples to build functions that accept a variable number of arguments
Prepend the argument’s variable name with an * to do this
It is referred to as the gather operator
Step9:  The complement is the scatter operator
It allows you to pass a sequence of values as individual arguments to the function
Step10:  Lists and tuples
The zip function takes two or more sequences and "zips" them into a list of tuples
Step11:  Essentially, it returns an iterator over a list of tuples
If the sequences aren't the same length, the result has the length of the shorter one
Step12:  You can also use tuple assignment to get the individual values
Step13:  You can combine zip, for and tuple assignment to traverse two (or more) sequences at the same time
Step14:  If you need the indices, use the enumerate function
Step15:  Dictionaries and tuples
The method items returns a tuple of key-value pairs from a dictionary
Step16:  Remember that there is no particular ordering in a dictionary
You can also add a list of tuples to a dictionary using the update method
Using items, tuple assignment, and a for loop is an easy way to traverse the keys and values of a dictionary
Step17:  Since tuples are immutable, you can even use them as keys in a dictionary
<ASSISTANT_TASK:>
Python Code:
a_tuple = ( 'a', 'b', 'c', 'd', 'e' )
a_tuple = 'a', 'b', 'c', 'd', 'e'
a_tuple = 'a',
type( a_tuple )
Explanation: Chapter 12: Tuples
Contents
- Tuples are immutable
- Tuple assignment
- Tuples as return values
- Variable-length argument tuples
- Lists and tuples
- Dictionaries and tuples
- Comparing tuples
- Sequences of sequences
- Debugging
- Exercises
This notebook is based on "Think Python, 2Ed" by Allen B. Downey <br>
https://greenteapress.com/wp/think-python-2e/
Tuples are immutable
A tuple is a sequence of values
They values can be any type and are index by integers
They are similar to lists, except:
Tuples are immutable
Tuples values usually have different types (unlike lists which generally hold only a single type)
There are multiple ways to create a tuple
End of explanation
a_tuple = tuple()
print( a_tuple )
a_tuple = tuple( 'lupins' )
print( a_tuple )
Explanation: If you want to create a tuple with a single value, add a comma (,) after the value, but don’t add parenthesis
You can also use the built in function tuple
End of explanation
a_tuple = ( 'a', 'b', 'c', 'd', 'e' )
print( a_tuple[1:3] )
print( a_tuple[:3] )
print( a_tuple[1:] )
# Uncomment to see error
# a_tuple[0] = 'z'
Explanation: Most list operators (e.g., bracket and slice) work on tuples
If you try to modify one of the elements, you get an error
End of explanation
a = 5
b = 6
# Conventional value swap
temp = a
a = b
b = temp
print( a, b )
Explanation: Tuple assignment
Often, you may want to swap the values of two variables
This is done conventionally using a temporary variable for storage
End of explanation
a, b = b, a
print( a, b )
Explanation: Python allows you to do it using a tuple assignment
End of explanation
addr = 'monty@python.org'
uname, domain = addr.split( '@' )
print( uname )
print( domain )
Explanation: On the left is a tuple of varaibles and on the right is a tuple of expressions
Note that the number on each side of the equality sign must be the same
The right side can be any kind of sequence (e.g., a string or list)
End of explanation
quotient, remainder = divmod( 7, 3 )
print( quotient )
print( remainder )
Explanation: Tuples as return values
A function can only return one value
However, if we make that value a tuple, we can effectively return multiple values
For example, the divmod function takes two (2) arguments and retunrs a tuple of two (2) values, the quotient and remainder
End of explanation
def min_max( a_tuple ):
    return min( a_tuple ), max( a_tuple )
numbers = ( 13, 7, 55, 42 )
min_num, max_num = min_max( numbers )
print( min_num )
print( max_num )
Explanation: Note the use of a tuple assignment
Here is how to build a function that returns a tuple
End of explanation
def printall( *args ):
    print( args )
printall( 1 , 2.0 , '3' )
Explanation: Note that min and max are built-in functions
Variable-length argument tuples
All the functions we have built and used required a specific number of arguments
You can use tuples to build functions that accept a variable number of arguments
Prepend the argument’s variable name with an * to do this
It is referred to as the gather operator
End of explanation
a_tuple = ( 7, 3 )
# divmod( a_tuple ) # Uncomment to see error
divmod( *a_tuple )
Explanation: The complement is the scatter operator
It allows you to pass a sequence of values as individual arguments to the function
End of explanation
a_string = 'abc'
a_list = [ 0, 1, 2 ]
for element in zip( a_string, a_list ):
    print( element )
Explanation: Lists and tuples
The zip function takes two or more sequences and "zips" them into a list of tuples
End of explanation
for element in zip( 'Peter', 'Tony' ):
    print( element )
Explanation: Essentially, it returns an iterator over a list of tuples
If the sequences aren't the same length, the result has the length of the shorter one
End of explanation
a_list = [ ('a', 0), ('b', 1), ('c', 2) ]
for letter, number in a_list:
    print( letter, number )
Explanation: You can also use tuple assignment to get the individual values
End of explanation
def has_match( tuple1, tuple2 ):
    result = False
    for x, y in zip( tuple1, tuple2 ):
        if( x == y ):
            result = True
    return result
Explanation: You can combine zip, for and tuple assignment to traverse two (or more) sequences at the same time
End of explanation
for index , element in enumerate( 'abc' ):
    print( index, element )
Explanation: If you need the indices, use the enumerate function
End of explanation
a_dict = { 'a': 0, 'b':1, 'c':2 }
dict_items = a_dict.items()
print( type( dict_items ) )
print( dict_items )
for element in dict_items:
    print( element )
Explanation: Dictionaries and tuples
The method items returns a tuple of key-value pairs from a dictionary
End of explanation
for key, value in a_dict.items():
    print( key, value )
Explanation: Remember that there is no particular ordering in a dictionary
You can also add a list of tuples to a dictionary using the update method
Using items, tuple assignment, and a for loop is an easy way to traverse the keys and values of a dictionary
End of explanation
directory = dict()
directory[ 'Smith', 'Bob' ] = '555-1234'
directory[ 'Doe', 'Jane' ] = '555-9786'
for last, first in directory:
    print( first, last, directory[last, first] )
Explanation: Since tuples are immutable, you can even use them as keys in a dictionary
End of explanation
<END_TASK>