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

**DATA 200
Computational Programming for Data Analytics**

Spring 2023
Instructor: Ron Mak
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 5.7 Passing Lists to Functions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Passing an Entire List to a Function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### When we pass an object such as a list to a function, what the function receives is **not a copy** of the argument object, but a **reference** to the object. You can informally think of a reference to an object as the address of the object. Therefore, by using the object reference, the function is able to modify the argument itself." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def modify_elements(items):\n", " \"\"\"\"Multiplies all element values in items by 2.\"\"\"\n", " for i in range(len(items)):\n", " items[i] *= 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numbers = [10, 3, 7, 1, 9]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "modify_elements(numbers)\n", "\n", "numbers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Yes indeed, the function has modified the actual argument, the list `numbers`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Passing a Tuple to a Function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Remember that some sequences such a tuples are immutable." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numbers_tuple = (10, 20, 30)\n", "\n", "numbers_tuple" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "modify_elements(numbers_tuple)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### A Note Regarding Tracebacks" ] }, { "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 }