今日内容:
1.常用数据类型及内置方法
2.文件处理
3.函数
列表类型:
定义:
在[]内,可以存放多个任意类型的值,并以逗号隔开。
一般用于存放学生的爱好,课堂的周期等等...
# list(['钱垚', '李小龙', '张全蛋', '赵铁柱'])
# students = ['钱垚', '李小龙', '张全蛋', '赵铁柱']
# print(students[1]) # 李小龙
#
# student_info = ['杨波', 84, 'male', ['泡8', '喝9']]
# # 取杨波同学的所有爱好
# print(student_info[3])
# # # 取杨波同学的第二个爱好
# print(student_info[3][1])
优先掌握的操作:
# # 1、按索引存取值(正向存取+反向存取):即可存也可以取
# print(student_info[-2]) # 杨波
#
# # 2、切片(顾头不顾尾,步长)
# print(student_info[0:4:2]) # ['杨波', 'male']
#
# # 3、长度
# print(len(student_info)) # 4
#
# # 4、成员运算in和not in
# print('杨波' in student_info) # True
# print('杨波' not in student_info) # False
#
# # 5、追加
# student_info = ['杨波', 84, 'male', ['泡8', '喝9']]
# # 在student_info列表末尾追加一个值
# student_info.append('安徽最牛的学员,合肥学院')
# print(student_info)
#
# # 6、删除
# # 删除列表中索引为2的值
# del student_info[2]
# print(student_info)
# 7、循环
# for student in student_info:
# print(student)
需要掌握的:
# student_info = ['尹浩卿', 95, 'female', ['尬舞', '喊麦'], 95]
# # 1.index 获取列表中某个值的索引
# print(student_info.index(95)) # 1
#
# # 2.count 获取列表中某个值的数量
# print(student_info.count(95)) # 2
#
# # 3.取值,默认取列表中最后一个值,类似删除
# # 若pop()括号中写了索引,则取索引对应的值
# student_info.pop()
# print(student_info)
# # 取出列表中索引为2的值,并赋值给sex变量名
# sex = student_info.pop(2)
# print(sex)
# print(student_info)
# student_info = ['尹浩卿', 95, 'female', ['尬舞', '喊麦'], 95]
#
# # 4.移除,把列表中的某个值的第一个值移除
# student_info.remove(95)
# print(student_info) # ['尹浩卿', 'female', ['尬舞', '喊麦'], 95]
#
# name = student_info.remove('尹浩卿')
# print(name) # None
# print(student_info) # ['female', ['尬舞', '喊麦'], 95]
# 5.插入值
# student_info = ['尹浩卿', 95, 'female', ['尬舞', '喊麦'], 95]
# # # 在student_info中,索引为3的位置插入“合肥学院”
# # student_info.insert(3, '合肥学院')
# # print(student_info)
# 6.extend 合并列表
# student_info1 = ['尹浩卿', 95, 'female', ['尬舞1', '喊麦2'], 95]
# student_info2 = ['娄逸夫', 94, 'female', ['尬舞1', '喊麦2']]
# # 把student_info2所有的值插入student_info1内
# student_info1.extend(student_info2)
# print(student_info1)
元组类型:
定义:
在()内,可以存放多个任意类型的值,并以逗号隔开。
注意:
元组与列表不一样的是,只能在定义时初始化值,不能对其进行修改。
优点:
在内存中占用的资源比列表要小。
# 定义:
# tuple((1, 2, 3, '五', '六'))
tuple1 = (1, 2, 3, '五', '六')
print(tuple1) # (1, 2, 3, '五', '六')
# 优先掌握的操作:
# 1、按索引取值(正向取+反向取):只能取
print(tuple1[2]) # 3
# 2、切片(顾头不顾尾,步长)
# 从0开始切片到5-1, 步长为3
print(tuple1[0:5:3]) # (1, '五')
# 3、长度
print(len(tuple1)) # 5
# 4、成员运算in和not in
print(1 in tuple1) # True
print(1 not in tuple1) # False
# 5、循环
for line in tuple1:
# print(line)
# print默认end参数是\n
print(line, end='_')
''''''
'''
不可变类型:
变量的值修改后,内存地址一定不一样。
数字类型
int
float
字符串类型
# str
#
# 元组类型
# tuple
#
# 可变类型:
# 列表类型
# list
#
# 字典类型
# dict
#
# '''
# # 不可变类型
# # int
# number = 100
# print(id(number)) # 1434810944
# number = 111
# print(id(number)) # 1434811296
#
# # float
# sal = 1.0
# print(id(sal)) # 2771339842064
# sal = 2.0
# print(id(sal)) # 2771339841896
#
# str1 = 'hello python!'
# print(id(str1)) # 1975751484528
# str2 = str1.replace('hello', 'like')
# print(id(str2)) # 1975751484400
# 可变类型:
# 列表
list1 = [1, 2, 3]
list2 = list1
list1.append(4)
# list1与list2指向的是同一份内存地址
print(id(list1))
print(id(list2))
print(list1)
print(list2)
字典类型:
作用:
在{}内,可存放多个值,以key-value存取,取值速度快。
定义:
key必须是不可变类型,value可以是任意类型
# dict1 = dict({'age': 18, 'name': 'tank'})
# dict1 = {'age': 18, 'name': 'tank'}
# print(dict1) # {'age': 18, 'name': 'tank'}
# print(type(dict1)) # <class 'dict'>
# 取值,字典名 + [],括号内写值对应的key
# print(dict1['age'])
# 优先掌握的操作:
# 1、按key存取值:可存可取
# 存一个 level: 9的值到dict1字典中
# dict1['level'] = 9
# print(dict1) # {'age': 18, 'name': 'tank', 'level': 9}
# print(dict1['name']) # tank
#
# # 2、长度len
#
# # 3、成员运算in和not in 只判断字典中的key
# print('name' in dict1) # True
# print('tank' in dict1) # False
# print('tank' not in dict1) # True
#
# # 4、删除
# del dict1['level']
# print(dict1) # {'age': 18, 'name': 'tank'}
#
# # 5、键keys(),值values(),键值对items()
# # 得到字典中所有key
# print(dict1.keys())
# # 得到字典中所有值values
# print(dict1.values())
# # 得到字典中所有items
# print(dict1.items())
# 6、循环
# 循环遍历字典中所有的key
# for key in dict1:
# print(key)
# print(dict1[key])
# get
dict1 = {'age': 18, 'name': 'tank'}
# print(dict1.get('age'))
# []取值
# print(dict1['sex']) # KeyError: 'sex'
# get取值
print(dict1.get('sex')) # None
# 若找不到sex,为其设置一个默认值
print(dict1.get('sex', 'male'))
'''
'''
转载于:https://www.cnblogs.com/xuwangzai/p/11094575.html