day19函数和作用域

=============函数===================
1.深拷贝和浅拷贝
    浅拷贝:只拷贝第一层,
    深拷贝:完全一样复制


2.数据结构——集合(set)
3.函数
----------------------拷贝----------


    import copy


    s=[1,'alex','alive']
    s1=[1,'alex','alive']
    s1[1]=2
    print(s)
    print(s1)


    s2=s.copy()
    print(s2)


    s=[[1,2],'alex','alive']
    s3=s.copy()
    s3[1]='jajg'
    print(s3)
    print(s)
    s3[0][0]='jsjf'
    print(s)


    浅拷贝:对于容器类型,则指向原来的地址,对于int等非容器的对象,则创建新的地址
    hus=['ajjga',13,[8000,555]]
    wof=hus.copy()
    wof[0]='jagja'
    wof[1]=23


    hus[2][1] -=  200
    print(wof)


    深拷贝,就是克隆
    hs=[['jsjg','jajga'],34,45]
    ad=copy.deepcopy(hs)


    ad[0][0]='ghg'
    print(ad)




 ------------------集合--------------
    集合是存放不同的元素
    列表
    a=[2,3,46]#列表
    b=list([2,34,54])
    print(a)
    print(b)
    #元组
    c=(1,2,3)
    d=tuple([4,5,67])
    print(c)
    print(d)
    s=set('ajg leg')
    print(s)


    简单的几个可以去去重
    s=set('ajagj agja')
    s1=['ajgafa','ajfje','agagag']
    s2=set(s1)
    print(s2,type(s2))
    print(s,type(s))
    #集合对象是一组无序排序的可以哈希的值:集合成员是可以做字典的键
    #可以hash: 不可变的类型
    #想做set 不能是列表和字典
    #集合分为 可变集合 不可变集合


    s.add('aga')
    s.update('oooooouayng')
    print(s)
    #set里面没有重复的值,容器才能进行迭代
    #pop 删除是随机删除
    #clear() 清空


    #集合类型操作符
    print(set('alcex') ==set('alcexxxx'))
    print(set('alex')<=set('alexa'))
    print(set('alex') and set('alexa'))
    print(set('alex') or set('alexa'))


-------------------函数------------------


    import os
    import datetime
    import time
    def logger():
        f=open('a.log','a+',encoding='utf-8')
        f.write('time-2.15.-255')
        f.close()
        print('time-2.15.-255')
    print(datetime)
    print('---funcation')
    logger()
    函数 不等于funcation
    计算机函数,==subrouteine  子程序,procedures 过程
    作用
    1.减少重复代码
    2.方便修改,更易扩展
    3.保持代码的一致性
    def f(index):
        print('funcation %s'%index)
    f(6)
    a=datetime.datetime.now()
    print(a)


    def logger(n):


        time_format='%Y-%m-%d %X'
        time_current=time.strftime(time_format)
        with open('日志记录','a',encoding='utf-8') as f:
            f.write('%s end action%s\n'%(time_current,n))
        c=time.time()


    def action1(n):
        print('starting action1')
        logger(n)


    def action2(n):
        print('starting action2')
        logger(n)


    def action3(n):
        print('starting action3')
        logger(n)


    action1(1)
    action2(2)
    action3(3)
    关键字参数,和位置参数,
    顺序参数,必须参数
    默认参数
    加法器


    def add(*args):
        print(args)
        sum=0
        for i in args:
            sum+=i
        print(sum)
    add(1,2,3,44,8)




    s=7
    s+=1
    print(s)


    def print_info(name,age,sex='male'):
        print('Name: %s'%name)
        print('Age: %d'%age)
        print('Sex: %s'%sex)
    print_info()


    def print_ino(*args,**kwargs):
        for i in args:
            print(i)
        for a in kwargs:
            print(a)
    print_ino(12,343,ajdjg='ajga',adfaf='ajgagag')


    #针对不定长参数*args 和**kwrgs 的参数位置只能按照顺序进行
    #默认参数和不定长参数
    def ad(sex='male',*args,**kwargs):
        for i in args:
            print(i)
        for j in kwargs:
            print(j)
        print(sex)
    ad('jang','jagaga',name='ouyang',ss='ajjg',se='falme',)


    #1.函数没有return,默认返回None
    #2.return 多个对象,python 得到的是一个元组返回




    def ff():
        return 1,3,'3423',8
    print(ff())#(1, 3, '3423', 8)
    a=ff()
    s0=a[0]
    s1=a[1]
    print(s0,s1)
    #返回得到的是一个元组


-------------------------作用域----------------


    #__author:oulen
    #date:2018/5/31
    #函数的作用域
    x=int(2.9)
    g_count=0
    while True:
        x=3
    print(x)


    count=0
    def outer():
        print(count)
        count=5


    '''
    build-in
    LEGB 局部》外层》当前模块全局》python 内置函数
    global:全局变量,


    enclosing:嵌套的父级函数的作用域


    L:local 函数局部作用域
    E:enclosing,
    G:global
    B:built-in


    local:
    '''






=================模块=================




























==================面向对象编程==========

猜你喜欢

转载自blog.csdn.net/qq_37311616/article/details/80530626