"Stupid way to learn Python3" series of Practice plans--18. Naming, variables, code, functions

Source: Internet
Author: User
Tags define function function definition unpack
Topic

From the title look, finally a bit into the feeling, is not it. Next Zed is going to teach us the simplest way to use a function. New Knowledge

A function is a piece of code that can be run repeatedly, using functions to improve the reuse of a piece of code, reduce the amount of code, and improve efficiency.
Python defines a function in a particular format, always beginning with a def, followed by a space with the name of the function, followed by a pair of parentheses and a colon, if the function has parameters to be written in parentheses, multiple functions separated by commas. Then, on the other line, and the Def start position is 4 spaces (indented 4 spaces) to begin writing functions, canceling 4 spaces indicates the end of the function definition.

For example, a function that prints any number plus one may look like this:

# define function
def plus_one (number):
    new_number = number + 1
    print (new_number)

# Use function
Plus_one (5)

# The result will be
6.

Zed wrote in "Stupid 2":

Functions can do three things: they name code fragments, just like "variables" to strings and numbers. They can accept parameters just like your script accepts argv. By using #1 and #2, they allow you to create "miniature scripts" or "Small Commands" (well, I've been thinking about this for a long time and finally figured out, "by using the first and second they allow you to create ' miniature scripts ' or ' small Commands '") bonus drills .

Write a "function note" for yourself for subsequent reference. Note the following: The function definition begins with def. is the function name made up of characters and underscores _. Whether or not the function name is followed by the parentheses (the parentheses contain the parameters.) Multiple arguments are separated by commas. The name of the parameter has duplicates. (Cannot use duplicate parameter names) immediately followed by parentheses and colons of parameter names:. The code that follows the function definition uses an indentation of 4 spaces. Whether the indent is canceled at the end of the function.

When you run a function, remember to check the following: whether the function name is used when calling the function. Whether the function name is followed by (. The parentheses have no parameters. Multiple arguments are separated by commas. Whether the function ends with the.

Follow these two tables to check your practice until you don't need to check. Finally, read the following words several times:
"' Run function ', ' Call function ', ' and ' use function ' is the same meaning '



my answer. 18.0 Basic Exercises

# This is the same as our script using argv
def print_two (*args):
    arg1, arg2 = args
    print ("Arg1:%r, arg2:%r"% (arg1, arg2))

# Real The argument *args is meaningless, and we can actually write
def print_two_again (Arg1, arg2):
    print ("Arg1:%r, arg2:%r"% (arg1, arg2))

# This is a parameter
def print_one (arg1):
    print ("arg1:%r"% arg1)

# This is a def with no parameters
Print_none ():
    print ("I got Nothin '. ')

# The following is a demo of the Calling function,
# They don't print anything without using the function.
print_two ("Zed", "Shaw")
Print_two_again ("Zed", "Shaw")
Print_one ("first!"
) Print_none ()

There's nothing wrong with running, and then the program dismembered the program time

Zed disassembled the code with Print_two as an example: first we tell Python to create a function, and the command we use is def, meaning "definition (define)." Next to Def is the function name. In this case, the name is Print_two, but the name can be arbitrarily taken, even if it is "peanuts" does not matter. But it's best to be able to embody functions. Then we tell the function that we need *args (asterisk args), which is very similar to the argv of the script, and the arguments must be in parentheses () to work properly. Then use a colon: End the line, and then start the next indent. Below the colon, the line indented with 4 spaces is the content of the function that belongs to Print_two. The first line of action is to unpack the parameters, which is similar to the principle of the script parameter unpack. To demonstrate its rationale, we print out each parameter after the unpack, which is similar to what we did in the previous scripting exercise.

The problem with function print_two is that Che Bao is not necessary here, and there is a more straightforward way to write variables directly in parentheses. This is Print_two_again practice .

The point of the bonus exercise is a little special, it is the answer, has been more comprehensive



back to Table of contents

"Stupid way to learn Python3" series of Practice plan--Directory

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.