day2

it2022-05-05  167

今日内容:1.常用数据类型及内置方法2.文件处理3.函数列表:定义:在[]内,可以存放多个任意类型的值,并以逗号隔开。一般用于存放学生的爱好,课程的周期等....#定义一个学生列表,可存放多个学生students=['张三','李四','王二麻子']print(students[1])#李四students_info=['绘绘',20,'male',['泡8','喝9']]print(students_info[1])#20#优先掌握的操作:#1、按索引存取值(正向存取+反向存取):即可存也可以取print(student_info[-2])#2、切片(顾头不顾尾,步长)print(student_info[0:4:2])#3、长度print(len(student_info))#4、成员运算in和not inprint('绘绘'in student_info)print('绘绘'not in student_info)#5、追加student_info=['绘绘',20,'male',['泡8','喝9']]#在students_info列表末尾追加一个值student_info.append('合肥学院')print(student_info)#6、删除del student_info[2]print(student_info)#7、循环for student in student_info: print(student)##########需要掌握的:####################student_info=['猪猪','98','female',['跳舞','吉他']]#1.index 获取列表中的索引print(student_info.index(98))#2.count 获取列表中某个值的数量print(student_info.count(98))#3.取值,默认取列表中最后一个值,类似删除#若pop()括号中写了索引,则取索引对应的值student_info.pop()print(student_info)sex=student_info.pop(2)print(sex)print(student_info)#4.移除student_info.remove(98)print(student_info)name=student_info.remove('猪猪')print(name)print(student_info)#5.插入值student_info=['猪猪','98','female',['跳舞','吉他'],98]student_info.insert(3,'合肥学院')print(student_info)#6.extend 合并列表student_info1=['猪猪','98','female',['跳舞','吉他'],98]student_info2=['猪猪2','92','female',['跳舞2','吉他2']]#把2插入1student_info1.extend(student_info2)print(student_info1)#############################元组定义:在()中,可以存放多个任意类型的值,并以逗号隔开注意:元组与列表不同的是,只能在定义是初始化,不能对其进行修改优点:在内存中占用的资源比较小,#定义tuple1=(1,2,3,'五','六') #tuple1((1,2,3,'五','六'))print(tuple1)#####优先掌握的操作:#####1、按索引取值(正向取+反向取):只能取print(tuple1[2])#2、切片(顾头不顾尾,步长)#从0开始切片到5-1,步长为2print(tuple1[0:5:2])#取所有的值,步长为2#3、长度print(len(tuple1))#4、成员运算in和not inprint(1 in tuple1)print(1 not in tuple1)#5、循环for line in tuple1: print(line,end=' ')###############可变类型,对其进行修改,内存地址会变#例如 :#列表类型 listlist1=[1,2,3]list2=list1#字典 diet#不可变类型:对值进行修改,内存地址一定不一样#例如:数字类型#int number=100print(id(number))number=111print(id(number)) #float sal=1.0print(id(sal))sal=2.0print(id(sal))#字符串类型 str str1='hello python'print(id(str1))str2=str1.replace('hello','like')print(id(str2)) 元组类型#字典#作用:在{}内可以存多个值,key-value存取,取值速度快#定义:key必须是不可变类型,value可以是任意类型dict1=dict({'age':18,'name':'tank'})dict1={'age':18,'name':'tank'}print(type(dict1))#取值,字典名+[],括号内写值对应的keyprint(dict1['age'])#优先掌握的操作:#1、按key存取值:可存可取#存一个level 9的值到dict1的字典中dict1['level']=9print(dict)print(dict['name'])#2、长度len#3、成员运算in和not in z只看到字典中的keyprint('name'in dict1) #Tureprint('tank'in dict1)# Falseprint('tank'not in dict1)# False#4、删除#5、键keys(),值values(),键值对items()print(dict1.keys())print(dict1.values())print(dict1.items())#6、循环for key in dict1: print(key) print(dict1[key])##getdict1={'age':18,'name':'tank'}print(dict1.get('age'))#[]取值print(dict1.['sex'])##keyError#get取值print(dict1.get('sex'))#None#若找不到就设置一个默认值print(dict1.get('sex','male'))#None##if判断语法if判断条件:若条件成立,执行此处代码逻辑代码elif 判断条件若条件成立,则执行此处代码逻辑代码else 判断条件以上都不成立 则执行此处代码逻辑代码#判断两个数的大小x=10y=20z=30#Tab 往右四个空格#shift+Tab 往左四个空格if x>y print(x)elif z>y print(z)else print(y)###whlie循环语法 whlie 条件判断: #成立执行此处 逻辑代码break #跳出本层循环continue #结束本次循环,进入下一次循环#whlie循环whlie True: name=input('请输入猜测的字符'),strip() if name=='tank': print('tank success!') break print('请重新输入')#限制循环次数str1='tank'#初始值num=0#whlie循环whlie num<3: name=input('请输入猜测的字符'),strip() if name=='tank': print('tank success!') break print('请重新输入') num+=3#############################################文件处理 open() 写文件 wt: 读文件 rt: 追加文件 at: 注意:必须制定字符编码,以什么方式写就医什么方式打开执行python文件的过程: 1.启动python解释器中,加载到内存中 2.把写好的python文件加载到解释器中 3.借测到python语法,执行代码 synaxError 语法错误打开文件会产生两种资源 1.python程序 2.操作系统打开文件#写文件 wt:#参数一:文件的绝对路径#参数二:操作文件的模式f=open('file.txt',mode='wt',encoding='utf-8')f.write('tank')f.close()#关闭操作系统文件资源#读文件 rt:f=open('file.txt',mode='wt',encoding='utf-8')print(f.read())f.close()#关闭#追加文件 at:a=open('file.txt','a',encoding='utf-8')a.write('\n 合肥学院')a.close()#关闭##文件处理之上下文处理#with 可以管理open打开的文件会在with执行完毕之后自动调用close()关闭文件With open() as f "句柄"if aa: passwhlie True: passfor line in range(1): line#写with open('file.txt','a',encoding='utf-8') as f; f.write('墨菲定律')#读with open('file.txt','a',encoding='utf-8') as f; res=f.read() print(res)#追加with open('file.txt','a',encoding='utf-8') as f; f.write('围城')####对图片,视频,音频,进行读写操作####对图片,视频,音频,进行读写操作#rb模式,读取二进制,不需要指定字符编码#du读取相片cxk.jpgwith open ('cxk,jpg','rb')as f; res=f.read() print(res)jpg=res#吧cxk.jpg 的二进制流写入cxk.jog文件中with open('cxk_copy.jpg','wb')as f_w: f_w.write(jpg)'''with 管理多个文件'''#通过with来管理open打开的两个文件句柄 f_r,f_wwith open('cxk','rb') as f_r,open('cxk_copy.jpy','wb')as f_w: #通过f_r句柄吧图片的二进制流读取出来 res=f_r.read() #通过f_r句柄吧图片的二进制流写入cxk_copy.jpg文件中 f_w.write(res) ######################################################四、函数 什么是函数 函数是一把工具 使用函数的好处: 1.解决代码冗余问题 2.使代码结构更清晰 3.易管理 函数的使用必须遵循:先定义, 后调用 函数定义语法: def 函数名(参数1,参数2...): ''' 注释:声明函数''' 逻辑代码 return 返回值 def:define 定义。 函数名:必须看其命知其意 ():接收外部传入的参数 注释:用来声明函数的作用 return:返回给调用的值定义函数的三种形式:1.无参函数不需要接受外部传入的参数2.有参函数需要接受外部传入的参数3.空函数pass#1.无参函数def login(): user=input('请输入用户名'),strip() pwd=input('请输入用户名'),strip() if user=='tank'and pwd=='123': print('login success!') else: print('login error!')#函数的内存地址print(login)#函数调用login()#2.有参函数def login(username ,password): user=input('请输入用户名'),strip() pwd=input('请输入用户名'),strip() if user=='tank'and pwd=='123': print('login success!') else: print('login error!')#函数调用#若函数在定义时需要接收参数,调用者必须为其穿传参login('tank','123')#3.空函数'''ATM1.登录2.注册3.体现4.取款5.转账6.还款'''#登录功能def login(): #代表什么都不做 pass#注册功能def register(): #代表什么都不做 pass#还款def repy(): #代表什么都不做 pass........................................'''参数的参数'''#在定义阶段 x,y称之为形参def func(x,y): print(x,y)# 在调用阶段10,100称之为实参func(10,100)'''关键字参数关键字实参'''#位置形参x, ydef func(x, y):print(x, y)#在调用阶段: x=10, y=100称之为关键字参数。func(y=111, x=10) #10 111#不能少传# func(y=111) # 报错TypeError#不能多传# func(y=111, x=222, z='333') #报错TypeError默认参数:在定义阶段,为参数设置默认值def foo(x=10,y=20): print(x, y)#不传参,则使用默认参数foo()#传参,使用传入的参数foo (200,300)'''函数的嵌套定义函数对象函数的名称空间:内置:pythjon解释器自带的都称之为“内置名称空间”全局:su所有顶着头写的变量 函数 都称为”全局名称空间“局部;zai在函数内部定义的,都称之为“局部名称空间”名称空间加载顺序:内置---全局---局部名称空间查找顺序:局部---全局---内置'''#h函数的嵌套定义def func1(): print('from func1..') def func2(): print('from func2..')#函数对象 print( func1) def f1(): pass def f2(): passdic1={'1':f1,'2':f2}choice=input('请选择功能编号:')if choice=='1': print(dic1[choice]) dic1[choice]()elif choic=='2' print(dic1[choicel]) dicl[chioce]()x=10#名称空间def func1():#x=20 print('from func1..') print(x) def func2(): print('from func2..') func1() #func2()#error

转载于:https://www.cnblogs.com/Corbett/p/11087319.html


最新回复(0)