Learn Python The Hard Way - Read For Free
Learn Python The Hard Way - Read For Free
2016
LearnPythontheHardWayReadforFree
https://learnpythonthehardway.org/book/ex21.html
1/6
19.10.2016
LearnPythontheHardWayReadforFree
return a + b
4
5
return a - b
8
9
10
11
return a * b
12
13
14
15
return a / b
16
17
18
19
20
age = add(30, 5)
21
height = subtract(78, 4)
22
weight = multiply(90, 2)
23
iq = divide(100, 2)
24
25
print "Age: %d, Height: %d, Weight: %d, IQ: %d" % (age, height, weight, iq)
26
27
28
29
30
31
32
33
https://learnpythonthehardway.org/book/ex21.html
2/6
19.10.2016
LearnPythontheHardWayReadforFree
We are now do ng our own math funct ons for add , subtract , multiply , and
divide . The mportant th ng to not ce s the last l ne where we say return a + b ( n
add ). What th s does s the follow ng:
1
Python adds the two numbers. Then when the funct on ends, any l ne that runs t
w ll be able to ass gn th s a + b result to a var able.
and
As w th many other th ngs n th s book, you should take th s real slow, break t down,
and try to trace what's go ng on. To help there are extra cred t to solve a puzzle and
learn someth ng cool.
Study Dr lls
https://learnpythonthehardway.org/book/ex21.html
3/6
19.10.2016
LearnPythontheHardWayReadforFree
If you aren't really sure what return does, try wr t ng a few of your own
funct ons and have them return some values. You can return anyth ng that you
can put to the r ght of an = .
At the end of the scr pt s a puzzle. I'm tak ng the return value of one funct on and
using t as the argument of another funct on. I'm do ng th s n a cha n so that I'm
k nd of creat ng a formula us ng the funct ons. It looks really we rd, but f you run
the scr pt you can see the results. What you should do s try to f gure out the
normal formula that would recreate th s same set of operat ons.
Once you have the formula worked out for the puzzle, get n there and see what
happens when you mod fy the parts of the funct ons. Try to change t on purpose
to make another value.
Do the nverse. Wr te a s mple formula and use the funct ons n the same way to
calculate t.
Th s exerc se m ght really whack your bra n out, but take t slow and easy and treat t
l ke a l ttle game. F gur ng out puzzles l ke th s s what makes programm ng fun, so I'll
be g v ng you more l ttle problems l ke th s as we go.
raw_input()
4/6
19.10.2016
LearnPythontheHardWayReadforFree
l ke a formula.
Buy DRM-Free
When you buy d rectly from the author, Zed A. Shaw, you'll get a profess onal
qual ty PDF and hours of HD V deo, all DRM-free and yours to download and
use as you see f t.
$
29. 99
OR, YOU CAN READ LEARN PYTHON THE HARD WAY FOR FREE
(HTTPS://LEARNPYTHONTHEHARDWAY.ORG/BOOK/) RIGHT HERE, VIDEO LECTURES NOT INCLUDED.
https://learnpythonthehardway.org/book/ex21.html
5/6
19.10.2016
LearnPythontheHardWayReadforFree
HOME
(/)
Next
ABOUT
(HTTPS://LEARNCODETHEHARDWAY.ORG/ABOUT/)
r (ex22.html)
CONTACT
(HTTPS://LEARNCODETHEHARDWAY.ORG/CONTACT/)
(https://tw tter.com/lzsthw)
https://learnpythonthehardway.org/book/ex21.html
6/6