Skip to content

Commit f4fe2f7

Browse files
committed
Changes during first Python class
1 parent 043a04b commit f4fe2f7

File tree

1 file changed

+146
-28
lines changed

1 file changed

+146
-28
lines changed

Diff for: notebook/Python-1.ipynb

+146-28
Original file line numberDiff line numberDiff line change
@@ -73,24 +73,21 @@
7373
},
7474
{
7575
"cell_type": "code",
76-
"execution_count": 2,
76+
"execution_count": 3,
7777
"metadata": {
7878
"collapsed": false
7979
},
8080
"outputs": [
8181
{
82-
"ename": "TypeError",
83-
"evalue": "unsupported operand type(s) for ^: 'float' and 'int'",
84-
"output_type": "error",
85-
"traceback": [
86-
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
87-
"\u001b[0;32m<ipython-input-2-2cffe887fbc6>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0;36m5\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0;36m0.6\u001b[0m \u001b[0;34m-\u001b[0m \u001b[0;36m0.5\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0;36m9.81\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0;36m0.6\u001b[0m\u001b[0;34m^\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
88-
"\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for ^: 'float' and 'int'"
82+
"name": "stdout",
83+
"output_type": "stream",
84+
"text": [
85+
"1.2342\n"
8986
]
9087
}
9188
],
9289
"source": [
93-
"print 5*0.6 - 0.5*9.81*0.6^2"
90+
"print 5*0.6 - 0.5*9.81*0.6**2"
9491
]
9592
},
9693
{
@@ -139,7 +136,7 @@
139136
},
140137
{
141138
"cell_type": "code",
142-
"execution_count": 4,
139+
"execution_count": 7,
143140
"metadata": {
144141
"collapsed": false
145142
},
@@ -160,6 +157,29 @@
160157
"print y"
161158
]
162159
},
160+
{
161+
"cell_type": "code",
162+
"execution_count": 9,
163+
"metadata": {
164+
"collapsed": false
165+
},
166+
"outputs": [
167+
{
168+
"name": "stdout",
169+
"output_type": "stream",
170+
"text": [
171+
"<type 'int'>\n",
172+
"<type 'float'>\n"
173+
]
174+
}
175+
],
176+
"source": [
177+
"a=2\n",
178+
"print type(a)\n",
179+
"a=2.5\n",
180+
"print type(a)"
181+
]
182+
},
163183
{
164184
"cell_type": "markdown",
165185
"metadata": {},
@@ -179,7 +199,7 @@
179199
},
180200
{
181201
"cell_type": "code",
182-
"execution_count": 5,
202+
"execution_count": 10,
183203
"metadata": {
184204
"collapsed": false
185205
},
@@ -250,12 +270,24 @@
250270
},
251271
{
252272
"cell_type": "code",
253-
"execution_count": null,
273+
"execution_count": 14,
254274
"metadata": {
255275
"collapsed": false
256276
},
257-
"outputs": [],
258-
"source": []
277+
"outputs": [
278+
{
279+
"name": "stdout",
280+
"output_type": "stream",
281+
"text": [
282+
"0.5\n"
283+
]
284+
}
285+
],
286+
"source": [
287+
"length = 666\n",
288+
"from __future__ import division\n",
289+
"print 1/2"
290+
]
259291
},
260292
{
261293
"cell_type": "markdown",
@@ -267,7 +299,7 @@
267299
},
268300
{
269301
"cell_type": "code",
270-
"execution_count": 7,
302+
"execution_count": 16,
271303
"metadata": {
272304
"collapsed": false
273305
},
@@ -334,12 +366,23 @@
334366
},
335367
{
336368
"cell_type": "code",
337-
"execution_count": null,
369+
"execution_count": 18,
338370
"metadata": {
339371
"collapsed": false
340372
},
341-
"outputs": [],
342-
"source": []
373+
"outputs": [
374+
{
375+
"name": "stdout",
376+
"output_type": "stream",
377+
"text": [
378+
"3.142\n"
379+
]
380+
}
381+
],
382+
"source": [
383+
"import math\n",
384+
"print \"%.3f\" % math.pi"
385+
]
343386
},
344387
{
345388
"cell_type": "markdown",
@@ -568,12 +611,70 @@
568611
},
569612
{
570613
"cell_type": "code",
571-
"execution_count": null,
614+
"execution_count": 19,
572615
"metadata": {
573616
"collapsed": false
574617
},
575-
"outputs": [],
576-
"source": []
618+
"outputs": [
619+
{
620+
"data": {
621+
"text/plain": [
622+
"['__doc__',\n",
623+
" '__file__',\n",
624+
" '__name__',\n",
625+
" '__package__',\n",
626+
" 'acos',\n",
627+
" 'acosh',\n",
628+
" 'asin',\n",
629+
" 'asinh',\n",
630+
" 'atan',\n",
631+
" 'atan2',\n",
632+
" 'atanh',\n",
633+
" 'ceil',\n",
634+
" 'copysign',\n",
635+
" 'cos',\n",
636+
" 'cosh',\n",
637+
" 'degrees',\n",
638+
" 'e',\n",
639+
" 'erf',\n",
640+
" 'erfc',\n",
641+
" 'exp',\n",
642+
" 'expm1',\n",
643+
" 'fabs',\n",
644+
" 'factorial',\n",
645+
" 'floor',\n",
646+
" 'fmod',\n",
647+
" 'frexp',\n",
648+
" 'fsum',\n",
649+
" 'gamma',\n",
650+
" 'hypot',\n",
651+
" 'isinf',\n",
652+
" 'isnan',\n",
653+
" 'ldexp',\n",
654+
" 'lgamma',\n",
655+
" 'log',\n",
656+
" 'log10',\n",
657+
" 'log1p',\n",
658+
" 'modf',\n",
659+
" 'pi',\n",
660+
" 'pow',\n",
661+
" 'radians',\n",
662+
" 'sin',\n",
663+
" 'sinh',\n",
664+
" 'sqrt',\n",
665+
" 'tan',\n",
666+
" 'tanh',\n",
667+
" 'trunc']"
668+
]
669+
},
670+
"execution_count": 19,
671+
"metadata": {},
672+
"output_type": "execute_result"
673+
}
674+
],
675+
"source": [
676+
"dir(math)"
677+
]
577678
},
578679
{
579680
"cell_type": "markdown",
@@ -582,26 +683,43 @@
582683
"## <span style=\"color:blue\">Exercise: Find errors in the coding of a formula</span>\n",
583684
"Given a quadratic equation,\n",
584685
"$$ax^2 + bx + c = 0,$$\n",
585-
"$$x1 = −b+\\frac{\\sqrt{b^2 −4ac}}{2a},$$ and\n",
586-
"$$x2 = −b−\\frac{\\sqrt{b^2 −4ac}}{2a}.$$\n",
686+
"$$x1 = \\frac{−b+\\sqrt{b^2 −4ac}}{2a},$$ and\n",
687+
"$$x2 = \\frac{−b−\\sqrt{b^2 −4ac}}{2a}.$$\n",
587688
"Why does the following program not work correctly?"
588689
]
589690
},
590691
{
591692
"cell_type": "code",
592-
"execution_count": null,
693+
"execution_count": 22,
593694
"metadata": {
594695
"collapsed": false
595696
},
596-
"outputs": [],
697+
"outputs": [
698+
{
699+
"name": "stdout",
700+
"output_type": "stream",
701+
"text": [
702+
"(-0.25+0.968245836552j) (-0.25-0.968245836552j)\n"
703+
]
704+
}
705+
],
597706
"source": [
598707
"a = 2; b = 1; c = 2\n",
599-
"from math import sqrt\n",
708+
"from cmath import sqrt\n",
600709
"q = sqrt(b*b - 4*a*c)\n",
601-
"x1 = (-b + q)/2*a\n",
602-
"x2 = (-b - q)/2*a\n",
710+
"x1 = (-b + q)/(2*a)\n",
711+
"x2 = (-b - q)/(2*a)\n",
603712
"print x1, x2"
604713
]
714+
},
715+
{
716+
"cell_type": "code",
717+
"execution_count": null,
718+
"metadata": {
719+
"collapsed": true
720+
},
721+
"outputs": [],
722+
"source": []
605723
}
606724
],
607725
"metadata": {

0 commit comments

Comments
 (0)