1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
{
"metadata": {
"name": "",
"signature": "sha256:6797b9444a439c70039a1e66c231ad4d494ff12505798711ee0fd43c452323b7"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Hour 15: Working with Functions"
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 15.1, Page No 245"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def function_1(x,y):\n",
" print \"Within Function_1.\"\n",
" return (x+y)\n",
"def function_2(x,y):\n",
" print \"Within Function_2.\"\n",
" return (x+y)\n",
"\n",
"x1=80\n",
"y1=10\n",
"x2=float\n",
"y2=float\n",
"x2=100.123456\n",
"y2=10.123456\n",
"print \"Pass function_1 %d\" % x1,\n",
"print \" and %d\" % y1\n",
"print \"Function_1 returns %d\" % function_1(x1,y1)\n",
"print \"Pass function_2 %f\" % x2,\n",
"print \" and %f\" % y2\n",
"print \"Function_1 returns %f\" % function_2(x2,y2)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Pass function_1 80 and 10\n",
"Within Function_1.\n",
"Function_1 returns 90\n",
"Pass function_2 100.123456 and 10.123456\n",
"Within Function_2.\n",
"Function_1 returns 110.246912\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 15.2, Page No 248"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import time\n",
"def GetDateTime():\n",
" print \"Within GetDateTime().\"\n",
" print \"Current date and time is %s\" % str(time.strftime(\"%a %b %d %X %Y\"))\n",
"\n",
"print \"Before the GetDateTime() function is called.\"\n",
"GetDateTime()\n",
"print \"After The GetDateTime() function is called.\""
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Before the GetDateTime() function is called.\n",
"Within GetDateTime().\n",
"Current date and time is Mon Nov 10 11:08:33 2014\n",
"After The GetDateTime() function is called.\n"
]
}
],
"prompt_number": 23
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Example 15.3, Page No 253"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def AddDouble(*args):\n",
" result=float\n",
" result=0.0\n",
" print \"The number of arguments is \"+ str(len(args))\n",
" for i in range(len(args)):\n",
" result= result + args[i]\n",
" return result\n",
"\n",
"d1=float\n",
"d2=float\n",
"d3=float\n",
"d4=float\n",
"d1=1.5\n",
"d2=2.5\n",
"d3=3.5\n",
"d4=4.5\n",
"\n",
"print \"Given an argument: %2.1f\" % d1\n",
"print \"The result returned by AddDouble() is: %2.1f \\n\" % AddDouble(d1)\n",
"\n",
"print \"Given an argument: %2.1f\" % d1,\n",
"print \" and %2.1f\" % d2\n",
"print \"The result returned by AddDouble() is: %2.1f \\n\" % AddDouble(d1,d2)\n",
"\n",
"print \"Given an argument: %2.1f\" % d1,\n",
"print \", %2.1f\" % d2,\n",
"print \" and %2.1f\" % d3\n",
"print \"The result returned by AddDouble() is: %2.1f \\n\" % AddDouble(d1,d2,d3)\n",
"\n",
"print \"Given an argument: %2.1f\" % d1,\n",
"print \", %2.1f\" % d2,\n",
"print \", %2.1f\" % d3,\n",
"print \" and %2.1f\" % d4\n",
"print \"The result returned by AddDouble() is: %2.1f \\n\" % AddDouble(d1,d2,d3,d4)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Given an argument: 1.5\n",
"The number of arguments is 1\n",
"The result returned by AddDouble() is: 1.5 \n",
"\n",
"Given an argument: 1.5 and 2.5\n",
"The number of arguments is 2\n",
"The result returned by AddDouble() is: 4.0 \n",
"\n",
"Given an argument: 1.5 , 2.5 and 3.5\n",
"The number of arguments is 3\n",
"The result returned by AddDouble() is: 7.5 \n",
"\n",
"Given an argument: 1.5 , 2.5 , 3.5 and 4.5\n",
"The number of arguments is 4\n",
"The result returned by AddDouble() is: 12.0 \n",
"\n"
]
}
],
"prompt_number": 6
}
],
"metadata": {}
}
]
}
|