Python Programming: Advanced Function Arguments


Lesson Objectives

After this lesson, you will be able to…

  • Review all topics to this point.
  • Use keyword arguments in functions.

Review: Functions

Main points:

  • Define functions using the def keyword.
  • A function must be called before the code in it will run!
  • You will recognize function calls by the () at the end.

Review: Function Arguments

  • Provide an argument to a function when you need something small to vary.

Multiple Parameters

Functions can have…


Discussion: Print vs Return

Why doesn’t this do anything?


We Do: Review Exercises

Locally, let’s create a file called function_practice.py.

  • We’ll define a function named areBothEven.

  • It will accept two parameters: num1 and num2.

  • Inside the function, we’ll return True if num1 and num2 are both even but False if they are not.

  • We’ll test this with print(areBothEven(1, 4)), print(areBothEven(2, 4)), and print(areBothEven(2, 3)).


We Do: Another Review Exercise!

In our file, we’ll define another function named lightOrDark that takes the parameter hour.

  • If hour is greater than 24, the function will print “That’s not an hour in the day!” and return nothing.

  • If hour is less than 7 or greater than 17, the function will return “It’s dark outside!”

  • Otherwise, the function will return “It’s light outside!”

  • We’ll test this with print(lightOrDark(4)), print(lightOrDark(26)), and print(lightOrDark(10)).


Discussion: Arguments

Now, let’s make functions a little more sophisticated.

What do you think the following code does?

What if we want all of these to work?


Introducing *args

*args is a parameter that says “Put as many parameters as you’d like!”

  • Pronounced like a pirate - “arrrrghhhs!”
  • Known as positional arguments
  • The * at the beginning is what specifies the variable number of arguments

We Do: *args

Let’s create a local file for this lesson - args_practice.py.

  • We’ll write a function, sum_everything that takes any numbers of arguments and adds them together.
  • At the end, we’ll print out the sum.
  • Let’s try it with sum_everything(4, 5, 6) and sum_everything(6, 4, 5). The order doesn’t matter!
  • *args says “any number” - you can pass in none at all!

Discussion: Often, Order Does Matter.

Let’s switch gears. Back to a set number of arguments!

Check this out:

Without otherwise specifying, x is 1, y is 2, and z is 10.

  • What if we want x, the first parameter to get the value 10?
  • Is there a way to specify which argument goes to which parameter?

Forcing the Order

Here we’ve forced the order to be reversed from the default. In fact, we can specify any ordering we want by using the names of the parameters (keywords) when providing the argument values.

Take a moment to play around with the values until you really believe it!


Keyword Arguments (kwargs)

Using kwargs, odrer deons’t mtater:

  • Arguments are named according to their corresponding parameters.
  • Order doesn’t matter - Python will check the names and match them!
  • Values are assigned because the keyword argument and the parameter name match.

Protip: Keep your parameter names simple and concise to prevent typos and misspellings!


Mix It Up…. but Not With Every Argument?

Fun fact: You can provide some args in order - positional - and some with keywords.

  • Undefined are assigned in sequential order.
  • Keywords have to come last! - then, in any order.

Quick Review

*args: Any number of arguments - even 0! - can be passed in.

Keyword arguments (kwargs): Arguments can be passed in out of order.


Discussion: Variable Numbers of Kwargs?

What if I go to Froyo? I need:

  • One argument spoon, to pick a spoon size.
  • A variable number of arguments for all the flavors of frozen yogurt I might eat!

def yogurt_land(*args)?

  • No! *args won’t work - we need to know which arg is the spoon.

def yogurt_land(spoon, froyo)?

  • No! We don’t know the number of froyo arguments.

Any ideas?


Introducing: **kwargs

The * in *args means: Any number of arguments.

Let’s add ** to our kwargs: **kwargs can take a variable number of arguments. Note the double **!


We Do: 4 Froyos

  • Can we subtract one of the froyos?
  • Where is my 4th froyo?
  • What if I drop all my froyos on the ground? (No kwargs)
  • Can I skip the drink or spoon positional arguments?

Quick Review of Useful Argument Types:

At this point, we have *args, kwargs and **kwargs:


Discussion: Printing

print is a function! That’s why it has parentheses! - It’s built into Python, so you don’t have to define it. You can just use it.

When printing, commas automatically add spaces:

But since print is a function, too - do you think there’s anything we can do to change those spaces to something else?


Delicious Printing

  • We can replace name and dessert with your own name and favorite dessert. It’s a regular print!
  • sep can be any string, or even an icon (they’re made of strings - we’ll see later!) - but not an int.

Quick Review

So far, we’ve learned:

  • *args:
    • ​A variable number of function arguments.
  • kwargs:
    • ​A set number of function arguments.
    • Can be defined out of order
  • **kwargs:
    • Any number of positional arguments.
  • sep in print.

There’s one more: Optional parameters.


Optional Parameters with Default Values

This idea exists in programming - you’ve already seen it!

The default value for sep in print is " ". You don’t need to include it.

This makes it optional! Optional parameters have default values, so you don’t need to include them.

  • Only include them if you want to change them!

Default parameters are in the function declaration.

They’re there if you don’t include a value.


Any Functions: Optional Parameters with Default Values

These can be added to any functions.

Here, c has a default of 20. We don’t need to include it!


Partner Exercise: Poke At It!

Pair up! Choose a driver and a navigator.

  • In your local file, write a function, print_food that has four optional parameters (all with defaults of your choice): favorite_food, lunch_today, lunch_yesterday, and breakfast.

print_food should print out each of these.

Call this with a couple different arguments:

  • No arguments.
  • All arguments - a regular function call.
  • 2 keyword arguments. Give all four arguments, but use a keyword for lunch_yesterday and breakfast.
  • All keyword arguments - out of order.

Partner Exercise: Keep Poking!

Change roles!

Underneath print_food, rewrite it, twice.

First, write print_food_args, using *args as the parameter. Start the function by printing args, so you can see what’s going on. Then, print the values you pass in.

Then, write print_food_kwargs, using **kwargs as the parameter. Start the function by printing kwargs, so you can see what’s going on. Then, as above, print the values you pass in.


Summary + Q&A

  • *args:
    • ​A variable number of function arguments.
    • Taken in any order.
    • def multiply(*args):
  • kwargs:
    • ​A set number of function arguments.
    • Can be defined out of order
    • my_func(a=1, b=2, c=3)
  • **kwargs: ​- Any number of positional arguments.
    • def froyo(*kwargs)
  • sep in print.
  • Optional parameters:
    • Default values in the function declaration
    • def my_func(a=10, b=15, c=20)

Additional Resources