python-2 function

Source: Internet
Author: User

Http://docs.python.org/3/library/functions.html or rookie Chinese information

1-Using functions

ABS ( -20)# Find the absolute # to find the maximum value # turn to intfloat ("12.34 " # turn into float # turn into a string # turn into BOOL type bool (' '

2-Custom functions

def my_abs (x):     if  not isinstance (x, (int, float)):         return 222    if x >= 0:        return  x    Else :         return -X

X,y= (111,222); The x value is the 111,y value is 222. function can return a tuple function directly

3-Parameters of the function

3.1 Default parameters, define default parameters Remember one thing: the default parameter must point to the immutable object!

defSUM (x, n=2):     returnx+Nsum (5)#equivalent to calling Power (5, 2):defEnroll (name, Gender, age=6, city='Beijing'):    Print('City :', City) Enroll ('Adam','M', city='Tianjin')#you can pass only the specified parameters

3.2 Variable Parameters

def # * indicates variable    sum = 0    for in  numbers:        = sum + N * n    return Sumcalc (# parameter called nums=[1,2,3]calc (# the second way 

3.3 Keyword Parameters

defPerson (name, age, * *kw):Print('Name:', Name,'Age :', Age,'Other :', kw) extra= {' City':'Beijing','Job':'Engineer'}person ('Jack', **extra)

3.4 Named keyword Parameters

defPerson (name, age, *args, city='Beijing', Job):Print(name, age, args, city, job) person ('Xiaofeng', 12,city='Shenzhin', job='Myjob') Extra= {' City':'Beijing','Job':'Engineer'}person ('Xiaofeng', 12,**extra)

3.5 parameter Combinations

defF1 (A, B, c=0, *args, * *kw):Print('A ='A'B ='B'C ='C'args =', args,'kw =', kw) args= (1, 2, 3, 4) kw= {'D': 99,'x':'#'}f1 (*args, **kw)#A = 1 b = 2 c = 3 args = (4,) kw = {' d ': ' X ': ' # '}

python-2 function

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.