zoukankan      html  css  js  c++  java
  • Python学习笔记-DAY-3

    1.集合

    集合是一个无序的,不重复的数据组合,它的主要作用:
    去重&&关系测试

    list_1=set([1,3,5,7,9,4,6,])
    list_2=set([2,4,5,6,8,10])
    
    print(list_1.union(list_2))
    print(list_1.difference(list_2) )
    print(list_1.issubset(list_2))
    print(list_1.issuperset(list_2))
    print(list_1.symmetric_difference(list_2))
    print('运算符操作')
    print(list_1 | list_2)
    print(list_1 & list_2)
    print(list_1 - list_2)
    print(list_1 ^ list_2)
    
    print('增删改查')
    list_1.add(999)
    list_1.update([11,12,13])
    
    list_1.discard(11)
    list_1.remove(12)
    
    
    print(list_1)

    2.文件操作

    基本操作

    打开文件的模式有:

    • r,只读模式(默认)。
    • w,只写模式。【不可读;不存在则创建;存在则删除内容;】
    • a,追加模式。【可读;   不存在则创建;存在则只追加内容;】

    "+" 表示可以同时读写某个文件

    • r+,可读写文件。【可读;可写;可追加】
    • w+,写读
    • a+,同a

    "U"表示在读取时,可以将 自动转换成 (与 r 或 r+ 模式同使用)

    • rU
    • r+U

    "b"表示处理二进制文件(如:FTP发送上传ISO镜像文件,linux可忽略,windows处理二进制文件时需标注)

    • rb
    • wb
    • ab
    # 为了避免打开文件后忘记关闭,可以通过管理上下文,即:
    
    with open('log','r') as f:
    
    #如此方式,当with代码块执行完毕时,内部会自动关闭并释放文件资源。
    #在Python 2.7 后,with又支持同时对多个文件的上下文进行管理,即:
    
    with open('log1') as obj1, open('log2') as obj2:
        pass
     
    
    
    
    # Author AlleyYu
    
    fp = open('CharacterList.txt','r+',encoding='utf-8')
    raw_info=fp.read()
    #print(raw_info)
    #fp.write('this is the newinfo from python')
    
    
    #
    # for line in fp:
    #     print(line)
    
    
    for index, line in enumerate(fp.readlines()):
    
        if index==3:
            print('it is LINE 3 here')
            continue
    
        print(line)
    
    # import sys ,time
    #
    # for i in range(20):
    #     sys.stdout.write('#')
    #     sys.stdout.flush()
    #     time.sleep(0.1)
    fp.close()

    3.字符编码与转码

    4.函数

    函数是指将一组语句的集合通过一个名字(函数名)封装起来,要想执行这个函数,只需调用其函数名即可

    特性:

    1. 减少重复代码
    2. 使程序变的可扩展
    3. 使程序变得易维护

    形参&实参

    形参变量只有在被调用时才分配内存单元,在调用结束时,即刻释放所分配的内存单元。因此,形参只在函数内部有效。函数调用结束返回主调用函数后则不能再使用该形参变量

    实参可以是常量、变量、表达式、函数等,无论实参是何种类型的量,在进行函数调用时,它们都必须有确定的值,以便把这些值传送给形参。因此应预先用赋值,输入等办法使参数获得确定值

    位置参数,关键字参数,不固定参数,默认参数

    # Author AlleyYu
    # import sys,time
    # #print(sys.getdefaultencoding() )
    #
    #
    #
    # def func():
    #
    #   # time_format='%Y-%m-%d %X'
    #   # time_current=time.strptime(time_format)
    #    with open('CharacterList.txt','a+') as f:
    #        f.write(' end as 
    ')
    #
    #
    # def test1():
    #
    #     print ('this is test1')
    #     func()
    #
    # def test2():
    #
    #     print('this is test2')
    #     func()
    #     return 0
    #
    #
    # def test3():
    #     print('this is test3')
    #     func()
    #     return 1, 'hello',[5,7,9]
    
    
    
    #
    # def test4(x,y,*args,**kargs):
    #
    #     print(x,y,args,kargs)
    #
    
    
    # test1()
    # # test2()
    # # test3()
    # test4(1,2,h=13,z=15)
    # test4(1,2,5,h=13,z=15,j='help')
    # test4(1,2,h=13,z=15,j='help')

    局部变量与全局变量

    在子程序中定义的变量称为局部变量,在程序的一开始定义的变量称为全局变量。
    全局变量作用域是整个程序,局部变量作用域是定义该变量的子程序。
    当全局变量与局部变量同名时:
    在定义局部变量的子程序内,局部变量起作用;在其它地方全局变量起作用。
    name='alley'
    school='bupt'
    
    def test5():
         name='tongtong'
         school='kingdgarden'
         print ('func content', name ,school)
    
    print ('global info', name , school)
    test5()

    . 递归

    递归特性:

    1. 必须有一个明确的结束条件

    2. 每次进入更深一层递归时,问题规模相比上次递归都应有所减少

    3. 递归效率不高,递归层次过多会导致栈溢出(在计算机中,函数调用是通过栈(stack)这种数据结构实现的,每当进入一个函数调用,栈就会加一层栈帧,每当函数返回,栈就会减一层栈帧。由于栈的大小不是无限的,所以,递归调用的次数过多,会导致栈溢出)

    # Author AlleyYu
    
    def cacu(n):
        print(n)
        if(int(n/2)>0):
            cacu(n/2)
        print(n)
    
    def cacu2(n):
        print('2:;;;;',n)
        if(int(n/2)>0):
           return(cacu(n/2))
        print('2:;;;;;',n)
    
    cacu(10)
    cacu2(10)

    高阶函数

    变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接收另一个函数作为参数,这种函数就称之为高阶函数。

    1
    2
    3
    4
    5
    6
    def add(x,y,f):
        return f(x) + f(y)
     
     
    res = add(3,-6,abs)
    print(res)

    内置函数

  • 相关阅读:
    python编码小记
    eventlet学习笔记
    python库文件路径
    paste deploy初探
    python浅拷贝与深拷贝
    Javascript basic knowledge
    用大数据学习心理学
    Postgres Database management operations
    Python Socket and WSGI Sample
    Failed to connect socket to '/var/run/libvirt/libvirt-sock': No such file or directory错误处理
  • 原文地址:https://www.cnblogs.com/alleyyu/p/5751225.html
Copyright © 2011-2022 走看看