19.10.2016
Lear n Python the Har d Way - Read for Fr ee
Exercse Exercse 21: Functons Can Return Somethng You have have been usng the
=
character to name varables and set them to numbers or
strngs. We're now gong to blow your mnd agan by showng you how to use new Python word
return
=
and a
to set varables to be a value from a function. There wll be
one thng to pay close attenton to, but frst type ths n:
https://l ear npythonthehar dway.or g/book/ex21.htm l
1/6
19.10.2016
Learn Python the Hard Way - Read for Free
(a, b):
1
"ADDING %d + %d"
2
a
3
(a, b)
b
4
(a, b):
5
"SUBTRACTING %d - %d"
6
a
7
(a, b)
b
8
(a, b):
9
"MULTIPLYING %d * %d"
10
a
11
(a, b)
b
12
(a, b):
13
"DIVIDING %d / %d"
14
a
15
(a, b)
b
16 17
"Let's do some math with just functions!"
18 19 20
age
add(30, 5)
21
height
subtract(78, 4)
22
weight
multiply(90, 2)
23
iq
divide(100, 2)
24
"Age: %d, Height: %d, Weight: %d, IQ: %d"
25
(age, height, weight, iq)
26 27 28
# A puzzle for the extra credit, type it in anyway.
"Here is a puzzle."
29 30 31
what
add(age, subtract(height, multiply(weight, divide(iq, 2))))
32 33
"That becomes: ", what, "Can you do it by hand?"
https://learnpythonthehardway.org/book/ex21.html
2/6
19.10.2016
Learn Python the Hard Way - Read for Free
We are now dong our own math functons for divide
add
,
subtract
,
multiply
. The mportant thng to notce s the last lne where we say
, and
return a + b
(n
add
). What ths does s the followng:
1
Our functon s called wth two arguments:
2
We prnt out what our functon s dong, n ths case "ADDING."
3
Then we tell Python to do somethng knd of backward: we return the addton of a + b
4
. You mght say ths as, "I add
a
a
and
and
b
b
.
then return them."
Python adds the two numbers. Then when the functon ends, any lne that runs t wll be able to assgn ths
a + b
result to a varable.
As wth many other thngs n ths book, you should take ths real slow, break t down, and try to trace what's gong on. To help there are extra credt to solve a puzzle and learn somethng cool.
What You Should See $ python ex21.py Let's do some math with just functions! ADDING 30 + 5 SUBTRACTING 78 - 4 MULTIPLYING 90 * 2 DIVIDING 100 / 2 Age: 35, Height: 74, Weight: 180, IQ: 50 Here is a puzzle. DIVIDING 50 / 2 MULTIPLYING 180 * 25 SUBTRACTING 74 - 4500 ADDING 35 + -4426 That becomes:
-4391 Can you do it by hand?
Study Drlls https://learnpythonthehardway.org/book/ex21.html
3/6
19.10.2016
Learn Python the Hard Way - Read for Free
1
If you aren't really sure what
return
does, try wrtng a few of your own
functons and have them return some values. You can return anythng that you can put to the rght of an 2
=
.
At the end of the scrpt s a puzzle. I'm takng the return value of one functon and using t as the argument of another functon. I'm dong ths n a chan so that I'm
knd of creatng a formula usng the functons. It looks really werd, but f you run the scrpt you can see the results. What you should do s try to fgure out the normal formula that would recreate ths same set of operatons. 3
Once you have the formula worked out for the puzzle, get n there and see what happens when you modfy the parts of the functons. Try to change t on purpose to make another value.
4
Do the nverse. Wrte a smple formula and use the functons n the same way to calculate t.
Ths exercse mght really whack your bran out, but take t slow and easy and treat t lke a lttle game. Fgurng out puzzles lke ths s what makes programmng fun, so I'll be gvng you more lttle problems lke ths as we go.
Common Student Questons Why does Python prnt the formula or the functons "backward"?
It's not really backward, t's "nsde out." When you start breakng down the functon nto separate formulas and functon calls you'll see how t works. Try to understand what I mean by "nsde out" rather than "backward."
How can I use
to enter my own values?
Remember int(raw_input()) ? The problem wth that s then you can't enter floatng pont, so also try usng float(raw_input()) nstead.
What do you mean by "wrte out a formula"?
Try 24 + 34 / 100 - 1023 as a start. Convert that to use the functons. Now come up wth your own smlar math equaton and use varables so t's more https://learnpythonthehardway.org/book/ex21.html
4/6
19.10.2016
Learn Python the Hard Way - Read for Free
lke a formula.
Buy DRM-Free
When you buy drectly from the author, Zed A. Shaw, you'll get a professonal qualty PDF and hours of HD Vdeo, all DRM-free and yours to download and use as you see ft. $
29. 99
BUY DIRECTLY FROM THE AUTHOR (HTTPS: //PAYDIV.IO/ACCESS/BUY/2/)
OR, YOU CAN READ LEARN PYTHON THE HARD WAY FOR FREE (HTTPS://LEARNPYTHONTHEHARDWAY.ORG/BOOK/) RIGHT HERE, VIDEO LECTURES NOT INCLUDED.
Other Buyng Optons
BUY ON AMA ZON (HTTP: //BIT.LY/AMZNLPTHW)
BUY A HARD COPY FROM THE PUBLISHER (HTTP://BIT.LY/INFORMITLPTHW)
https://learnpythonthehardway.org/book/ex21.html
5/6
19.10.2016
Learn Python the Hard Way - Read for Free
BUY A HARD COPY FROM BARNES & NOBLE (HTTP://BIT.LY/BNLPTHW)
Prevous (ex20.html)
HOME (/)
Next
ABO UT (HTTPS://LEARNCODETHEHARDWAY.ORG/ABOUT/)
(ex22.html)
CONTACT (HTTPS://LEARNCODETHEHARDWAY.ORG/CONTACT/)
© 2016 ZED A. SHAW
(https://twtter.com/lzsthw)
https://learnpythonthehardway.org/book/ex21.html
6/6