• 第九章元组、文件及其他


    #1.
    #A:+ * 切片 作用于元组的时候会返回新元组
    #B:元组的不可变性只适用于于元组本身顶层而非其内容,例如:元组内部列表时可以被修改的
    tupleTem = ()                       #tupleTem = ()
    tupleTem = 0, 1                     #tupleTem = (0, 1)
    tupleTem = (0)                      #tupleTem = 0
    tupleTem = tupleTem + 1             #tupleTem = 1
    tupleTem = (0, )                    #tupleTem = (0,)
    #tupleTem = tupleTem + 1            #运行出错
    tupleTem += (1, 2)                  #tupleTem = (0, 1, 2)
    #tupleTem += (3)                    #运行出错
    v = (v for v in range(1, 3))        #v = <generator object <genexpr> at 0x0000000002D5F5E8>
    tupleTem = tuple(v)                 #tupleTem = (1, 2)
    tupleTem = 1, 2, 3, 4               #tupleTem = (1, 2, 3, 4)
    tupleTem = (1, (1, 's'))            #tupleTem = (1, (1, 's'))
    v = tupleTem[1][1]                  #v = 's'
    tupleTem = ([1, 2], 2, 3,)          #tupleTem = ([1, 2], 2, 3)
    #tupleTem[0] = [1]                  #运行出错
    tupleTem[0].append('s')             #tupleTem = ([1, 2, 's'], 2, 3)
    
    tupleTem = tuple((v * 4 for v in 'szn'))    #tupleTem = ('ssss', 'zzzz', 'nnnn')
    tupleCopy = tupleTem[:]                     #tupleCopy = ('ssss', 'zzzz', 'nnnn')
    tupleTem += (1, 2)                          #tupleTem = ('ssss', 'zzzz', 'nnnn', 1, 2)  tupleCopy = ('ssss', 'zzzz', 'nnnn')
    
    #2.
    #A:文件迭代器是最好的读取行工具
    #B:write()返回写入字符数
    buff = []
    with open('1.txt', "rb") as pFile:
        for line in pFile:
            buff.append(line)
    #buff = [b'1
    ', b'2
    ', b'3']
    
    pFile = open('2.txt', 'w')
    v = pFile.write('12
    3')            #v = 4
    pFile.close()
    
    #3.
    #A:pickle模块,能让我们直接在文件中存储几乎任何python对象
    import pickle
    value = [1, 2, (3, 4), '5', {6:7}]
    pFile = open("Test.pkl", "wb")
    pickle.dump(value, pFile)
    pFile.close()
    pFile = open("Test.pkl", "rb")
    value1 = pickle.load(pFile)         #value1 = [1, 2, (3, 4), '5', {6: 7}]
    
    #4.
    try:
        0 / 2               #执行至此
    except ZeroDivisionError:
        print()
    else:
        print()             #执行至此
    finally:
        print()             #执行至此
    
    try:
        2 / 0               #执行至此
    except KeyError:
        print()
    except ZeroDivisionError:
        print()             #执行至此
    else:
        print()             
    finally:
        print()             #执行至此
    
    #5.
    #A:赋值操作总是存储对象的引用,而不是这些对象的拷贝
    #B:无条件的切片以及copy()只能做顶层复制,不能够复制嵌套的数据结构
    value = [1, 2]
    v0 = [value, 's']
    v1 = {'a' : value}
    v1['a'].append('s')     #v1 = {'a': [1, 2, 's']}    value = [1, 2, 's']    v0 = [[1, 2, 's'], 's']
    
    v0 = [1, 2]
    v1 = [v0, 's']
    v2 = v1.copy()
    v2[0].append('z')       #v2 = [[1, 2, 'z'], 's']    v0 = [1, 2, 'z']
    
    v0 = [1, 2]
    v1 = [v0, 's']
    import copy
    v2 = copy.copy(v1)
    v2[0].append('z')       #v2 = [[1, 2, 'z'], 's']    v0 = [1, 2, 'z']
    
    v0 = [1, 2]
    v1 = [v0, 's']
    import copy
    v2 = copy.deepcopy(v1)
    v2[0].append('z')       #v2 = [[1, 2, 'z'], 's']    v0 = [1, 2]
    
    #6.
    #A:在python3中,不支持字典大小比较,字典通过排序之后的(键、值)列表进行比较
    #B:在在python3中,混合数字比较是错误的,比如1 < 's'
    v0 = {'a':2,'b':1}
    v1 = {'b':1, 'a':2}
    #v = v0 < v1            #运行出错
    v = v0 == v1            #v = True
    
    #v = 1 < 's'            #运行出错
    #v = 1 < [1]            #运行出错
    
    #7.
    #A:任何非空对象为真,非0数字为真,其余为假,None也为False
    
    #8.
    #A:isinstance(object, classinfo) object是否是classinfo类型
    v = isinstance('s', str)        #v = True
    v = isinstance(1, list)         #v = False
    v = isinstance([], list)        #v = True
    v = isinstance((1, 's'), tuple) #v = True
    
    #9.
    v = [1, 2]
    v1 = v * 4              #v1 = [1, 2, 1, 2, 1, 2, 1, 2]
    v2 = [v] * 4            #v2 = [[1, 2], [1, 2], [1, 2], [1, 2]]
    v[0] = 0                #v1 = [1, 2, 1, 2, 1, 2, 1, 2]  v2 = [[0, 2], [0, 2], [0, 2], [0, 2]]
    
    #10.
    v0 = [1, 2]
    v1 = v0
    v0.append(3)            #v0 = [1, 2, 3] v1 = [1, 2, 3]
    
    v = [1, 2]
    v.append(v)             #v = [1, 2, [...]]
    value = v[2][2][2][2][2][2][2][2][2][2]     #value = [1, 2, [...]]
    print(v)                #输出[1, 2, [...]]
    

      

  • 相关阅读:
    浏览器开发者工具----F12 功能介绍
    python 爬虫之beautifulsoup(bs4)使用 --待完善
    python 爬虫之beautifulsoup(bs4)环境准备
    python dic字典使用
    python list的使用
    python 实现进制转换(二进制转十进制)
    字符串str的使用方法
    python 第一课 helloworld
    python 学习地址
    React中setState 什么时候是同步的,什么时候是异步的?
  • 原文地址:https://www.cnblogs.com/szn409/p/6545523.html
Copyright © 2020-2023  润新知