美文网首页
python基础五

python基础五

作者: 我想走走 | 来源:发表于2018-02-26 10:17 被阅读16次
    #rb模式
    f = open('test1.py','rb') #b的方式不能指定编码
    data = f.read()
    print(data)
    print(data.decode('utf-8'))
    #wb模式
    g = open('test2.py','wb')
    g.write(bytes('111\n',encoding='utf-8'))
    g.write('峰峰'.encode('utf-8'))
    #ab模式 append缩写表示在最后追加
    s = open('test2.py','ab')
    s.write(bytes('111\n',encoding='utf-8'))
    s.write('xiaoming'.encode('utf-8'))
    
    f = open('a.txt','r',encoding='utf8')
    data = f.read()
    print(data)
    print(f.closed)
    print(f.encoding)
    
    error = f.errors
    print(error)
    
    f.flush()
    f.name
    #告诉你当前位置在哪里,按照字节来读
    f.readline()
    print(f.tell())
    #控制光标的移动
    f.seek(3)
    print(f.tell())
    #read是按照字符进行读,而其他的是按照字节进行读
    newdata = f.read(1)
    print(newdata)
    #表示字节截取10个字节
    f.truncate(10)
    
    a = [1,2,3]
    for i in a:
        print(i)
    
    x = 'hello'
    print(dir(x))
    
    iter_a = a.__iter__()
    print(iter_a.__next__())
    
    index = 0
    while index < len(a):
        print(a[index])
        index+=1
    diddai_l = a.__iter__()
    
    
    while True:
        try:
            print(diddai_l.__next__())
        except StopIteration:
            print('执行完毕')
            break
    #三元表达式
    name = 'a'
    res ='c' if name == 'a' else 'b'
    print(res)
    
    #列表解析
    egg_list = []
    for i in range(10):
        egg_list.append('鸡蛋%s' %i)
    print(egg_list)
    
    new_list = ['鸡蛋%s' %i for i in range(10)]
    new_list = ['鸡蛋%s' %i for i in range(10) if i < 5]
    print(new_list)
    #生成器表达式
    laomuji = ('鸡蛋%s' %i for i in range(10))
    print(laomuji)
    print(next(laomuji))
    print(next(laomuji))
    print(next(laomuji))
    
    def test():
        yield 1
        yield 2
        yield 3
        yield 4
    g= test()
    print(g)
    print(next(g))
    print(next(g))
    
    second_list = [1,2,3,4]
    print(sum(second_list))
    
    
    #浅拷贝
    s = [[1,2,3],'alex','alvin']
    s1 = s.copy()
    s1[0][0] = 8
    print(s1)
    print(s)
    #深拷贝
    import copy
    snew = [[1,2,3],'alex','alvin']
    s2= copy.deepcopy(snew)
    s2[0][0] = 999
    print(snew)
    print(s2)
    
    #集合的创建
    a = [1,3,4]
    b = list([4,5,6])
    print(a)
    print(b)
    #无序 去重复
    s = set('fengfeng')
    sm = ['aleven','ee','aleven']
    print(set(sm))
    print(s)
    for i in s:
       print(i)
    

    相关文章

      网友评论

          本文标题:python基础五

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