Stupid way to learn Python (21)

Source: Internet
Author: User

Exercise 21: Functions can return something

You've learned to use = to name a variable, and to define a variable as a number or a string. And then we'll let you witness more miracles. What we're going to show you is how to use = and a new Python word return to set the variable to "the value of a function". There is one thing you need to pay attention to, but for the moment, let's write the following script:

1 defAdd (A, b):2     Print "ADDING%d +%d"%(A, b)3     returnA +b4 5 defSubtract (A, b):6     Print "subtracting%d-%d"%(A, b)7     returnAb8 9 defMultiply (A, b):Ten     Print "multiplying%d *%d"%(A, b) One     returnAb A  - defDivide (A, b): -     Print "dividing%d/%d"%(A, b) the     returnAb -  -  - Print "Let's do some math with just functions!" +  -Age = Add (30, 5) +Height = Subtract (78, 4) AWeight = Multiply (90, 2) atiq = Divide (100, 2) -  - Print "Age :%d, Height:%d, Weight:%d, IQ:%d"%(age, height, weight, IQ) -  -  - #A Puzzle for the extra credits, type it in anyway. in Print "Here is a puzzle." -  towhat = Add (age, subtract (height, multiply (weight, divide (IQ, 2)))) +  - Print "that becomes:", What,"Can Do it by hand?"
View Code

    Now we have created our own subtraction math function:  add , &NBSP;subtract ,  < Span class= "Pre" >multiply , and  divide . It is important that the last line of the function, such as &NBSP;add   The last line is &NBSP;return a +  b , which implements this function:

    1. We used two parameters when we called the function: a and b .
    2. We print out the functions of this function, here is the calculation of addition (adding)
    3. Next we tell Python to let it do a callback action: we return the value of a + b (return). Or you could say, "I'll add a and b together and return the results." ”
    4. Python adds two numbers and then, when the function is finished, it assigns the result of a + b to a variable.

As with many of the other things in this book, you need to slowly digest the content, step-by-step, and track what happened. To help you understand, this section of the plus points exercise will let you solve a puzzle, and let you learn something cool.

The results you should see

Bonus points Exercise
    1. If you're not sure about the return function, try writing a few functions yourself to get them to return some values. You can put anything that can be placed on the right side as the return value of a function.
    2. The end of the script is a puzzle. I use the return value of a function as a parameter to another function. I link them together, just like I write math equations. It may be a bit hard to read, but you'll know the result if you run it. Next, you need to try to make the same function as the expression in the normal way.
    3. Once you've solved the puzzle, try changing some parts of the function and see what happens. You can change it to a destination and let it output another value.
    4. Finally, turn it upside down and do it once. Write a simple equation and use the same function to calculate it.

This exercise may make you a little bit bigger, but it's time to take it as a game, and solving such puzzles is one of the pleasures of programming. You'll see a little puzzle like that later.

Exercise Exercises

1.

Stupid way to learn Python (21)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.