一、模块
01.概念
02.第一个模块体验
03.模块名也是一个标识符
04.Pyc文件(了解)
二、目标:
①列表
②元组
③字典
④字符串
⑤公共方法
⑥高级变量
三、列表
01定义
注意:从列表中取值时,去过超出索引范围,程序会出错
02列表存储方式
03列表常用操作
04从列表中取值和索引
name_list = ["张三", "李四", "王五"]
name_list[1] = "lisi"
print(name_list)
05向列表增加数据
name_list = ["张三", "李四", "王五"]
name_list.append("王小二")
name_list.insert(1, "小红")
temp_list = ["孙悟空", "猪八戒", "沙师弟"]
name_list.extend(temp_list)
print(name_list)
06从列表删除数据
name_list = ["张三", "李四", "王五"]
name_list.append("王小二")
name_list.insert(1, "小红")
name_list.remove("王小二")
name_list.pop()
name_list.pop(1)
print(name_list)
name_list = ["张三", "李四", "王五"]
name_list.clear()
print(name_list
07del关键字
name_list = ["张三", "李四", "王五"]
del name_list[1]
name = "迪加"
del name
print(name_list)
08列表统计及删除方法扩展
name_list = ["张三", "李四", "张三", "王五"]
list_len = len(name_list)
print("列表中包含%d个元素" % list_len)
count = name_list.count("张三")
print("张三出现了%d次" % count)
name_list.remove("张三")
print(name_list)
09列表的排序和反转
name_list = ["zhangsan", "lisi", "zhangsan", "wangwu"]
num_list = [6, 8, 4, 1, 10]
name_list.reverse()
num_list.reverse()
print(name_list)
print(num_list)
10关键字、函数和方法
11循环遍历
name_list = ["zhangsan", "lisi", "zhangsan", "wangwu"]
for my_name in name_list:
print("我的名字叫 %s" % my_name)
12列表的应用场景
四、元组
01元组的定义、特点
02元组变量的常用操作
info_tuple = ("zhangsan", 18, 1.75, "zhangsan")
print(info_tuple[0])
print(info_tuple.index("zhangsan"))
print(info_tuple.count("zhangsan"))
print(len(info_tuple))
03元组变量的循环变量
info_tuple = ("zhangsan", 18, 1.75, "zhangsan")
for my_info in info_tuple:
print(my_info)
03元组的应用场景
04元组和格式化字符串
info_tuple = ("小明", 18, 175)
print("%s 年龄是 %d 身高是 %d" % info_tuple)
info_str = "%s 年龄是 %d 身高是 %d" % info_tuple
print(info_str)
05元组和列表之间的转换
|