{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "###
San Jose State University
Dep|artment of Applied Data Science

**DATA 200
Computational Programming for Data Analytics**

Spring 2024
Instructor: Ron Mak
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 4.11 Arbitrary Argument Lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### We can define a function with an **arbitrary argument list**, meaning we can call it with any number of arguments. In the function definition, we use a parameter of the form `*args`. That tells Python to pack the arguments into a tuple by that name. By convention, the name`args` is used, but you can use any name." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Defining a Function with an Arbitrary Argument List" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def average(*args):\n", " return sum(args) / len(args)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "average(5, 10)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "average(5, 10, 15)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "average(5, 10, 15, 20)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### If the function has multiple parameters, the `*args` parameter must be the rightmost one." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Passing an Iterable’s Individual Elements as Function Arguments " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### You can use the `*` operator to unpack a list, tuple, or any other iterable object to pass its individual items to a function." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "grades = [88, 75, 96, 55, 83]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "average(*grades)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### This is equivalent to:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "average(88, 75, 96, 55, 83)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### This function `print_minimum()` must be called with at **least two arguments**, but there can be more." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def print_minimum(parm1, parm2, *args):\n", " m = min(parm1, parm2, *args)\n", " print(f'The minimum of {len(args) + 2} values is {m}')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print_minimum(34, 12)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print_minimum(34, 12, 7, 46, 91)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print_minimum(34, 12, *range(5, 11))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print_minimum(34)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### **EXAMPLE:** Create a function named `calculate_product` that receives an arbitrary argument list and returns the product of all the arguments. Call the function with the arguments `10`, `20` and `30` then with the sequence of integers produced by `range(1, 6, 2)`." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def calculate_product(*args):\n", " product = 1\n", " \n", " for value in args:\n", " product *= value\n", " \n", " return product" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "calculate_product(10, 20, 30)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "calculate_product(*range(1, 6, 2))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Keyworded Variable Argument Lists" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### We can define a function with a **keyworded variable argument list**. By convention, the name `kwargs` is used as the function parameter." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def employee_info(**kwargs):\n", " for key, value in kwargs.items():\n", " print(f'{key = }, {value = }')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "employee_info(first='Bob', last='Smith', id=12345)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def misc_items(label, **kwargs):\n", " print(f'{label}:')\n", "\n", " for key, value in kwargs.items():\n", " print(f' {key} is {value}')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "misc_items('Colors', background='white', border='black', margins='gray')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "misc_items('Companies', computer='Apple', food=\"MacDonald's\", university='SJSU', city='San Jose')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "##########################################################################\n", "# (C) Copyright 2019 by Deitel & Associates, Inc. and #\n", "# Pearson Education, Inc. All Rights Reserved. #\n", "# #\n", "# DISCLAIMER: The authors and publisher of this book have used their #\n", "# best efforts in preparing the book. These efforts include the #\n", "# development, research, and testing of the theories and programs #\n", "# to determine their effectiveness. The authors and publisher make #\n", "# no warranty of any kind, expressed or implied, with regard to these #\n", "# programs or to the documentation contained in these books. The authors #\n", "# and publisher shall not be liable in any event for incidental or #\n", "# consequential damages in connection with, or arising out of, the #\n", "# furnishing, performance, or use of these programs. #\n", "##########################################################################\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Additional material (C) Copyright 2023 by Ronald Mak" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.5" } }, "nbformat": 4, "nbformat_minor": 4 }