returning tuples (slides)

CSc 110 - returning tuples

What are tuples?

  • immutable lists

  • a sequence of zero or more values (just like lists)

  • addressable by index (just like lists)

  • iterable (just like lists)

tuple syntax

Use () to create a new tuple:

my_tuple = (2, 4, 6)
my_tuple[0]
2
my_tuple[2]
6

Empty tuple:

empty = ()
empty
()

tuples are immutable

No assignment is allowed

my_tuple = (2, 4, 6)
my_tuple[1] = 99 # will fail and cause an exception

Why use tuples?

  • They are a little more efficient than lists
  • They can be used as dictionary keys (important)
  • When functions return multiple values, it returns a tuple

Why use tuples?

Tuples can be used as dictionary keys (lists cannot).

coordinates = {
    (40.7128, -74.0060): "New York",
    (34.0522, -118.2437): "Los Angeles",
    (51.5074, -0.1278): "London"
}

coordinates
{(40.7128, -74.006): 'New York',
 (34.0522, -118.2437): 'Los Angeles',
 (51.5074, -0.1278): 'London'}

Returning multiple values

def low_high(x, y):
  if x < y:
    return x, y
  else:
    return y, x
  
def main():
  print(low_high(4, 2))
  low, high = low_high(4, 2)
  assert low == 2
  assert high == 4
  
main()
(2, 4)

Write a function

It calculates and returns the total and average of all people’s numbers.

grades = ['Eric', 90, 'Sam', 87, 'Josh', 83, 'Susan', 100]
total, mean = descr_stats(grades)
assert total == 360
assert mean == 90.0

Write a function – solution

def descr_stats(numbers):
  total = 0
  count = 0
  for i in range(1, len(numbers), 2):
    total += numbers[i]
    count += 1
  return total, total/count

def main():
  grades = ['Eric', 90, 'Sam', 87, 'Josh', 83, 'Susan', 100]
  print(descr_stats(grades))
  total, mean = descr_stats(grades)
  assert total == 360
  assert mean == 90.0

main()
(360, 90.0)

Write a function

It takes a dictionary as argument and returns the sum and the multiplication of the values in the dictionary

test_dict = {"a": 2, "b": 3, "c": 5}
total, mult = sum_and_multiplication(test_dict)
assert total == 10
assert mult == 30

Write a function – solution 1

def sum_and_multiplication(dictionary):
  total = 0
  multiplication = 1
  for k in dictionary:
    total += dictionary[k]
    multiplication *= dictionary[k]
  return total, multiplication

def main():
  test_dict = {"a": 2, "b": 3, "c": 5}
  total, mult = sum_and_multiplication(test_dict)
  assert total == 10
  assert mult == 30
  
main()

Write a function – solution 2

def sum_and_multiplication(dictionary):
  total = 0
  multiplication = 1
  for v in dictionary.values():
    total += v
    multiplication *= v
  return total, multiplication

def main():
  test_dict = {"a": 2, "b": 3, "c": 5}
  total, mult = sum_and_multiplication(test_dict)
  assert total == 10
  assert mult == 30
  
main()

Write a function

It takes one list of integers as argument and returns two lists: one with all the odd numbers and the other with all the even numbers.

assert odds_and_evens([2,6,1]) == ([1], [2,6])

Write a function – solution

def odds_and_evens(integers):
  odds = []
  evens = []
  for n in integers:
    if n % 2 == 0:
      evens.append(n)
    else:
      odds.append(n)
  return odds, evens
      
def main():
  assert odds_and_evens([2,6,1]) == ([1], [2,6])
  
main()