Python function Learning Notes

Source: Internet
Author: User
#!/usr/bin/env python    
#个人学习笔记, no other use   
def add (x=9,y=10):    
    if x>y:    
        a=x+y    
        print "X+y"
        return A    
    if x<y:    
        a=y-x    
        print "Y-x" return
        a    
    if x==y:    
        a=x*y    
        print "X*y" return
        A Print Add (2,3) print add (    
5,4)    
print Add (6,6)

Pass the value of an array to the function

#!/usr/bin/env python    
        
t= (' name ', ' Tom ')    
        
def f (x,y):    
    print "%s:%s"% (X,y)    
f (1,2)    
f (*t)

Passing the value of a dictionary to a function

#!/usr/bin/env python    
        
d={' age ': ' Name ': ' Tom '}    
def f (name= ' Lisa ', age=23):    
     print ' name---%s '% name    
     print ' age----%s '% Age    
f (**d)    
#如果是一个 *, passing the key in the dictionary, two * *, passing the value in the dictionary

Handling Redundant parameter Passes

#!/usr/bin/env python    
def f (x,*args):    
     print x    
     print args    
f (1,2,3,4)    
#结果显示为   
1 (2, 3, 4) # 2,3,4 as a whole passed to args.
def m (x,*args,**kwords):    
     print x    
     print args    
     print kwords    
m (1,2,3,4,5,args=2)    
#运行结果   
1 (2 , 3, 4, 5) {' Args ': 2}

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.