zoukankan      html  css  js  c++  java
  • Python学习笔记2(函数)

    #callback 判断是否存在
    import match
    x = 1
    y = math.sqrt
    
    callable(x)
    #False
    callable(y)
    #True
    
    #函数
    def fib(num):
        result = [0,1]
        for i in range(num-2):
            result.append(result[-2],result[-1])
            return result;
    
    fibs(10)
    #[0,1,1,2,3,5,8,13,21,34]
    
    #函数文档
    def square(x):
        'Calcutates the square of the number x'
        return x*x
    
    square.__doc__
    #'Calcutates the square of the number x'
    
    #None
     def test():
         print 'This is printed'
         return
         print 'This is not'
    
    x = test()
    #This is printed
    
    print x 
    #None
    
    #改变列表
    def change(n):
        n[0] = 'Mr.Gumby'
    
    names = ['Mr.Entiy','Mrs.Thing']
    change(names)
    print names
    #['Mr,Gumby','Mrs.Thing']
    
    #复制列表
    names = ['1','2']
    n = names[:]
    
    n is names
    #False
    n == names
    #True
    
    #位置参数
    def hello_1(greeting,name):
        print '%s,%s!' % (greeting,name)
    
    def hello_2(name,greeting):
        print '%s,%s!' % (name,greeting)
    
    hello_1('Hello','World')
    #Hello,World
    hello_2('Hello','World')
    #Hello,World
    hello_2(greeting='Hello',name='World')
    #World,Hello
    
    #收集参数
    
    def print_params(title,*params):
        print title
        print params
    
    print_params('params:',1,2,3)
    #params:
    #(1,2,3)
    
    def print_params2(title,**params):
        print title
        print params
    
    print_params2('params:',x=1,y=2,z=3)
    #{'z':3,'x':1,'y':2}
  • 相关阅读:
    node03- NODE入门
    node02- NPM的使用
    node01- 初识、特点、
    Linux中的split命令,文件切割
    修改jar包中文件
    ByteBuf 使用
    Stream的flatmap与map异同的理解
    h5底部输入框被键盘遮挡问题
    div给我画条龙
    contains 了解
  • 原文地址:https://www.cnblogs.com/longdidi/p/3162706.html
Copyright © 2011-2022 走看看