|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "432200e4", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "#### Lamda Functions" |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "code", |
| 13 | + "execution_count": 1, |
| 14 | + "id": "8e6b86ee", |
| 15 | + "metadata": {}, |
| 16 | + "outputs": [], |
| 17 | + "source": [ |
| 18 | + "### Lambda Functions ###\n", |
| 19 | + "\n", |
| 20 | + "# A lambda function is a small anonymous function defined with the lambda keyword.\n", |
| 21 | + "# It can take any number of arguments but can only have one expression.\n", |
| 22 | + "# The expression is evaluated and returned when the function is called.\n" |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "cell_type": "code", |
| 27 | + "execution_count": 2, |
| 28 | + "id": "107cde44", |
| 29 | + "metadata": {}, |
| 30 | + "outputs": [], |
| 31 | + "source": [ |
| 32 | + "# Lambda functions are often used for short, throwaway functions that are not reused elsewhere in the code.\n", |
| 33 | + "# Syntax:\n", |
| 34 | + "# lambda arguments: expression\n" |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "code", |
| 39 | + "execution_count": 6, |
| 40 | + "id": "0255f1a2", |
| 41 | + "metadata": {}, |
| 42 | + "outputs": [ |
| 43 | + { |
| 44 | + "name": "stdout", |
| 45 | + "output_type": "stream", |
| 46 | + "text": [ |
| 47 | + "15\n" |
| 48 | + ] |
| 49 | + } |
| 50 | + ], |
| 51 | + "source": [ |
| 52 | + "# Using basic functions\n", |
| 53 | + "def addition(a,b):\n", |
| 54 | + " return a + b\n", |
| 55 | + "print(addition(5,10)) # Output: 15\n", |
| 56 | + "\n", |
| 57 | + "# What are the disadvantages of using normal functions?\n", |
| 58 | + "# 1. More lines of code.\n", |
| 59 | + "# 2. Less readable for small functions.\n", |
| 60 | + "# 3. Cannot be used in places where a function is required as an argument (like in map, filter, etc.) \n", |
| 61 | + "\n" |
| 62 | + ] |
| 63 | + }, |
| 64 | + { |
| 65 | + "cell_type": "code", |
| 66 | + "execution_count": 5, |
| 67 | + "id": "35283896", |
| 68 | + "metadata": {}, |
| 69 | + "outputs": [ |
| 70 | + { |
| 71 | + "name": "stdout", |
| 72 | + "output_type": "stream", |
| 73 | + "text": [ |
| 74 | + "15\n" |
| 75 | + ] |
| 76 | + } |
| 77 | + ], |
| 78 | + "source": [ |
| 79 | + "# Using lambda functions\n", |
| 80 | + "addition_lambda = lambda a, b: a + b\n", |
| 81 | + "print(addition_lambda(5, 10)) # Output: 15" |
| 82 | + ] |
| 83 | + }, |
| 84 | + { |
| 85 | + "cell_type": "code", |
| 86 | + "execution_count": 7, |
| 87 | + "id": "4affbf21", |
| 88 | + "metadata": {}, |
| 89 | + "outputs": [ |
| 90 | + { |
| 91 | + "data": { |
| 92 | + "text/plain": [ |
| 93 | + "True" |
| 94 | + ] |
| 95 | + }, |
| 96 | + "execution_count": 7, |
| 97 | + "metadata": {}, |
| 98 | + "output_type": "execute_result" |
| 99 | + } |
| 100 | + ], |
| 101 | + "source": [ |
| 102 | + "def even(num):\n", |
| 103 | + " if num%2==0:\n", |
| 104 | + " return True\n", |
| 105 | + "even(24)" |
| 106 | + ] |
| 107 | + }, |
| 108 | + { |
| 109 | + "cell_type": "code", |
| 110 | + "execution_count": 9, |
| 111 | + "id": "1af47f86", |
| 112 | + "metadata": {}, |
| 113 | + "outputs": [ |
| 114 | + { |
| 115 | + "name": "stdout", |
| 116 | + "output_type": "stream", |
| 117 | + "text": [ |
| 118 | + "True\n" |
| 119 | + ] |
| 120 | + } |
| 121 | + ], |
| 122 | + "source": [ |
| 123 | + "even1 = lambda num: num%2==0\n", |
| 124 | + "print(even1(24))" |
| 125 | + ] |
| 126 | + }, |
| 127 | + { |
| 128 | + "cell_type": "code", |
| 129 | + "execution_count": 10, |
| 130 | + "id": "2b2584e9", |
| 131 | + "metadata": {}, |
| 132 | + "outputs": [ |
| 133 | + { |
| 134 | + "data": { |
| 135 | + "text/plain": [ |
| 136 | + "39" |
| 137 | + ] |
| 138 | + }, |
| 139 | + "execution_count": 10, |
| 140 | + "metadata": {}, |
| 141 | + "output_type": "execute_result" |
| 142 | + } |
| 143 | + ], |
| 144 | + "source": [ |
| 145 | + "def addition(x,y,z):\n", |
| 146 | + " return x + y + z\n", |
| 147 | + "addition(12,13,14)" |
| 148 | + ] |
| 149 | + }, |
| 150 | + { |
| 151 | + "cell_type": "code", |
| 152 | + "execution_count": 11, |
| 153 | + "id": "8d1f06cb", |
| 154 | + "metadata": {}, |
| 155 | + "outputs": [ |
| 156 | + { |
| 157 | + "name": "stdout", |
| 158 | + "output_type": "stream", |
| 159 | + "text": [ |
| 160 | + "39\n" |
| 161 | + ] |
| 162 | + } |
| 163 | + ], |
| 164 | + "source": [ |
| 165 | + "addition1 = lambda x,y,z: x + y + z\n", |
| 166 | + "print(addition1(12,13,14))\n", |
| 167 | + "\n" |
| 168 | + ] |
| 169 | + }, |
| 170 | + { |
| 171 | + "cell_type": "code", |
| 172 | + "execution_count": 12, |
| 173 | + "id": "6f0439e4", |
| 174 | + "metadata": {}, |
| 175 | + "outputs": [ |
| 176 | + { |
| 177 | + "data": { |
| 178 | + "text/plain": [ |
| 179 | + "4" |
| 180 | + ] |
| 181 | + }, |
| 182 | + "execution_count": 12, |
| 183 | + "metadata": {}, |
| 184 | + "output_type": "execute_result" |
| 185 | + } |
| 186 | + ], |
| 187 | + "source": [ |
| 188 | + "## MAP() FUNCTION \n", |
| 189 | + "# It applies a given function to all items in an iterable (like a list) and returns a map object (which is an iterator).\n", |
| 190 | + "\n", |
| 191 | + "numbers = [1, 2, 3, 4, 5]\n", |
| 192 | + "def square(num):\n", |
| 193 | + " return num ** 2\n", |
| 194 | + "square(2)" |
| 195 | + ] |
| 196 | + }, |
| 197 | + { |
| 198 | + "cell_type": "code", |
| 199 | + "execution_count": 14, |
| 200 | + "id": "306978a4", |
| 201 | + "metadata": {}, |
| 202 | + "outputs": [ |
| 203 | + { |
| 204 | + "data": { |
| 205 | + "text/plain": [ |
| 206 | + "[1, 4, 9, 16, 25]" |
| 207 | + ] |
| 208 | + }, |
| 209 | + "execution_count": 14, |
| 210 | + "metadata": {}, |
| 211 | + "output_type": "execute_result" |
| 212 | + } |
| 213 | + ], |
| 214 | + "source": [ |
| 215 | + "list(map(lambda num: num ** 2, numbers))" |
| 216 | + ] |
| 217 | + } |
| 218 | + ], |
| 219 | + "metadata": { |
| 220 | + "kernelspec": { |
| 221 | + "display_name": "open3d_env", |
| 222 | + "language": "python", |
| 223 | + "name": "python3" |
| 224 | + }, |
| 225 | + "language_info": { |
| 226 | + "codemirror_mode": { |
| 227 | + "name": "ipython", |
| 228 | + "version": 3 |
| 229 | + }, |
| 230 | + "file_extension": ".py", |
| 231 | + "mimetype": "text/x-python", |
| 232 | + "name": "python", |
| 233 | + "nbconvert_exporter": "python", |
| 234 | + "pygments_lexer": "ipython3", |
| 235 | + "version": "3.10.16" |
| 236 | + } |
| 237 | + }, |
| 238 | + "nbformat": 4, |
| 239 | + "nbformat_minor": 5 |
| 240 | +} |
0 commit comments