Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d2dcb73

Browse files
Updates on functions
1 parent 58ea5d4 commit d2dcb73

File tree

4 files changed

+16435
-59
lines changed

4 files changed

+16435
-59
lines changed

‎.ipynb_checkpoints/Functions-checkpoint.ipynb

Lines changed: 97 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,29 @@
7878
"# 13"
7979
]
8080
},
81+
{
82+
"cell_type": "markdown",
83+
"metadata": {},
84+
"source": [
85+
"### Naming a function\n",
86+
"\n",
87+
"Like variables, python functions should also be in `lower_snake_case`\n",
88+
"\n",
89+
"One of the more universal, yet simple rules is: Function names should be verbs if the function changes the state of the program, and nouns if they're used to return a certain value.\n",
90+
"\n",
91+
"1. *Self-explanatory names*: a function `get_name()` will tell the developer what it returns as well as set_address(), is_male(), etc.\n",
92+
"2. *Short*: a function name must be as short as possible so that it's simple to type as well as easy to remember. A function `get_number_of_pages_in_the_book()` is not good, something like `get_book_page_count()` is better.\n",
93+
"3. Use of prefixes: use prefixes in the functions such as `get_name()`, `set_name()`, etc.\n",
94+
"\n",
95+
"The most important thing to note is to follow a constant naming convention throughout the function"
96+
]
97+
},
8198
{
8299
"cell_type": "markdown",
83100
"metadata": {},
84101
"source": [
85102
"## Lambda function (Inline or Anonymous function):\n",
86-
"One liner of a function\n",
103+
"One liner of a function. It is created for use at one point or one statememt and is not intended to be named and stored.\n",
87104
"\n",
88105
"Syntax: \n",
89106
"```python\n",
@@ -93,7 +110,7 @@
93110
},
94111
{
95112
"cell_type": "code",
96-
"execution_count": 6,
113+
"execution_count": 9,
97114
"metadata": {},
98115
"outputs": [
99116
{
@@ -107,21 +124,33 @@
107124
"name": "stdout",
108125
"output_type": "stream",
109126
"text": [
110-
"True\n"
127+
"From is_even(x): True\n",
128+
"From lambda: True\n"
111129
]
112130
}
113131
],
114132
"source": [
115133
"# Example 1\n",
116-
"# def c(x):\n",
117-
"# return x % 2 == 0 #One line function to check if even\n",
134+
"\n",
135+
"def is_even(x):\n",
136+
" return x % 2 == 0 # One line function to check if even\n",
137+
"\n",
118138
"a = int(input())\n",
119-
"c = lambda x: x % 2 == 0 # Equivalent lambda function\n",
120-
"print(c(a))\n",
139+
"print(\"From is_even(x):\", is_even(a))\n",
140+
"print(\"From lambda:\", (lambda x: x % 2 == 0)(a)) # Equivalent lambda function\n",
121141
"\n",
122142
"# Input: 10"
123143
]
124144
},
145+
{
146+
"cell_type": "markdown",
147+
"metadata": {},
148+
"source": [
149+
"The `lambda` function is generally created for use with iterables where the function is applied to several values but at only once (that part of the code).\n",
150+
"\n",
151+
"The usual places include maps, filter and keys for sorting for any iterable. Visit [Lists](DS_Lists.ipynb) or [Tuples](DS_Tuples.ipynb) to know more on them"
152+
]
153+
},
125154
{
126155
"cell_type": "markdown",
127156
"metadata": {},
@@ -177,6 +206,7 @@
177206
"source": [
178207
"Breaking down of problem:\n",
179208
"\n",
209+
"```python\n",
180210
"n = 6 \n",
181211
"n <= 1 False \n",
182212
"6 * factorial(5) \n",
@@ -203,14 +233,69 @@
203233
"\n",
204234
"Building up:\n",
205235
"\n",
206-
"1 <br>\n",
236+
"1\n",
207237
"2 * 1 \n",
208238
"3 * 2 * 1 \n",
209239
"4 * 3 * 2 * 1 \n",
210240
"5 * 4 * 3 * 2 * 1 \n",
211241
"6 * 5 * 4 * 3 * 2 * 1 \n",
212242
"\n",
213-
"720"
243+
"720\n",
244+
"```"
245+
]
246+
},
247+
{
248+
"cell_type": "markdown",
249+
"metadata": {},
250+
"source": [
251+
"You can find the steps in recursion below \n",
252+
"The '|' line indicates the same level of recursive call"
253+
]
254+
},
255+
{
256+
"cell_type": "code",
257+
"execution_count": 5,
258+
"metadata": {},
259+
"outputs": [
260+
{
261+
"name": "stdout",
262+
"output_type": "stream",
263+
"text": [
264+
"6\n",
265+
"|\t5\n",
266+
"|\t|\t4\n",
267+
"|\t|\t|\t3\n",
268+
"|\t|\t|\t|\t2\n",
269+
"|\t|\t|\t|\t|\t1\n",
270+
"|\t|\t|\t|\t2\n",
271+
"|\t|\t|\t6\n",
272+
"|\t|\t24\n",
273+
"|\t120\n",
274+
"720\n"
275+
]
276+
},
277+
{
278+
"data": {
279+
"text/plain": [
280+
"720"
281+
]
282+
},
283+
"execution_count": 5,
284+
"metadata": {},
285+
"output_type": "execute_result"
286+
}
287+
],
288+
"source": [
289+
"# The indent paramter defines the offset of output. This program is to understand recursion only.\n",
290+
"def fact(n, indent=''):\n",
291+
" print(indent, n, sep='')\n",
292+
" if 0 <= n <= 1: return 1\n",
293+
" else:\n",
294+
" fac = n * fact(n-1, indent + \"|\\t\")\n",
295+
" print(indent, fac, sep='')\n",
296+
" return fac\n",
297+
"\n",
298+
"fact(6)"
214299
]
215300
},
216301
{
@@ -459,7 +544,7 @@
459544
},
460545
{
461546
"cell_type": "code",
462-
"execution_count": 34,
547+
"execution_count": 2,
463548
"metadata": {},
464549
"outputs": [
465550
{
@@ -488,7 +573,7 @@
488573
"source": [
489574
"#Example\n",
490575
"# Example \n",
491-
"def add(a, *b): # Function Definition - * incicates variable length arguments\n",
576+
"def add(a, *b): # Function Definition - * indicates variable length arguments\n",
492577
" print(b)\n",
493578
" return a + sum(b) # returns the sum; sum is a built in function that returns sum of elements in an iterable\n",
494579
"x = int(input())\n",
@@ -1230,7 +1315,7 @@
12301315
"name": "python",
12311316
"nbconvert_exporter": "python",
12321317
"pygments_lexer": "ipython3",
1233-
"version": "3.9.4"
1318+
"version": "3.9.6"
12341319
}
12351320
},
12361321
"nbformat": 4,

‎Functions.ipynb

Lines changed: 97 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,29 @@
7878
"# 13"
7979
]
8080
},
81+
{
82+
"cell_type": "markdown",
83+
"metadata": {},
84+
"source": [
85+
"### Naming a function\n",
86+
"\n",
87+
"Like variables, python functions should also be in `lower_snake_case`\n",
88+
"\n",
89+
"One of the more universal, yet simple rules is: Function names should be verbs if the function changes the state of the program, and nouns if they're used to return a certain value.\n",
90+
"\n",
91+
"1. *Self-explanatory names*: a function `get_name()` will tell the developer what it returns as well as set_address(), is_male(), etc.\n",
92+
"2. *Short*: a function name must be as short as possible so that it's simple to type as well as easy to remember. A function `get_number_of_pages_in_the_book()` is not good, something like `get_book_page_count()` is better.\n",
93+
"3. Use of prefixes: use prefixes in the functions such as `get_name()`, `set_name()`, etc.\n",
94+
"\n",
95+
"The most important thing to note is to follow a constant naming convention throughout the function"
96+
]
97+
},
8198
{
8299
"cell_type": "markdown",
83100
"metadata": {},
84101
"source": [
85102
"## Lambda function (Inline or Anonymous function):\n",
86-
"One liner of a function\n",
103+
"One liner of a function. It is created for use at one point or one statememt and is not intended to be named and stored.\n",
87104
"\n",
88105
"Syntax: \n",
89106
"```python\n",
@@ -93,7 +110,7 @@
93110
},
94111
{
95112
"cell_type": "code",
96-
"execution_count": 6,
113+
"execution_count": 9,
97114
"metadata": {},
98115
"outputs": [
99116
{
@@ -107,21 +124,33 @@
107124
"name": "stdout",
108125
"output_type": "stream",
109126
"text": [
110-
"True\n"
127+
"From is_even(x): True\n",
128+
"From lambda: True\n"
111129
]
112130
}
113131
],
114132
"source": [
115133
"# Example 1\n",
116-
"# def c(x):\n",
117-
"# return x % 2 == 0 #One line function to check if even\n",
134+
"\n",
135+
"def is_even(x):\n",
136+
" return x % 2 == 0 # One line function to check if even\n",
137+
"\n",
118138
"a = int(input())\n",
119-
"c = lambda x: x % 2 == 0 # Equivalent lambda function\n",
120-
"print(c(a))\n",
139+
"print(\"From is_even(x):\", is_even(a))\n",
140+
"print(\"From lambda:\", (lambda x: x % 2 == 0)(a)) # Equivalent lambda function\n",
121141
"\n",
122142
"# Input: 10"
123143
]
124144
},
145+
{
146+
"cell_type": "markdown",
147+
"metadata": {},
148+
"source": [
149+
"The `lambda` function is generally created for use with iterables where the function is applied to several values but at only once (that part of the code).\n",
150+
"\n",
151+
"The usual places include maps, filter and keys for sorting for any iterable. Visit [Lists](DS_Lists.ipynb) or [Tuples](DS_Tuples.ipynb) to know more on them"
152+
]
153+
},
125154
{
126155
"cell_type": "markdown",
127156
"metadata": {},
@@ -177,6 +206,7 @@
177206
"source": [
178207
"Breaking down of problem:\n",
179208
"\n",
209+
"```python\n",
180210
"n = 6 \n",
181211
"n <= 1 False \n",
182212
"6 * factorial(5) \n",
@@ -203,14 +233,69 @@
203233
"\n",
204234
"Building up:\n",
205235
"\n",
206-
"1 <br>\n",
236+
"1\n",
207237
"2 * 1 \n",
208238
"3 * 2 * 1 \n",
209239
"4 * 3 * 2 * 1 \n",
210240
"5 * 4 * 3 * 2 * 1 \n",
211241
"6 * 5 * 4 * 3 * 2 * 1 \n",
212242
"\n",
213-
"720"
243+
"720\n",
244+
"```"
245+
]
246+
},
247+
{
248+
"cell_type": "markdown",
249+
"metadata": {},
250+
"source": [
251+
"You can find the steps in recursion below \n",
252+
"The '|' line indicates the same level of recursive call"
253+
]
254+
},
255+
{
256+
"cell_type": "code",
257+
"execution_count": 5,
258+
"metadata": {},
259+
"outputs": [
260+
{
261+
"name": "stdout",
262+
"output_type": "stream",
263+
"text": [
264+
"6\n",
265+
"|\t5\n",
266+
"|\t|\t4\n",
267+
"|\t|\t|\t3\n",
268+
"|\t|\t|\t|\t2\n",
269+
"|\t|\t|\t|\t|\t1\n",
270+
"|\t|\t|\t|\t2\n",
271+
"|\t|\t|\t6\n",
272+
"|\t|\t24\n",
273+
"|\t120\n",
274+
"720\n"
275+
]
276+
},
277+
{
278+
"data": {
279+
"text/plain": [
280+
"720"
281+
]
282+
},
283+
"execution_count": 5,
284+
"metadata": {},
285+
"output_type": "execute_result"
286+
}
287+
],
288+
"source": [
289+
"# The indent paramter defines the offset of output. This program is to understand recursion only.\n",
290+
"def fact(n, indent=''):\n",
291+
" print(indent, n, sep='')\n",
292+
" if 0 <= n <= 1: return 1\n",
293+
" else:\n",
294+
" fac = n * fact(n-1, indent + \"|\\t\")\n",
295+
" print(indent, fac, sep='')\n",
296+
" return fac\n",
297+
"\n",
298+
"fact(6)"
214299
]
215300
},
216301
{
@@ -459,7 +544,7 @@
459544
},
460545
{
461546
"cell_type": "code",
462-
"execution_count": 34,
547+
"execution_count": 2,
463548
"metadata": {},
464549
"outputs": [
465550
{
@@ -488,7 +573,7 @@
488573
"source": [
489574
"#Example\n",
490575
"# Example \n",
491-
"def add(a, *b): # Function Definition - * incicates variable length arguments\n",
576+
"def add(a, *b): # Function Definition - * indicates variable length arguments\n",
492577
" print(b)\n",
493578
" return a + sum(b) # returns the sum; sum is a built in function that returns sum of elements in an iterable\n",
494579
"x = int(input())\n",
@@ -1230,7 +1315,7 @@
12301315
"name": "python",
12311316
"nbconvert_exporter": "python",
12321317
"pygments_lexer": "ipython3",
1233-
"version": "3.9.4"
1318+
"version": "3.9.6"
12341319
}
12351320
},
12361321
"nbformat": 4,

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /