Multiple Parameters and Return Values

Introduction to Functions in Python

Hugo Bowne-Anderson

Instructor

Multiple function parameters

  • Accept more than 1 parameter:
def raise_to_power(value1, value2):
    """Raise value1 to the power of value2."""
    new_value = value1 ** value2
    return new_value
  • Call function: # of arguments = # of parameters
result = raise_to_power(2, 3)

print(result)
8
Introduction to Functions in Python

A quick jump into tuples

  • Make functions return multiple values: Tuples!

  • Tuples:

    • Like a list - can contain multiple values

    • Immutable - can’t modify values!

    • Constructed using parentheses ()

even_nums = (2, 4, 6)

print(type(even_nums))
<class 'tuple'>
Introduction to Functions in Python

Unpacking tuples

  • Unpack a tuple into several variables:
even_nums = (2, 4, 6)

a, b, c = even_nums
print(a)
2
print(b)
4
print(c)
6
Introduction to Functions in Python

Accessing tuple elements

  • Access tuple elements like you do with lists:
even_nums = (2, 4, 6)

print(even_nums[1])
4
second_num = even_nums[1]

print(second_num)
4
  • Uses zero-indexing
Introduction to Functions in Python

Returning multiple values

def raise_both(value1, value2):
    """Raise value1 to the power of value2
    and vice versa."""

    new_value1 = value1 ** value2
    new_value2 = value2 ** value1

    new_tuple = (new_value1, new_value2)

    return new_tuple
result = raise_both(2, 3)

print(result)
(8, 9)
Introduction to Functions in Python

Let's practice!

Introduction to Functions in Python

Preparing Video For Download...