zoukankan      html  css  js  c++  java
  • python笔记一

    tuple所谓的“不变”是说,tuple的每个元素,指向永远不变
    
    “可变的”tuple    
t = (1,[2,3])     
    t[1][1]='4'     
    print(t);        
    # (1, [2, '4'])    
    
    要定义一个只有1个元素的tuple 
    因为括号()既可以表示tuple,又可以表示数学公式中的小括号,这就产生了歧义,因此,Python规定,这种情况下,按小括号进行计算,计算结果自然是1。
    t = (1);
    print(t) # 1 被当作括号
    
    所以,只有1个元素的tuple定义时必须加一个逗号,,来消除歧义   
    t = (1,) #  (1,)
       
    % 格式化字符串  
%s表示用字符串替换,%d表示用整数替换,%f表示浮点数, 
    %+显示正负号,%-左对齐,%0 补零,%[-+0宽度.精度]
    print('%0+7.2f' %(1.123456)) 7位,小数点后取2位,不够补0,前面使用了正负号    
    # +001.12   
    
    
    set和dict类似,也是一组key的集合,但不存储value。由于key不能重复,所以,在set中,没有重复的key。
    print(set([1,2,2,2,2,3,4,5]))
    # {1, 2, 3, 4, 5}   
    
          
    定义函数时,需要确定函数名和参数个数;    
    如果有必要,可以先对参数的数据类型做检查;    
    函数体内部可以用return随时返回函数结果;     
    函数执行完毕也没有return语句时,自动return None。     
    函数可以同时返回多个值,但其实就是一个tuple。 
    
    def hello(x = 1,y = 2, *o) :  # *x 可变参数    
        '函数说明'
        pass
        return x, y
    
    print(hello(3,4));
     # (3, 4)    
    
    
    def world (l = []) :    # 默认参数指向对象的问题    
        l.append('hello');    
        return l;      
    print(world())     
    print(world())    
    # ['hello']
    # [‘hello', 'hello']   
    
    def world (l = None) :       
        if l is None :   
            l = []     
            l.append('hello')    
        return l    
    print(world())         
    print(world())       
    # ['hello']      
    # ['hello']   
    
    
    关键字参数
函数的调用者可以传入任意不受限制的关键字参数。至于到底传入了哪些,就需要在函数内部通过kw检查      
    def hello(x = 1,y = 2, **o) :  # **o 关键字参数           
        '函数说明'      
        if 'name' in o :    
            print (o['name'])     
        return    
    
    hello(3,4, name = 'leyi')  
    # leyi
    
    命名关键字参数
如果要限制关键字参数的名字,就可以用命名关键字参数,
    和关键字参数**kw不同,命名关键字参数需要一个特殊分隔符*,*后面的参数被视为命名关键字参数 , 如果函数定义中已经有了一个可变参数,后面跟着的命名关键字参数就不再需要一个特殊分隔符*了,如果没有可变参数,就必须加一个*作为特殊分隔符    
    
    def hello(x = 1,y = 2, *, name) :  # *之后的name 为命名关键字参数
        '函数说明'    
        print(name)     
        return    
    
    hello(3,4, name = 'leyi')     
    # leyi              
            
          
    匿名函数  
    l = lambda x, y : x *y       
    print(l(2,3))    
    # 6    
    
    装饰器   
    在代码运行期间动态增加功能的方式,称之为“装饰器”(Decorator),
    本质上,decorator就是一个返回函数的高阶函数      
    
    def middleFunction (fn) :        
        def wrapper(*args) :    
            print("插入的功能", fn.__name__)        
            return fn(*args) 
        return wrapper    
    
    @ middleFunction
    def hello () :            
        print('hello world')    
            
    hello()    
        
      
    模块的标准文件模板     
    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    
    ‘module description’
    
    __author__ = ‘leyi’
    
    # code
    def  hello () :
    	pass 
    
    # 命令行运行该模块时执行     
    if __name__=='__main__':
        hello()
    
    
    __xxx__这样的变量是特殊变量 
    类似_xxx和__xxx这样的函数或变量是非公开的(private)(规范)   
    

      

  • 相关阅读:
    Balanced Binary Tree
    Swap Nodes in Pairs
    Reverse Nodes in k-Group
    Reverse Linked List II
    Remove Nth Node From End of List
    Remove Duplicates from Sorted List II
    Remove Duplicates from Sorted List
    Partition List
    Merge Two Sorted Lists
    【Yii2.0】1.2 Apache检查配置文件语法
  • 原文地址:https://www.cnblogs.com/leyi/p/11514364.html
Copyright © 2011-2022 走看看