美文网首页
06Python学习之路-抽象

06Python学习之路-抽象

作者: YKDog | 来源:发表于2016-05-20 17:41 被阅读26次

    06Python学习之路-抽象

    斐波那契数列

    fibs = [0, 1]
    for i in range(10):
        fibs.append(fibs[-1] + fibs[-2])
        
    输出
    >>> fibs
    [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
    >>> 
    
    

    callable判断是否可以被调用

    >>> import math
    >>> x = 1
    >>> y = math.sqrt
    >>> callable(x)
    False
    >>> callable(y)
    True
    
    

    第一个函数

    def hello(name):
        return "Hello, " + name + " !"
        
    输出
    >>> hello("YKDog")
    'Hello, YKDog !'
    
    

    利用函数打印斐波那契数

    def fibs(num):
    
        fib = [0, 1];
        for i in range(num - 2):
            fib.append(fib[-1] + fib[-2])
        return fib
        
    输出
    
    >>> fibs(5)
    [0, 1, 1, 2, 3]
    

    字符串是值类型, 不是引用类型

    def try_change(n):
    
        n = "YKDog"
        return
    
    调用
    >>> x = "YKPig"
    >>> try_change(x)
    >>> x
    'YKPig'
    
    

    列表是引用类型

    def try_change(n):
    
        n[0] = "Good Game"
        return
        
    调用
    >>> x
    ['Good Game', 'haha']
    
    

    引用 与 赋值

    >>> x = ["Hello", "YKDog"]
    >>> y = x[:]
    >>> y == x
    True
    >>> y is x
    False
    >>> z = y
    >>> z is y
    True
    

    初始化函数

    def init(data):
        data["first"] = {}
        data["middle"] = {}
        data["last"] = {}
        
    调用
    >>> storage = {}
    >>> init(storage)
    >>> storage
    {'first': {}, 'last': {}, 'middle': {}}
    
    

    get使用

    >>> dicta = {"a":"A", "b":"B"}
    >>> dicta.get("a")
    'A'
    

    字符串的分割

    >>> x = "**Hello**world*".split("*")
    >>> x
    ['', '', 'Hello', '', 'world', '']
    >>> x = " Hello ".split()
    >>> x
    ['Hello']
    
    

    函数的参数名和默认值 值得注意的是可以改变形参名字顺序 省略参数个数

    def hello(name = "YKDog", greeting = "Hello"):
    
        #YKDog Hello, is default
        print("%s ,%s !" % (greeting, name))
        
    调用   
        
    >>> hello()
    Hello ,YKDog !
    >>> hello(name = "YTPig", greeting = "Hello")
    Hello ,YTPig !
    >>> hello(greeting = "Hello", name = "YTPig")
    Hello ,YTPig !
    
    >>> hello(name = "QF")
    Hello ,QF !
    
    >>> hello("hehe")
    Hello ,hehe !
    
    

    *元素收集 而不是 指针

    def collection(x, y, z = 3, *pa, **pi):
        print(x, y, z)
        print(pa)
        print(pi)
        
    调用
    不能使用x = 1名字和形参一样 冲突 *是收集单个元素为元组, **是收集对应等式, 建立字典.
    
    >>> collection(1, 2, 3, 4, 5, x = 1, y = 2)
    Traceback (most recent call last):
      File "<pyshell#55>", line 1, in <module>
        collection(1, 2, 3, 4, 5, x = 1, y = 2)
    TypeError: collection() got multiple values for argument 'x'
    >>> collection(1, 2, 3, 4, 5, a = 1, b = 2)
    1 2 3
    (4, 5)
    {'a': 1, 'b': 2}
    >>> 
    
    

    已经是元组 加 * 分散

    def add(x, y): return x + y
    
    >>> add(*(1, 2))
    3
    
    

    阶乘计算

    def factorial(n):
        result = 1
        for i in range(1, n+1):
            result *= i
        return result 
        
    结果
    >>> factorial(5)
    120
    

    相关文章

      网友评论

          本文标题:06Python学习之路-抽象

          本文链接:https://www.haomeiwen.com/subject/uspnrttx.html