列表: 定义:在[]内,可以存放多个任意类型的值,并以逗号隔开,一般用于存放学生的爱好,课堂的周期等等 #定义一个学生列表,可存放多个学生 students = ['儿子','爷爷'] print(students[1]) student_info = ['scy',20,'male',['冲锋','study']] #取scy的所有爱好 print(student_info[3]) #取scy的第二个爱好 print(student_info[3][1]) print(student_info[-2]) #2的意思是每隔两个输出 print(student_info[0:4:2]) student_info.append('合肥学院') print(student_info) 删除 del student_info = ['scy',20,'male',['冲锋','study']] del student_info[2] print(student_info) 获取列表中某个值的索引 index student_info = ['scy',20,'male',['冲锋','study']] print(student_info.index(20)) #获取列表中某个值的数量 count print(student_info.count(20)) #取值,默认取列表中最后一个值 student_info.pop() print(student_info) #取出列表中索引为2的值,并赋值给sxe变量名 sex = student_info.pop(2) print(sex) print(student_info) 移除,把列表中的某个值的第一个值移除 student_info = ['scy',20,'male',['冲锋','study']] student_info.remove(20) print(student_info) name = student_info.remove('scy') print(name) print(student_info) 插入值 insert student_info = ['scy',20,'male',['冲锋','study']] student_info.insert(3,'合肥学院') print(student_info) 合并列表 extend student_info1 = ['scy',20,'male',['冲锋','study']] student_info2 = ['erzi',18,'female',['冲锋','work']] student_info1.extend(student_info2) print(student_info1)
''' 元祖: 定义:在()内,可以存放多个任意类型的值,并以逗号隔开。 注意:元祖与列表不一样的是,只能在定义时初始化值,不能对其进行修改 优点:在内存中占用的资源比列表要小 ''' tuple1 = (1,2,3,'五','六') print(tuple1) #按索引取值 print(tuple1[2]) #切片 print(tuple1[0:5:2]) #长度 print(len(tuple1)) #in和not in print(1 in tuple1) #True print(1 not in tuple1) #False #循环 for line in tuple1: #默认end参数是\n #print(line) print(line,end=' ')
''' 不可变类型: 变量的值修改后,内存地址一定不一样 数字类型 int float 字符串类型 char 元祖类型 tuple 可变类型: 列表类型 list 字典类型 dict ''' #不可变 number = 100 print(id(number)) number = 111 print(id(number)) sal = 1.0 print(id(sal)) sal = 2.0 print(id(sal)) str1 = 'hello world!' print(id(str1)) str2 = str1.replace('hello','like') print(id((str2))) #可变 list1 = [1,2,3] list2 = list1 list1.append(4) print(id(list1)) print(id(list2)) print(list1) print(list2)
''' 字典类型: 作用: 在{}内,以逗号隔开可存放多个值 以key-value存取,取值速度快 定义: key必须是不可变类型,value可以是任意类型 ''' dict1 = {'age':18,'name':'scy'} print(dict1) print(type(dict1)) #取值,字典名 + [],括号内写值对应的key print(dict1['age']) #按key存取值 dict1['level'] = 9 print(dict1) print(dict1['name']) #成员运算in和not in print('name' in dict1) print('scy' in dict1) print('scy' not in dict1) #删除 del dict1['level'] print(dict1) # 键keys(),值values(),键值对items() # 得到字典中所有keys print(dict1.keys()) # 得到字典中所有values print(dict1.values()) # 得到字典中所有items print(dict1.items()) 循环遍历字典中所有的key for key in dict1: print(key) print(dict1[key]) get dict1 = {'age': 18, 'name': 'scy'} print(dict1.get('age')) #[]取值 print(dict1['sex']) #KeyError: 'sex' #get取值 print(dict1.get('sex')) #若找不到sex,为其设置一个默认值 print(dict1.get('sex','male'))
''' while循环: 语法: while 条件判断: #成立执行此处 逻辑代码 break #跳出本层循环 continue #结束本次循环,进入下一次循环 ''' str1 = 'scy' #while循环 while True: name = input('请输入猜测的字符:').strip() if name == 'scy': print('scy success!') break print('请重新输入! ') #限制循环次数 str1 = 'scy' #初始值 num = 0 #while循环 while num < 3: name = input('请输入猜测的字符:').strip() if name == 'scy': print('scy success! ') break print('请重新输入! ') num += 1
''' 参数一:文件的绝对路径 参数二:mode 操作文件的模式 参数三:encoding 指定的字符编码 f = open('file.txt',mode='wt',encoding='utf-8') f.write('scy') f.close() #追加写文本文件 a = open('file.txt','a',encoding='utf-8') a.write('\n 合肥学院') a.close() ''' ''' 文件处理之上下文管理: with可以管理open打开的文件, 会在with执行完毕后自动调用close()关闭文件 with open() ''' ''' 文件处理之上下文管理 with open() as f "句柄" ''' #写 with open('file1.txt','w',encoding='utf-8') as f: f.write('王朝') #读 with open('file1.txt','r',encoding='utf-8') as f: res = f.read() print(res) #追加 with open('file1.txt','a',encoding='utf-8') as f: f.write('\n 阿鲁巴') 读取相片cxk.jpg with open('cxk.jpg','rb') as f: res = f.read() print(res) jpg = res #把cxk.jpg的二进制流写入cxk_copy.jpg文件中 with open('cxk_copy.jpg','wb') as f_w: f_w.write(jpg) ''' with 管理多个文件 ''' #通过with来管理open打开的两个文件句柄f_r,f_w with open('cxk.jpg','rb') as f_r, open('cxk_copy.jpg','wb') as f_w: res = f_r.read() f_w.write(res)
'''函数 定义: 函数指的其实是一把工具 使用函数的好处: 1.解决代码冗长的问题 2.使代码的结构更清晰 3.易管理 函数的使用必须遵循:先定义,后调用 函数定义的语法: def 函数名(参数一,参数二...): #注释:声明函数 逻辑代码 return 返回值 def:defind 定义 函数名:必须看其名知其意 ():接受外部传入的参数 注释:用来声明函数的作用 return:返回给调用者的值 ''' '''定义函数的三种形式: 1.无参函数 不需要接受外部传入的参数 2.有参函数 需要接受外部传入的参数 3.空函数 pass ''' # 无参函数 def login(): user = input('请输入用户名').strip() pwd = input('请输入密码').strip() if user == 'scy' and pwd == '123': print('login successful!') else: print('login error!') # 函数的内存地址 print(login) # 函数 login() # 有参函数 # username,password用来接受外部传入的值 def login(username,password): user = input('请输入用户名').strip() pwd = input('请输入密码').strip() if user == username and pwd == password: print('login successful!') else: print('login error!') #函数调用 #若函数在定义时需要接受参数,调用者必须为其穿传数 login('scy','123') #空函数 def login(): #代表什么都不做 pass #参数的参数: #在定义阶段:x,y称之为形参 def func(x,y): print(x,y) # #在调用阶段:10,100称之为实参 func(10,100) ''' 位置参数: 位置形参 位置实参 必须按照位置一一传参 ''' #在定义阶段:位置形参 def func(x,y): print(x,y) #在调用阶段:10,100称位置实参 func(10,100) ''' 关键字参数: 关键字实参 按照关键字传参 ''' #位置形参x,y def func(x,y): print(x,y) # #在调用阶段:x=10,y=111称之为关键字参数 func(y=111,x=10) #不能少传 func(y=111) #不能多传 func(y=111,x=10,z='333') ''' 默认参数: 在定义阶段,为参数设置默认值 ''' def foo(x=10,y=20): print(x,y) #不传参,则使用默认参数 foo() #传参,使用传入的参数 foo(200,300) ''' 函数的嵌套定义: 在函数内部定义函数 函数对象: 函数的内存地址称之为函数对象 函数的名称空间: 内置: python解析器自带的都称之为“内置名称空间” 全局: 所有顶着头写的变量、函数。。。都称之为“全名称空间” 局部: 在函数内部定义的,都称之为“局部名称空间” 名称空间加载顺序: 内置--->全局--->局部 名称空间查找顺序: 局部--->全局--->内置 ''' #函数的嵌套定义 def func1(): print('from func1...') def func2(): print('from func2...') #函数对象 print(func1) def f1(): pass def f2(): pass dic1 = {'1': f1, '2': f2} choice = input('请选择功能编号:') if choice == '1': print(dic1[choice]) dic1[choice]() elif choice == '2': print(dic1[choice]) dic1[choice]() x = 10 #名称空间 #函数的嵌套定义 def func1(): # x = 20 print('from func1...') print(x) #x = 30 报错:代码由上自下执行,无法引用x=30 def func2(): print('from func2...') func1()