f = 3 d = 6 # print(f>5 or d>5) # print(not (d>5)) # (f>5) and print(111) #输出 # print( '我是好人%s' % ('哈哈')) # name = input('请输入名字') # print( 'tama d huaile %s' % (name)) # if 6>7: # print(222) # else: # print(555) # a = range(1,10) # for i in a: # print(i) # list 添加 # a = [1,2,4,5] # a.append(6) # a.insert(2,66) #插入到指定位置 # print(a) # list 删除 # a = [1,2,4,5] # a.pop() # del(a[2]) # a.remove(2) # print(a) # for循环拿到index # list = ['a', 'b', 'c'] # # for index, v in enumerate(list): # print(index) # print(v) # 字典查询 # b = {'aaa':20,'fff':90} # print(b.get('aaa')) # print(b['aaa']) # 字典添加 # b = {'aaa':20,'fff':90} # b['hhh'] =202 # print(b) # 字典删除 # b = {'aaa':20,'fff':90} # # del b #全部删除 # del b['aaa'] #删除某一个 # b.clear() #删除所有元素 但保留字典 只是为空而已 # print(b) # 字典遍历 # b = {'aaa':20,'fff':90} #拿到键 # for k in b.keys(): # print(k) #拿到值 # for v in b.values(): # print(v) #拿到键和值 # for k,v in b.items(): # print(k,v) #拿到元素 # for item in b.items(): # print(item[0]) # print(item[1]) #局部变量 # def getNum(n = '扯淡'): # a = 2 # print(a) # getNum() # print(a) #全局变量 # a = 1 # print(a) # def getNum(): # print(a) # getNum() #文件操作 # f = open('test.txt','w') # write写 创建一个文件 # f.write('hell word') #写入文件内容 # f = open('demofile/test.txt','a') # a是 append 往文件里追加 # f.write('哈哈哈\n' *5) #导入json模块 将数组转为json数据存入文件 import json # namelist = ['jms','anna'] # #序列化 转为json # name = json.dumps(namelist) # fp = open('demofile/test.txt','a') # fp.write(name) # 反序列化 # fp = open('demofile/test.txt','r') # content = fp.read() # fanxulie = json.loads(content) # print(type(fanxulie)) # try: # open('demofile/test1.txt', 'r') # except: # print('出现异常')
标签:aaa,python,练习,基础,json,print,txt,open,字典 From: https://www.cnblogs.com/xm666/p/17301759.html