returning tuples (class 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)
  • They are used in Python a lot, so you need to understand them, even if you don’t use them
  • When functions return multiple values, it returns a tuple

Returning multiple values in a function

def descr_stats(numbers):
  total = 0
  for n in numbers:
    total += n
  return total, total/len(numbers)

def main():
  grades = [90, 87, 83, 95, 100]
  total, mean = descr_stats(grades)
  assert total == 455
  assert mean == 91.0

main()

Write a function

  1. Its name is low_high
  2. It takes two numbers as argument: x and y
  3. It returns a tuple with the lowest number first, the highest number second
assert low_high(4, 2) == (2, 4)
assert low_high(1, 5) == (1, 5)

Write a function – solution

def low_high(x, y):
  if x < y:
    return x, y
  else:
    return y, x
  
def main():
  assert low_high(4, 2) == (2, 4)
  assert low_high(1, 5) == (1, 5)
  
  print("Passed tests.")
  
main()
Passed tests.

Write a function

  1. Its name is sum_and_multiplication
  2. It takes a dictionary as argument
  3. It iterates over the dictionary values, summing all the values and multiplying all the values (create two separate variables, one for the addition and another for the multiplication)
  4. It 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

def sum_and_multiplication(dictionary):
  total = 0
  multiplication = 1
  for key in dictionary:
    total += dictionary[key]
    multiplication *= dictionary[key]
    
  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()

Quiz 9

current time

You have 10 minutes to complete the quiz

  • No need for comments, no need for a main(), no need for test cases

Allowed built-in functions: round(), input(), float(), str(), int(), len(), range(), open()

Write a function

  1. Its name is odds_and_evens
  2. It takes one list of integers as argument
  3. It returns two lists: one with all the odd numbers in integers, and another with all the even numbers in integers
print( 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():
  print( odds_and_evens([2,6,1]) ) # ([1], [2,6])
  
main()
([1], [2, 6])