Tuples
# ----------------------------------------------
# TUPLES
# ----------------------------------------------
# A tuple is a comma-separated sequence of values.
# Parentheses are common, but commas define the tuple.
#
# A single value in parantheses is not a tuple.
# For a single element, include the final comma.
t1 = 'a', 'b', 'c'
t2 = ('a', 'b', 'c')
print(type(t1)) # <class 'tuple'>
print(type(t2)) # <class 'tuple'>
# -----------------------------
# SINGLE ELEMENT TUPLES
# -----------------------------
# Parentheses alone don NOT crete a tuple.
# The comma does.
t1 = 'a'
t2 = ('a')
print(type(t1)) # <class 'str'>
print(type(t2)) # <class 'str'>
t3 = ('a',)
print(type(t3)) # <class 'tuple'>
# -----------------------------
# IMMUTABLE
# -----------------------------
# List are mutalbe
mylist = [1, 2, 3]
mylist[0] = 4
print(mylist) # [4, 2, 3]
# Tuples are immutable
mytuple = (1, 2, 3)
# mytuple[0] = 4
# TypeError: 'tuple' object does not support item assignment
# --------------------------------
# REPLACING A TUPLE
# --------------------------------
# You cannot change elements,
# but you cand bind the variable to a new tuple.
t = ('a', 'b', 'c', 'd', 'e')
t = ('A',) + t[2:]
print(t) # ('A', 'c', 'd', 'e')
Variable Swapping
# ---------------------------------
# VARIABLE SWAPPING
# ---------------------------------
# Tuple unpacking makes swapping simple and save.
c = 3
d = 4
c, d = d, c
assert (c, d) == (4, 3)
# Traditional approach (more verbose)
a = 1
b = 2
tmp = a
a = b
b = tmp
assert (a, b) == (2, 1)
# ------------------------------------------
# UNPACKING (example)
# -----------------------------------------
# Multiple variables can receive values at once.
# This is extremely common in Python code.
name, domain = "office@google.com".split('@')
assert name == "office"
assert domain == "google.com"
Tuples as Dictionaries Keys
# ---------------------------------------
# TUPLES AS DICTIONARY KEYS
# ---------------------------------------
# Tuples are often uses as compound keys.
phonebook = {
("Johny", "Cash"): 4007344455,
("John", "Lennon"): 400768696,
}
for first, last in phonebook:
print(first, last, phonebook[first, last])
# Johny Cash 4007344455
# John Lennon 400768696
Tuples and Functions
# --------------------------------------
# MULTIPLE RETURN VALUES
# --------------------------------------
# Functions return ONE object.
# That object is often a tuple.
#
# Example:
# - Quontient & Reminder:
#
# To compute the quontient and reminders
# it is better to compute both at the same time.
quot = 7//3
rem = 7%3
assert (quot, rem) == (2, 1)
quot, rem = divmod(7, 3) # built-in function (returns tuple)
assert (quot, rem) == (2, 1)
# ------------------------------------------
# UNPACKING ARGUMENTS
# ------------------------------------------
# * expands a tuple into separate arguments.
#
# Functions can take a variable number of arguments.
# A parameter name that begins with * gathers arguments into a tuple.
t = (7, 3)
# divmod(t)
# TypeError: divmod expected 2 arguments, got 1
assert divmod(*t) == (2, 1) # it works!