1.列表实现:
students=['gjw','chj'] print(students[1])student_info=['gjw','18','男',['打球','喝酒']] print(student_info[1]) print(student_info[3][1]) print(student_info[0:4:2])末尾追加值 student_info.append('hfuu') print(student_info)删除值 del student_info[1] print(student_info)
2.index获取列表索引值:
index获取列表中的索引值 student_info=['chj',22,'female',['唱','跳','篮球']] print(student_info.index(22)) print(student_info.count(22)) student_info.pop() print(student_info)取出列表中索引为2的值 sex=student_info.pop(2) print(sex) print(student_info)
3.移除列表中某个值:
# student_info=['chj',22,'female',['唱','跳','篮球']]# print(student_info.index(22))# print(student_info.count(22))
4.合并列表
student1_info=['gjw','18','男',['打球','喝酒']]student1_info.extend(student_info) print(student1_info)
5.元组的各种:
tuple1=(1,2,3,4,'五') print(tuple1) print(tuple1[0:5:2])字典类型:key为不可变类型,value为任意类型 dict=({'age':18,'name':'gjw'}) print(dict) print(type(dict))按key存取值 dict['level']=9 print(dict) print(dict.keys()) print(dict.values())循环遍历所有的key值 for key in dict: print(key) print(dict[key])get取值 print(dict.get('sex','male')) print(dict.get('sex'))
6.函数各种:
#if..else 循环,while循环 str1='gjw' while True: name=input('请输入猜测的字符:').strip() if name=='gjw': print('gjw sucess') breakprint('请重新输入:')
7.执行Python文件等:
'''执行python的文件过程: 1:启动python解释器,加载到内存中 2:把写好的python文件加载到解释器中 3:检测python语法,执行代码打开文件会产生两种资源: 1:python程序 2:操作系统打开文件'''#参数一:文件的绝对路径,参数二:操作文件的模式wt:写文本,rt:读文本,at:追加文本 f=open('file.text',mode='wt',encoding='utf-8') f.write('gjw') f.close()#关闭操作系统文件资源 f = open('file.text', mode='r', encoding='utf-8') print(f.read()) f.close() #追加写文本文件 a=open('file.text','a',encoding='utf-8') a.write('\n hfuu') a.close()#写 with open('file.txt','w',encoding='utf-8') as f: f.write('fuck you') #读 with open('file.txt', 'r', encoding='utf-8') as f: res=f.read() print(res)#追加 with open('file.txt', 'a', encoding='utf-8') as f: f.write('mother fuck')rb模式读取二进制不需要指定字符编码读取照片cxk.jpg with open('cxk.jpg','rb') as f: res=f.read() print(res)jpg=res #把cxk。jpg二进制流写入cxl_copy.jpg文件中 with open('cxk_copy.jpg','wb')as f_w: f_w.write(jpg)
8.有参、无参函数:
# '''# 1、无参函数:不需要接受外部传入的参数# 2、需要外部接受传入的参数# 3、空函数# ''' def login(username,passward): user=input('请输入用户名:').strip() pwd=input('请输入密码:').strip() if user==username and pwd==passward: print('登录成功!') else: print('滚!') login('gjw','123')'''ATM 1.登录 2.注册 3.提现 4、取 5、还 6、转''' #1 def login(): pass #2 def register(): pass #3 def repay(): pass def func(x,y): print(x,y)func(10,100)#x=10def func1(): x=20 print('from func1...') print(x) def func2(): print('from func2...')func1()print(func1)def f1(): passdef f2(): passdic1={ '1':f1(),'2':f2}choice=input('请选择功能编号;')if choice=='1': print(dic1[choice]) dic1[choice]()elif choice=='2': print(dic1[choice]) dic1[choice]()