day02
2019-06-26 22:31 瑞1234 阅读(150) 评论(0) 收藏 举报一 列表类型:
定义:
在[]内,可以存放多个任意类型的值,并以逗号隔开。
一般用于存放学生的爱好,课堂的周期等等...
#优先掌握的操作:
1、按索引存取值(正向存取+反向存取):即可存也可以取
|
1
|
<span style="font-family: 宋体; font-size: 14px;">print(student_info[-2])</span> |
2、切片(顾头不顾尾,步长)
|
1
|
<span style="font-family: 宋体; font-size: 14px;">print(student_info[0:4:2])</span> |
3、长度
|
1
|
<span style="font-family: 宋体; font-size: 14px;">print(len(student_info))</span> |
4、成员运算in和not in
|
1
2
|
<span style="font-family: 宋体; font-size: 14px;">print('stu1' in student_info)print('stu1' not in student_info)</span> |
5、追加
|
1
2
|
<span style="font-family: 宋体; font-size: 14px;">#student_info = ['stu1',23,'male',['studying','reading']]student_info.append('llllll')</span> |
6、删除
|
1
2
3
|
<span style="font-family: 宋体; font-size: 14px;">del student_info[2] #删除列表中索引为2的值print(students)</span> |
7、循环
|
1
2
|
<span style="font-family: 宋体; font-size: 14px;">for student in student_info: print(student)</span> |
# 需要掌握的:
|
1
|
<span style="font-family: 宋体; font-size: 14px;">student_info2=['stu2',95,'female',['play','water'],95]</span> |
# 1 index 获取列表中某个值的索引
|
1
|
<span style="font-family: 宋体; font-size: 14px;">print(student_info2.index(95))</span> |
# 2 count 获取列表中某个值的数量
|
1
|
<span style="font-family: 宋体; font-size: 14px;">print(student_info2.count(95))</span> |
# 3 取值,默认取列表中的最后一个值,类似删除
#若pop()括号中写了索引,则取索引对应的值
|
1
2
3
4
5
6
|
<span style="font-family: 宋体; font-size: 14px;">student_info2.pop()print(student_info2)#取出列表中索引为2的值,并赋值给sex变量名sex = student_info2.pop()print(sex)print(student_info)</span> |
# 4 移除,把列表中的某个值的第一个值移除
|
1
2
3
4
5
6
7
|
<span style="font-family: 宋体; font-size: 14px;">student_info2=['stu2',95,'female',['play','water'],95]student_info2.remove(95)#搜索 95 ,并删除第一个95print(student_info2)name = student_info2.remove('stu2')print(name)#Noneprint(student_info2) </span> |
# 5 插入值
|
1
2
3
|
<span style="font-family: 宋体; font-size: 14px;">student_info2=['stu2',95,'female',['play','water'],95]student_info2.insert(3,'school')print(student_info2)</span> |
# 6 extend 合并列表
|
1
2
3
4
5
|
<span style="font-family: 宋体; font-size: 14px;">student_info = ['stu1',23,'male',['studying','reading']]student_info2=['stu2',95,'female',['play','water'],95]student_info.extend(student_info2)print(student_info)</span> |
二 元组类型:
定义: 在()内,可以存放多个任意类型的值,并以逗号隔开。
注意:
元组与列表不一样的是,只能在定义时初始化值,不能对其进行修改。
优点:
在内存中占用的资源比列表要小。
# 优先掌握的操作
1 按索引存取值(正向存取+反向存取):可存可取
2 切片(顾头不顾尾,步长)# 从 0 开始切片到 5-1 ,步长为2
3 长度
4 成员运算in和not in
5 循环
for line in tuple1:
#print(line)
#print默认end参数是\n
print(line,end='_')
三 字典类型:
作用:
在{}内,可存放多个值,以key-value存取,取值速度快。
定义:
key必须是不可变类型,value可以是任意类型
#优先掌握的操作
1 按key存取值:可存可取
2 长度len
3 成员运算in和not in,只判断字典中的key
4 删除
5 键keys(),值value(),键值对items()
6 循环
|
1
2
3
4
|
<span style="font-family: 宋体; font-size: 14px;"># #循环遍历字典中所有的keyfor key in dict: print(key) print(dict[key])<em id="__mceDel"><em id="__mceDel"><em id="__mceDel"><em id="__mceDel"><br></em></em></em></em></span> |
#字典取值另一种方法 get
|
1
2
3
4
5
6
7
8
|
<span style="font-family: 宋体; font-size: 14px;">#dict=...print(dict.get('age'))#[]取值print(dict.get(['sex']))#get取值print(dict.get('sex'))# #若找不到sex,为其设置一个默认值print(dict.get('sex','male'))</span> |
四 函数
什么是函数?
函数指的其实是一把工具
使用函数的好处:
1.解决代码冗余问题
2.使代码的结构更清晰
3.易管理
函数的使用必须遵循:先定义,后调用。
函数的定义语法:
def 函数名(参数1,参数2 ...): #注释:声明函数
逻辑代码
return 返回值
def:defind 定义
函数名:必须看其名知其意
():接收外部传入的参数
注释:用来声明函数的作用
return:返回给调用者的值
1、函数的参数# 在定义阶段:x,y称之为形参
|
1
2
3
4
|
<span style="font-family: 宋体; font-size: 14px;">def func(x,y):#x,y print(x,y)# 在调用阶段:10,100称之为实参func(10,100)</span> |
2、位置参数
|
1
2
3
4
5
6
|
<span style="font-family: 宋体; font-size: 14px;">#在定义阶段:位置形参def func(x,y):#x,y print(x,y)# 在调用阶段:10,100称之为 位置实参func(10,100)</span> |
3、关键字参数:只有关键字实参
|
1
2
3
4
5
6
7
8
9
10
|
<span style="font-family: 宋体; font-size: 14px;">#位置形参x,ydef func(x,y): print(x,y)# 在调用阶段:x=10,y=100称之为 关键字参数func(y=10,x=100) #100 10# 不能少传func(x=100) #报错TypeError# 不能多传func(y=10,x=100,z=34)#报错TypeError</span> |
4、默认参数:#在定义阶段,为参数设置默认值
|
1
2
3
4
5
6
7
|
<span style="font-family: 宋体; font-size: 14px;">def foo(x=10,y=20): print(x,y)# 不传参,则使用默认参数foo()# 传参,使用传入的参数foo(200,300)</span> |
5、函数的嵌套定义
在函数内部定义函数
·函数对象:
-函数的内存地址称之为函数对象
·函数的名称空间:
-内置:
python解析器自带的都称之为“内置名称空间”
-全局:
所有顶着头写的变量、函数...都称之为“全名称空间”
-局部:
在函数内部定义的,都称之为“局部名称空间”
-名称空间加载顺序:
内置--->全局--->局部
-名称空间查找顺序:
局部--->全局--->内置
五 文件处理
·文件处理:
open()
写文件:
wt:写文本
读文本:
rt:读文本
追加写文件:
at:追加文本
注意:必须指定字符编码,以什么方式写,就要以什么方式打开
执行python代码的过程:
1.先启动python解释器,加载到内存中。
2.把写好的python文件加载到解释器中。
3.检测python语法,执行代码
SyntaxError:语法错误!
打开文件会产生两种资源:
1.python程序
2.操作系统打开文件
文件处理之上下文管理
# with可以管理open打开的文件
会在with执行完毕后自动调用close()关闭文件
浙公网安备 33010602011771号