
# Tuples are lists’ immutable cousins:
#
#   Pretty much anything you can do to a list that doesn’t
#   involve modifying it, you can do to a tuple.

# You specify a tuple by using parentheses (or nothing) instead 
# of square brackets (for lists):

my_list = [1, 2]

my_tuple = (1, 2)
other_tuple = 3, 4

# List elements are MUTABLE:
my_list[1] = 3 			# my_list is now [1, 3]
print(my_list)

# Tuple elements are IMMUTABLE:
try:
    my_tuple[1] = 3
except TypeError:
    print("cannot modify a tuple")

# -------------------------------------------------------------------------
# Functions can return tuples.
#
#     Tuples are a convenient way to return multiple values from functions
# -------------------------------------------------------------------------

def sum_and_product(x, y):
    return (x + y),(x * y)

# This call assigns a tuple to sp
sp = sum_and_product(2, 3) 			# equals (5, 6)
print(sp)

# This call UNPACKS a tuple to s and p
s, p = sum_and_product(2, 3) 			# s is 5, p is 6
print(s, p)


# Tuples (and lists) can also be used for multiple assignment:

x, y = 1, 2 		# now x is 1, y is 2
print(x,y)

x, y = y, x 		# Pythonic way to swap variables; now x is 2, y is 1
print(x,y)


