Python function day-3

Source: Internet
Author: User

def(*args):Print(Args,type (args)) Li= [11,22,33,44]f1 (LI)#use Li as *args to output a tuple with only one LI elementF1 (*li)#add * Rather li element traversal and join to tuple#Print Results([11,22,33,44])(11,22,33,44)def(**Keargs):Print(Kwargs,type (Kwargs)) DiC= {'K1': 123,'K2': 345}#F1 (DIC) is wrongF1 (k1=dic) F1 (**dic)#How to pass a dictionary#Print Results{'K1': {'K1': 123,'K2': 345}} <class 'Dict'>{'K1': 123,'K2': 345} <class 'Dict'>

First, the syntax:

def function name ():    function Body     return value

Α.return returns a value that, if no return value, returns none by default

B. As soon as the return is executed in the function, the code below the function is not executing

Second, the parameters

A. Formal parameters

B. Actual parameters

C. Default parameters: Default parameters must be put in the tail, or will be an error

D. Dynamic Parameters

def F (*args): the type of print(args)       #A is a tuple, only the parameters that can be passed in are tuple elements  def F1 (**kwargs):   # is also a dynamic parameter, but must pass the value of the dictionary type, key=values the form of the value of print(Kwargs)  

E. Universal parameters: Def (P,*args,**kwargs): #表示上述的综合. *a returns a tuple, which is also a tuple element, **a returns DICT, and the dict element is passed in.

F. Incoming list, tuple, tuple for dynamic parameters

def(*args):Print(Args,type (args)) Li= [11,22,33,44]f1 (LI)#use Li as *args to output a tuple with only one LI elementF1 (*li)#add * Rather li element traversal and join to tuple

#Print Results([11,22,33,44])(11,22,33,44)-----------------------------Split Line---------------------------def(**Keargs):Print(Kwargs,type (Kwargs)) DiC= {'K1': 123,'K2': 345}#F1 (DIC) is wrongF1 (k1=dic) The value of #创建了一个只有一个key (K1) is the dictionary of DIC F1 (**dic)#How to pass a dictionary#Print Results{'K1': {'K1': 123,'K2': 345}} <class 'Dict'>{'K1': 123,'K2': 345} <class 'Dict'>

G. Global variables

# DECLARE global variables in uppercase
P = 135 def fun1 (): = 123 # Local, can only call global # in the function body Global variables, all functions can be shared. You can use global p to manipulate p in a function. p= 246 print(a,p)def fun2 (): # Locally, only in function weight call print(a,p) #这个P值变成了246 Note that the value of the global variable P has been modified successfully FUN1 () fun2 ()


#打印结果

123 246
456 246

Python function day-3

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.