-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathExtras_Lambda_ Function
More file actions
1 lines (1 loc) · 3.72 KB
/
Copy pathExtras_Lambda_ Function
File metadata and controls
1 lines (1 loc) · 3.72 KB
1
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"authorship_tag":"ABX9TyMdOz2NHZJiUyBZtR/o5bJ5"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","source":["# **Python Lambda Function**\n","A small, anonymous function that can be defined in a single lin of code.\n","\n","Basic Syntax:\n","\n","```\n","# variable_name = lambda arguments : expression\n","```\n","\n","* **variable_name**: variable name that acts as a function after decleration\n","* **lambda**: Lambda keyword\n","* **arguments**: Any number of arguments passed to the lambda function\n","* **expression**: A single expression to evaluate and return te resulting value\n","\n","\n"],"metadata":{"id":"vZLW7ycBC-6T"}},{"cell_type":"code","source":["square = lambda x: x**2\n","result = square(5)\n","print(result)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"Q0NtuoujEQV0","executionInfo":{"status":"ok","timestamp":1761910895510,"user_tz":-180,"elapsed":72,"user":{"displayName":"ünal hayta","userId":"12872051804862692116"}},"outputId":"e9df2466-30ed-460c-83bd-05c2c8608174"},"execution_count":2,"outputs":[{"output_type":"stream","name":"stdout","text":["25\n"]}]},{"cell_type":"code","source":["add = lambda x, y: x + y\n","result = add(5, 3)\n","print(result)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"94AnR8YZErI9","executionInfo":{"status":"ok","timestamp":1761910911189,"user_tz":-180,"elapsed":52,"user":{"displayName":"ünal hayta","userId":"12872051804862692116"}},"outputId":"9b363626-0f11-4131-a65b-6e95ac3d67f4"},"execution_count":3,"outputs":[{"output_type":"stream","name":"stdout","text":["8\n"]}]},{"cell_type":"markdown","source":["**Lambda function can also used as parameters for other functions**"],"metadata":{"id":"6BaLeVn5Ex2X"}},{"cell_type":"code","source":["myList = [1,2,3,4,5]\n","result = list(map(lambda x: x**2, myList)) # map() function applies given function to each elemnt of the list.\n","print(result)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"isp9vSSWEu-L","executionInfo":{"status":"ok","timestamp":1761911259227,"user_tz":-180,"elapsed":55,"user":{"displayName":"ünal hayta","userId":"12872051804862692116"}},"outputId":"d483e524-c879-4255-ec2d-8fac71607277"},"execution_count":7,"outputs":[{"output_type":"stream","name":"stdout","text":["[1, 4, 9, 16, 25]\n"]}]},{"cell_type":"code","source":["my_list = [1,2,3,4,5,6,7,8,9,10]\n","result = list(filter(lambda x: x % 2 == 0, my_list)) # filter() function creats a list of elements for which function is True.\n","print(result)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"1HDTNDctFdW8","executionInfo":{"status":"ok","timestamp":1761911577533,"user_tz":-180,"elapsed":86,"user":{"displayName":"ünal hayta","userId":"12872051804862692116"}},"outputId":"7cbbee26-f999-4363-ceda-cd112a2ecf99"},"execution_count":9,"outputs":[{"output_type":"stream","name":"stdout","text":["[2, 4, 6, 8, 10]\n"]}]},{"cell_type":"code","source":["from functools import reduce\n","my_List = [1,2,3,4,5]\n","result = reduce(lambda x,y: x*y, my_List) # reduce() function applies given function to the element of an iterable in a cumulative way, and returns a single value.\n","print(result)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"VzI4PkxQHHWA","executionInfo":{"status":"ok","timestamp":1761911779753,"user_tz":-180,"elapsed":37,"user":{"displayName":"ünal hayta","userId":"12872051804862692116"}},"outputId":"50798118-6cd0-4df1-de78-83045ac7cfdc"},"execution_count":11,"outputs":[{"output_type":"stream","name":"stdout","text":["120\n"]}]},{"cell_type":"code","source":[],"metadata":{"id":"FSmpR0HoHs66"},"execution_count":null,"outputs":[]}]}