Skip to content

Commit 30c37e1

Browse files
committed
New topic
Function notebook
1 parent 0b8f149 commit 30c37e1

File tree

2 files changed

+316
-0
lines changed

2 files changed

+316
-0
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## Functions in Python:\n",
8+
"Function is a set of organized lines of code that performs a specific, well defined task.\n",
9+
"\n",
10+
"It is used reduce the number of lines of code and improve reusability."
11+
]
12+
},
13+
{
14+
"cell_type": "markdown",
15+
"metadata": {},
16+
"source": [
17+
"Syntax: \n",
18+
"*def* function_name(parameters): => function definition \n",
19+
"     Statements \n",
20+
"      *return* value(s)\n",
21+
"\n",
22+
"... \n",
23+
"... \n",
24+
"function_name(parameters) => Caller"
25+
]
26+
},
27+
{
28+
"cell_type": "markdown",
29+
"metadata": {},
30+
"source": [
31+
"#### Return statement:(*return*)\n",
32+
"Returns back a value as specified. It marks the end of a function\n"
33+
]
34+
},
35+
{
36+
"cell_type": "markdown",
37+
"metadata": {},
38+
"source": [
39+
"## Fruitful function\n",
40+
"A fruitful function is a function that returns a value"
41+
]
42+
},
43+
{
44+
"cell_type": "code",
45+
"execution_count": 4,
46+
"metadata": {},
47+
"outputs": [
48+
{
49+
"name": "stdin",
50+
"output_type": "stream",
51+
"text": [
52+
" 12\n",
53+
" 13\n"
54+
]
55+
},
56+
{
57+
"name": "stdout",
58+
"output_type": "stream",
59+
"text": [
60+
"25\n"
61+
]
62+
}
63+
],
64+
"source": [
65+
"# Example 1:\n",
66+
"def add(a, b): # Function Definition\n",
67+
" return a+b # returns the sum\n",
68+
"x = int(input())\n",
69+
"y = int(input())\n",
70+
"print(add(x, y)) # Function Calling"
71+
]
72+
},
73+
{
74+
"cell_type": "markdown",
75+
"metadata": {},
76+
"source": [
77+
"## Lambda function (Inline or Unnamed function):\n",
78+
"One liner of a function\n",
79+
"\n",
80+
"Syntax: \n",
81+
"*lambda* parameters: Statement\n"
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": 6,
87+
"metadata": {},
88+
"outputs": [
89+
{
90+
"name": "stdin",
91+
"output_type": "stream",
92+
"text": [
93+
" 10\n"
94+
]
95+
},
96+
{
97+
"name": "stdout",
98+
"output_type": "stream",
99+
"text": [
100+
"True\n"
101+
]
102+
}
103+
],
104+
"source": [
105+
"# Example 1\n",
106+
"# def c(x):\n",
107+
"# return x % 2 == 0 #One line function\n",
108+
"a = int(input())\n",
109+
"c = lambda x: x % 2 == 0 # Equivalent lambda function\n",
110+
"print(c(a))"
111+
]
112+
},
113+
{
114+
"cell_type": "markdown",
115+
"metadata": {},
116+
"source": [
117+
"## Recursion:\n",
118+
"Recursion is a method of solving a problem where the solution depends on solutions to smaller instances of the same problem.\n",
119+
"\n",
120+
"## Recursive function:\n",
121+
"A function that performs recursion.\n",
122+
"\n",
123+
"They call themselves repeatedly by dividing the problem into sub problems.\n",
124+
"\n",
125+
">*def* function(paramters1): \n",
126+
"    function(parameters2)"
127+
]
128+
},
129+
{
130+
"cell_type": "code",
131+
"execution_count": null,
132+
"metadata": {},
133+
"outputs": [],
134+
"source": []
135+
}
136+
],
137+
"metadata": {
138+
"kernelspec": {
139+
"display_name": "Python 3",
140+
"language": "python",
141+
"name": "python3"
142+
},
143+
"language_info": {
144+
"codemirror_mode": {
145+
"name": "ipython",
146+
"version": 3
147+
},
148+
"file_extension": ".py",
149+
"mimetype": "text/x-python",
150+
"name": "python",
151+
"nbconvert_exporter": "python",
152+
"pygments_lexer": "ipython3",
153+
"version": "3.8.6rc1"
154+
}
155+
},
156+
"nbformat": 4,
157+
"nbformat_minor": 4
158+
}

Functions.ipynb

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"## Functions in Python:\n",
8+
"Function is a set of organized lines of code that performs a specific, well defined task.\n",
9+
"\n",
10+
"It is used reduce the number of lines of code and improve reusability."
11+
]
12+
},
13+
{
14+
"cell_type": "markdown",
15+
"metadata": {},
16+
"source": [
17+
"Syntax: \n",
18+
"*def* function_name(parameters): => function definition \n",
19+
"     Statements \n",
20+
"      *return* value(s)\n",
21+
"\n",
22+
"... \n",
23+
"... \n",
24+
"function_name(parameters) => Caller"
25+
]
26+
},
27+
{
28+
"cell_type": "markdown",
29+
"metadata": {},
30+
"source": [
31+
"#### Return statement:(*return*)\n",
32+
"Returns back a value as specified. It marks the end of a function\n"
33+
]
34+
},
35+
{
36+
"cell_type": "markdown",
37+
"metadata": {},
38+
"source": [
39+
"## Fruitful function\n",
40+
"A fruitful function is a function that returns a value"
41+
]
42+
},
43+
{
44+
"cell_type": "code",
45+
"execution_count": 4,
46+
"metadata": {},
47+
"outputs": [
48+
{
49+
"name": "stdin",
50+
"output_type": "stream",
51+
"text": [
52+
" 12\n",
53+
" 13\n"
54+
]
55+
},
56+
{
57+
"name": "stdout",
58+
"output_type": "stream",
59+
"text": [
60+
"25\n"
61+
]
62+
}
63+
],
64+
"source": [
65+
"# Example 1:\n",
66+
"def add(a, b): # Function Definition\n",
67+
" return a+b # returns the sum\n",
68+
"x = int(input())\n",
69+
"y = int(input())\n",
70+
"print(add(x, y)) # Function Calling"
71+
]
72+
},
73+
{
74+
"cell_type": "markdown",
75+
"metadata": {},
76+
"source": [
77+
"## Lambda function (Inline or Unnamed function):\n",
78+
"One liner of a function\n",
79+
"\n",
80+
"Syntax: \n",
81+
"*lambda* parameters: Statement\n"
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": 6,
87+
"metadata": {},
88+
"outputs": [
89+
{
90+
"name": "stdin",
91+
"output_type": "stream",
92+
"text": [
93+
" 10\n"
94+
]
95+
},
96+
{
97+
"name": "stdout",
98+
"output_type": "stream",
99+
"text": [
100+
"True\n"
101+
]
102+
}
103+
],
104+
"source": [
105+
"# Example 1\n",
106+
"# def c(x):\n",
107+
"# return x % 2 == 0 #One line function\n",
108+
"a = int(input())\n",
109+
"c = lambda x: x % 2 == 0 # Equivalent lambda function\n",
110+
"print(c(a))"
111+
]
112+
},
113+
{
114+
"cell_type": "markdown",
115+
"metadata": {},
116+
"source": [
117+
"## Recursion:\n",
118+
"Recursion is a method of solving a problem where the solution depends on solutions to smaller instances of the same problem.\n",
119+
"\n",
120+
"## Recursive function:\n",
121+
"A function that performs recursion.\n",
122+
"\n",
123+
"They call themselves repeatedly by dividing the problem into sub problems.\n",
124+
"\n",
125+
">*def* function(paramters1): \n",
126+
"    function(parameters2)"
127+
]
128+
},
129+
{
130+
"cell_type": "code",
131+
"execution_count": null,
132+
"metadata": {},
133+
"outputs": [],
134+
"source": []
135+
}
136+
],
137+
"metadata": {
138+
"kernelspec": {
139+
"display_name": "Python 3",
140+
"language": "python",
141+
"name": "python3"
142+
},
143+
"language_info": {
144+
"codemirror_mode": {
145+
"name": "ipython",
146+
"version": 3
147+
},
148+
"file_extension": ".py",
149+
"mimetype": "text/x-python",
150+
"name": "python",
151+
"nbconvert_exporter": "python",
152+
"pygments_lexer": "ipython3",
153+
"version": "3.8.6rc1"
154+
}
155+
},
156+
"nbformat": 4,
157+
"nbformat_minor": 4
158+
}

0 commit comments

Comments
 (0)