Skip to content

Commit a5186f8

Browse files
committed
functions
1 parent cb1230f commit a5186f8

File tree

2 files changed

+311
-1
lines changed

2 files changed

+311
-1
lines changed

Diff for: P-E-functions.ipynb

+309
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,309 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "b3c0145c",
6+
"metadata": {},
7+
"source": [
8+
"# Python: Everything\n",
9+
"- 28) **Functions** \n",
10+
" - Definition of a function\n",
11+
" - Example: Compute factorial of an integer\n",
12+
" - Calling by-reference or by-value in function calling\n",
13+
" - Example: Compute Fibonacci numbers\n",
14+
" - Having a variable number of arguments\n",
15+
" - Having a variable number of keyword arguments\n",
16+
"<br>----------------------------------------------\n",
17+
"<br> https://www.pinterest.com/HamedShahHosseini/programming-languages/\n",
18+
"<br>https://github.com/ostad-ai/Python-Everything"
19+
]
20+
},
21+
{
22+
"cell_type": "markdown",
23+
"id": "5aa91cce",
24+
"metadata": {},
25+
"source": [
26+
"**Functions:** are pieces of codes which are only run when we call them. A function definition begins with keyword *def*. \n",
27+
"<br>A function may have zero or more arguments.\n",
28+
"<br>A function may also send values by keyword *return*.\n",
29+
"<br> The following function computes the **Factorial** of an integer."
30+
]
31+
},
32+
{
33+
"cell_type": "code",
34+
"execution_count": 55,
35+
"id": "568f3eb5",
36+
"metadata": {},
37+
"outputs": [
38+
{
39+
"name": "stdout",
40+
"output_type": "stream",
41+
"text": [
42+
"Factorial(5) is 120\n"
43+
]
44+
}
45+
],
46+
"source": [
47+
"def facto(n):\n",
48+
" result=1\n",
49+
" if n==0 or n==1:\n",
50+
" pass\n",
51+
" else:\n",
52+
" for i in range(2,n+1):\n",
53+
" result*=i\n",
54+
" return result\n",
55+
"n=5\n",
56+
"print(f'Factorial({n}) is {facto(n)}')"
57+
]
58+
},
59+
{
60+
"cell_type": "markdown",
61+
"id": "35474859",
62+
"metadata": {},
63+
"source": [
64+
"<br>**Hint:** arguments send to a function can be changed inside a function, if the arguments are mutable objects such as lists, dictionary, and sets. Therefore, they are called **by-reference**. In contrast, passing immutable objects as arguments to a functions, we pass them **by value**; so any change inside the function will have no effect outside the function.\n",
65+
"<br>**Mutable:** an object is called mutable if its value can be modified or changed after it is created. Examples are: lists, sets, and dictionaries.\n",
66+
"<br>**Immutable:** is an object that once it is created, it cannot be modified. Examples are numbers, strings, and tuples."
67+
]
68+
},
69+
{
70+
"cell_type": "markdown",
71+
"id": "b1ade53a",
72+
"metadata": {},
73+
"source": [
74+
"Example below shows a function definiton that gets a list and changes it inside"
75+
]
76+
},
77+
{
78+
"cell_type": "code",
79+
"execution_count": 4,
80+
"id": "c819d4a6",
81+
"metadata": {},
82+
"outputs": [
83+
{
84+
"name": "stdout",
85+
"output_type": "stream",
86+
"text": [
87+
"List before calling by reference: ['Hello']\n",
88+
"List after calling by reference: ['Hello', 'World']\n"
89+
]
90+
}
91+
],
92+
"source": [
93+
"def change_list(my):\n",
94+
" my.append('World')\n",
95+
"\n",
96+
"mylist=['Hello']\n",
97+
"print(f'List before calling by reference: {mylist}')\n",
98+
"change_list(mylist)\n",
99+
"print(f'List after calling by reference: {mylist}')"
100+
]
101+
},
102+
{
103+
"cell_type": "markdown",
104+
"id": "9746c031",
105+
"metadata": {},
106+
"source": [
107+
"Example here shows a fucntion that gets a number, changes it isnide fucntion, but it has n oeffect on the number outside the function"
108+
]
109+
},
110+
{
111+
"cell_type": "code",
112+
"execution_count": 6,
113+
"id": "94ba4365",
114+
"metadata": {},
115+
"outputs": [
116+
{
117+
"name": "stdout",
118+
"output_type": "stream",
119+
"text": [
120+
"Number before calling function: 25\n",
121+
"Number inside function: 26\n",
122+
"Number after calling function: 25\n"
123+
]
124+
}
125+
],
126+
"source": [
127+
"def change_number(number):\n",
128+
" number+=1;\n",
129+
" print(f'Number inside function: {number}')\n",
130+
"number=25\n",
131+
"print(f'Number before calling function: {number}')\n",
132+
"change_number(number)\n",
133+
"print(f'Number after calling function: {number}')"
134+
]
135+
},
136+
{
137+
"cell_type": "markdown",
138+
"id": "80294988",
139+
"metadata": {},
140+
"source": [
141+
"Let's define a function to compute numbers in Fibonacci series. We can have **default values** for arguments in the definition of function, as shown below.\n",
142+
"<br> $F_0=0, F_1=1,$\n",
143+
"<br>and $F_n=F_{n-1}+F_{n-2}$ for $n>1$"
144+
]
145+
},
146+
{
147+
"cell_type": "code",
148+
"execution_count": 58,
149+
"id": "0ca133a8",
150+
"metadata": {},
151+
"outputs": [
152+
{
153+
"name": "stdout",
154+
"output_type": "stream",
155+
"text": [
156+
"Fibonacci numbers: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]\n",
157+
"Fibonacci numbers: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]\n",
158+
"Fibonacci numbers: [0, 1, 1, 2, 3, 5, 8, 13]\n"
159+
]
160+
}
161+
],
162+
"source": [
163+
"def fibo(n=10):\n",
164+
" result=[]\n",
165+
" f0,f1=0,1\n",
166+
" while len(result)<n:\n",
167+
" result.append(f0)\n",
168+
" f0,f1=f1,f0+f1\n",
169+
" return result\n",
170+
"# calling function without argument means that \n",
171+
"# the default value for argument is used\n",
172+
"print(f'Fibonacci numbers: {fibo()}')\n",
173+
"# setting values for arguments in function call\n",
174+
"print(f'Fibonacci numbers: {fibo(12)}')\n",
175+
"# setting values for arguments via kwarg=value\n",
176+
"print(f'Fibonacci numbers: {fibo(n=8)}')"
177+
]
178+
},
179+
{
180+
"cell_type": "markdown",
181+
"id": "b6165847",
182+
"metadata": {},
183+
"source": [
184+
"A function may have a variable number of arguments, by putting a * before the argument name. Example below multiplies a variable number of arguments."
185+
]
186+
},
187+
{
188+
"cell_type": "code",
189+
"execution_count": 21,
190+
"id": "b2c7aee2",
191+
"metadata": {},
192+
"outputs": [
193+
{
194+
"name": "stdout",
195+
"output_type": "stream",
196+
"text": [
197+
"Three numbers to multiply: 6\n",
198+
"Three numbers to multiply: 56\n"
199+
]
200+
}
201+
],
202+
"source": [
203+
"def mult(*args):\n",
204+
" result=1\n",
205+
" for arg in args:\n",
206+
" result*=arg\n",
207+
" return result\n",
208+
"print(f'Three numbers to multiply: {mult(2,3)}')\n",
209+
"print(f'Three numbers to multiply: {mult(7,2,4)}')"
210+
]
211+
},
212+
{
213+
"cell_type": "markdown",
214+
"id": "f1a64f12",
215+
"metadata": {},
216+
"source": [
217+
"Another example for a variable number of arguments"
218+
]
219+
},
220+
{
221+
"cell_type": "code",
222+
"execution_count": 57,
223+
"id": "22275727",
224+
"metadata": {},
225+
"outputs": [
226+
{
227+
"name": "stdout",
228+
"output_type": "stream",
229+
"text": [
230+
"Hello.World\n",
231+
"See.You.Bye\n"
232+
]
233+
}
234+
],
235+
"source": [
236+
"def concat(*args,separator='.'):\n",
237+
" return separator.join(args)\n",
238+
"print(concat('Hello','World'))\n",
239+
"print(concat('See','You','Bye'))"
240+
]
241+
},
242+
{
243+
"cell_type": "markdown",
244+
"id": "5353c8ff",
245+
"metadata": {},
246+
"source": [
247+
"We can also have a variable number of keyword arguments by putting a ** before name of the argument. "
248+
]
249+
},
250+
{
251+
"cell_type": "code",
252+
"execution_count": 40,
253+
"id": "f956e098",
254+
"metadata": {},
255+
"outputs": [
256+
{
257+
"name": "stdout",
258+
"output_type": "stream",
259+
"text": [
260+
"Value of sine(0) is 0.0\n",
261+
"Value of sine(1) is 0.8414709848078965\n",
262+
"Value of sine(2) is 0.9092974268256817\n",
263+
"Value of cosine(0) is 1.0\n",
264+
"Value of cosine(1) is 0.5403023058681397\n",
265+
"Value of cosine(2) is -0.4161468365471424\n"
266+
]
267+
}
268+
],
269+
"source": [
270+
"from math import sin,cos\n",
271+
"def operation(*args,**kwargs):\n",
272+
" for key,value in kwargs.items():\n",
273+
" for arg in args:\n",
274+
" print(f'Value of {key}({arg}) is {value(arg)}')\n",
275+
"\n",
276+
"operation(0,1,2,sine=sin,cosine=cos)"
277+
]
278+
},
279+
{
280+
"cell_type": "code",
281+
"execution_count": null,
282+
"id": "1004dd57",
283+
"metadata": {},
284+
"outputs": [],
285+
"source": []
286+
}
287+
],
288+
"metadata": {
289+
"kernelspec": {
290+
"display_name": "Python 3 (ipykernel)",
291+
"language": "python",
292+
"name": "python3"
293+
},
294+
"language_info": {
295+
"codemirror_mode": {
296+
"name": "ipython",
297+
"version": 3
298+
},
299+
"file_extension": ".py",
300+
"mimetype": "text/x-python",
301+
"name": "python",
302+
"nbconvert_exporter": "python",
303+
"pygments_lexer": "ipython3",
304+
"version": "3.8.10"
305+
}
306+
},
307+
"nbformat": 4,
308+
"nbformat_minor": 5
309+
}

Diff for: README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@
2626
24) Magic methods, __getitem__ and __setitem__: We review getitem and setitem methods that may be used for **indexing** and **assignment** on instances of a class, respectively.
2727
25) Iterators and iterables: We use iterators to iterate over iterables. This section reviews both of them. Lists, sets, tuples, and dictionaries are examples of iterables.
2828
26) Callable objects: We can make a class callable by defining a method in the class named **--class--**. This way, any instance of the class can be called like a function.
29-
27) List comprehension: We may use list comprehension to create lists from another lists or any iterables in a concise way. A list comprehension may include for-loops and if-clauses.
29+
27) List comprehension: We may use list comprehension to create lists from another lists or any iterables in a concise way. A list comprehension may include for-loops and if-clauses.
30+
28) Functions: We will have a closer look at functions. Calling functions by-reference or by-value are mentioned. Functions may have a variable number of arguments. Also, functions may have a variable number of keyword arguments. Some example are provided for clarification.

0 commit comments

Comments
 (0)