{ "cells": [ { "cell_type": "markdown", "id": "1205f428-b60e-44c1-bde0-9961eb4aefba", "metadata": {}, "source": [ "###
San Jose State University
Department of Applied Data Science

**DATA 200
Computational Programming for Data Analytics**

Spring 2024
Instructor: Ron Mak
" ] }, { "cell_type": "markdown", "id": "a70ed1ba-7108-4bb3-936c-5dc84484caf3", "metadata": {}, "source": [ "# Lambda Expressions\n", "#### A ***lambda expression*** is a nameless function used in specific situations where the function is automatically called." ] }, { "cell_type": "markdown", "id": "06e3832c-7ace-4bd5-9054-6399478b36eb", "metadata": {}, "source": [ "## Filter out all odd values from a list of integers.\n", "``` Python\n", "lambda x: (x%2 == 0)\n", "```" ] }, { "cell_type": "code", "execution_count": null, "id": "0de550df-fa55-4bff-b787-8039175c2949", "metadata": {}, "outputs": [], "source": [ "lst = list(range(20))\n", "lst" ] }, { "cell_type": "code", "execution_count": null, "id": "517f54c7-2037-4eea-9264-a2e1691bb785", "metadata": {}, "outputs": [], "source": [ "evens = list(filter(lambda x: (x%2 == 0), lst))\n", "evens " ] }, { "cell_type": "markdown", "id": "839a6378-5a28-4ae9-bf27-38132a692d8d", "metadata": {}, "source": [ "## Map all words to upper case.\n", "``` Python\n", "lambda pet: (pet.upper())\n", "``` " ] }, { "cell_type": "code", "execution_count": null, "id": "ba7d9ba1-5bd6-457a-a252-6ef893a8f8a1", "metadata": {}, "outputs": [], "source": [ "pets = ['cat', 'kitten', 'dog', 'puppy', 'goldfish', 'parrot']\n", "pets" ] }, { "cell_type": "code", "execution_count": null, "id": "1a08c184-35c3-46fd-867e-08a22a4ded5d", "metadata": {}, "outputs": [], "source": [ "uppers = list(map(lambda pet: (pet.upper()), pets))\n", "uppers" ] }, { "cell_type": "markdown", "id": "7f2d4de6-8e7e-4748-b848-728f742f42bb", "metadata": {}, "source": [ "#### (c) 2023 by Ronald Mak" ] }, { "cell_type": "code", "execution_count": null, "id": "190a085f-d5e1-4251-9ec4-b02b0a717043", "metadata": {}, "outputs": [], "source": [] } ], "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": 5 }