Stupid way to learn python exercise 18 naming, variables, code, and functions

Source: Internet
Author: User

Next we start to learn about "functions." The function can do the following 3 things.

1, they name the code snippet, just like "variable" to string and to name

2, they can accept the parameters, just like your script accepts argv.

3, using # and #, they let you create "mini script" or "small command"

First, put the code: (slightly modified with the code in the textbook)

#-*-coding:utf-8-*-
#this One is like your script with argv
def print_two (*args):
	arg1, arg2,arg3 = args
	Print "Arg1:%r,arg2:%r,arg3:%r"% (ARG1,ARG2,ARG3)
	
def print_two_again (Arg1, arg2):
	print "arg1:%r,arg2:%r"  % (ARG1,ARG2)
	
def print_several (Arg1, arg2): print ' This is the ' the ' ' is the ' the ' ' is the ' the ' ' is the ' ' is ' arg1
    Arg2:%r "%arg2
	
#this just takes on argument
def print_one (arg1):
	print" arg1:%r "%arg1
	
#this one take No argument
def print_none ():
	print "I got Nothing"
	
print_two ("Zed", "Shaw", "Handsome")
Print_ Two_again ("Zed", "Shaw")
print_one ("first!")
Print_none
Print ("Python", "interesting")
The results of the operation are as follows:

simengred$ python ex18.py
arg1: ' Zed ', arg2: ' Shaw ', Arg3: ' Handsome '
arg1: ' Zed ', arg2: ' Shaw '
arg1: ' first! '
(' Python ', ' interesting ')

The following are the considerations when creating a function:

1, the function begins with Def

2,def is followed by the function name, which is the same as the naming rule for the variable.

3, the function is followed by parentheses ()

4, the parentheses inside the function parameters, multiple parameters separated by commas, and the function name can not be repeated.

5, the parentheses need to write a colon:

6, the function below the content of a indent, the end of the function indentation end


Related Article

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.