Re-reading the LPTHW-Lesson18-21 function, lpthw. web

Source: Internet
Author: User

Re-reading the LPTHW-Lesson18-21 function, lpthw. web

1. def defines a function and selects an appropriate function name. The principle is that it is easy to understand and read. The function name format is the same as the variable name format. It starts with a letter and can contain letters, numbers, and underscores. After the function is named, put the parameters in (), and there can be no parameters. Then: end the function name and start the main part of the function. Four spaces are indented at the beginning of the body part.

#-*-Coding: UTF-8-*-def print_input (user_input): user_input = raw_input ("Enter the content to print ". decode ('utf-8 '). encode ('gbk') print user_input

2. You can specify the default values of some parameters when defining a function:

def exponential(bottom,exponent = 2):    value = bottom ** exponent    print "%d ** %d = %d" % (bottom,exponent,value)
exponential(20)
exponential(20,3)

Output:

PS: only the parameter at the end can define the default parameter value. Def func (a, B = 5) is valid, while def func (a = 5, B) is invalid.

3. Use global to declare global variables. Note the differences between global variables and local variables

(1) Example of local variables:

def func(x):    print "x is ",x    x = 2    print "Change local x to",xx = 50
func(x)print "x is still",x

Output:

(2) Declare global variables:

def func():    global x        print "x is",x    x = 2     print "Change local x to",xx = 50func()print "Now value of x is",x

Output:

4. When you call a function and assign values to parameters, you can use the key parameter method. That is, the called function has multiple parameters. If you only want to specify a part of the parameters, you can assign values to these parameters by name. The advantages include: ① do not worry about the order of parameter values ② If other parameters have default values, you can only assign values to some parameters.

Eg:

Def func (a, B = 5, c = 10): print "a is", a, "and B is", B, "and c is", cfunc (23, c = 34) #23 assign a value to a, B use the default value, c assign a value to 34 func (c = 2, a = 1) # c assign a value to 2, a assign a value to 1, B default value 5 func () # a value 12, B value 23, c default value 10

Output:

 

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.