Python basics-functions and python Basics

Source: Internet
Author: User

Python basics-functions and python Basics

A function is a function block that can be called repeatedly.

I. Definition

Def function name (parameter):... function body... return value

Requirements:

  • Def: function keyword
  • Function Name: the name of the function used for future calls.
  • Function body: code segment for running some operations
  • Returned value: whether the function is successfully run or not, or other results must be returned to the caller. By default, None is returned.
  • Parameters:

Common Parameters

Default parameters

Dynamic Parameters

####### Define a function ####### name is a formal parameter of a function, or def func (name) for short ): print name ####### execute the function ####### "Callback" is the actual parameter of the function func, referred to as the real parameter func ("Callback ")
Common Parameters
Def func (name, age = 20): pring "% s: % s" % (name, age) # specify the parameter func ("Qiutujs", 30) # use the default parameter func ("Qiutujs"). Note: The default parameter must be placed at the end of the parameter list.
Default parameters
Def func (* args): print (args) # execution method 1 func (11,22, 33,44) # execution method 2 li = [11,22, 33,44] func (* li) # If no * is added, the function func prints li as an element.
Dynamic Parameters
Def func (** kwargs): print (kwargs) # execution method 1 func (name = "success", age = 33) # execution method 2 li = {"name ": "dimensions", "age": 33, "job": "IT"} func (** li) Note: This dynamic parameter converts the input parameter to the dictionary type, therefore, when you enter a parameter, you must enter it as a key-value pair.
Dynamic Parameters
def func(*args,**kwargs):    print (args)    pring (kwargs)
Dynamic Parameters (Universal parameters)

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.