Learn Python The Hard Way learning (18)-naming, variables, code, functions

Source: Internet
Author: User

Is the title long? I am introducing you to functions. Every programmer has different opinions, but now I will only teach you the simplest usage.

Functions do three things:
They name the code, just like assigning a variable name to strings and numbers.
They receive parameters, just like argv in the script.
Use articles 1 and 2 to implement your own mini scripts or small methods.
If you use def to create a function, four functions are created and the relationships between them are displayed.
[Python]
# This one is like your scripts with argv
Def print_two (* args ):
Arg1, arg2 = args
Print "arg1: % r, arg2: % r" % (arg1, arg2)
 
 
# OK, that * args is actually pointless, we can just do this
Def print_two_again (arg1, arg2 ):
Print "arg1: % r, arg2: % r" % (arg1, arg2)
 
 
# This just takes one argument
Def print_one (arg1 ):
Print "arg1: % r" % arg1
 
 
# This one takes no arguments
Def print_none ():
Print "I got nothin '."
 
 
Print_two ("Zed", "Shaw ")
Print_two_again ("Zed", "Shaw ")
Print_one ("First! ")
Print_none ()

Let's analyze the first function:
First, we tell python that we need to use def to create a function.
In the same row, we give the function the name print_two. Of course, you can also use other well-understood names.
Then we define a variable * args for the function, which should be placed in.
Then end the line with the ":" sign.
After the colon is used, all the rows that use four spaces to indent will be included in the print_two function. The first line of indentation is an unwrapped statement.
To verify how this function works, we print the parameter value.
The problem is that print_two is not the simplest way to construct a function. In print_two_again, we use two parameters instead of unpacking.

The following are functions with and without parameters.

Running result
Arg1: 'zed', arg2: 'shares'
Arg1: 'zed', arg2: 'shares'
Arg1: 'First! '
I got nothin '.
Now we know how the function works. In fact, just like the exists and open methods we used earlier, we can write our own functions and then call them. Www.2cto.com

Extra score exercise
Write a function check list and write it to the card until you finish the remaining exercises, or you feel that you are not needed:
Do you use def to define your function?
Does your function name only contain characters and underscores?
Is there a (number?
Are you sure you want to use commas (,) to separate your parameters?
Is the parameter unique?
Is it used after the parameter): end?
Do the statements to be included in the function use four spaces for indentation?
Does the indent be removed when the function ends?
When you use a function, check the following:
Use a function name to call a function?
Is there a (number?
Are parameter values separated by commas?
Use) to end the call?
Author: lixiang0522

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.