元组(Tuple)
● Tuple(元组)与列表类似,不同之处在于元组的元素不能修改
● 用于存储一串信息,数据之间使用 , 分隔
● 元组用()定义
● 元组的索引从 0 开始
● 元组中 只包含一个元素 时,需要 在元素后面添加逗号
info_tuple = (50, )
元组应用场景:
1.函数的参数和返回值,一个函数可以接收任意多个参数,或者一次返回多个数据
2.格式化字符串,格式化字符串后面的()本质上就是一个元组
3.让列表不可以被修改,以保护数据安全
元组和列表之间的转换:
● 使用 list 函数可以把元组转换成列表
list (元组)
● 使用 tuple 函数可以把列表转换成元组
tuple(列表)
字典
● dictionary(字典) 时除列表以外 python 之中 最灵活 的数据类型
● 字典同样可以用来存储多个数据
● 和列表的区别:列表 是 有序 的对象集合 , 字典 是 无序 的对象集合
● 字典用 {}定义
● 字典使用 键值对 存储数据,键值对之间使用 ,分隔
◦ 键 key 是索引
◦ 值 value 是数据
◦ 键和值 之间使用:分隔
◦ 键必须是唯一的
◦ 值可以取任何数据类型,但 键 只能使用 字符串,数字或元组
# 字典是一个无序的数据结合,使用print函数输出字典时,
# 通常输出的顺序和定义的顺序是不一致的!
xiaoming = {"name":"小明",
"age:":18,
"gender":True}
print(xiaoming)
● 字典的基本使用
xiaoming_dict = {"name":"小明"}
# 1.取值
print(xiaoming_dict["name"])
# 2.增加/修改
xiaoming_dict["age"] = 18
xiaoming_dict["name"] = "小晓明"
# 3.删除
xiaoming_dict.pop("name")
print(xiaoming_dict)
● 字典常用操作
xiaoming_dict = {"name":"小明",
"age:":18,}
# 1.统计键值对数量
print(len(xiaoming_dict))
# 2.合并字典
temp_dict = {"height":1.75,
"age":20}
# 注意:如果被合并的字典中包含已经存在的键值对,会覆盖原有的键值对
xiaoming_dict.update(temp_dict)
# 3.清空字典
xiaoming_dict.clear()
print(xiaoming_dict)
● 字典的遍历
xiaoming_dict = {"name":"小明",
"age":18}
# 迭代遍历字典
# 变量K是每一次循环中,获取到的键值对的key
for k in xiaoming_dict:
print("%s-%s"%(k,xiaoming_dict[k]))
● 应用场景
1.使用多个键值对,存储描述一个物体的相关信息——描述更复杂的数据信息
2.将多个字典放在一个列表中,再进行遍历,在循环体内部针对每一个字典进行相同的处理
字符串
● 在python中可以使用 一对双引号 “ 或者 一对单引号 ’ 定义一个字符串
(大多数编程语言都是用 ” 来定义字符串)
● 可以使用 索引 获取一个字符串中 指定位置的字符,索引计数从0开始
● 也可以使用 for 循环遍历 字符串中每一个字符
1.字符串统计操作
hello_str = "hello hello"
# 1.统计字符串长度
print(len(hello_str))
# 2.统计某一个小(子)字符出现的次数
print(hello_str.count("llo"))
print(hello_str.count("abc"))
# 3.某一个字符串出现的位置
print(hello_str.index("llo"))
2.判断类型
判断空白符,数字:
| 方法 | 说明 |
|---|---|
| string.isspace() | 如果string中只包含空格或空白字符,则返回True |
| string.isdecimal() | 如果string只包含数字则返回True,全角数字 |
| string.isdigit() | 如果string只包含数字则返回True,全角数字,(1),\u00b2 |
| string.isnumeric() | 如果string只包含数字则返回True,全角数字,汉字数字 |
查找和替换:
| 方法 | 说明 |
|---|---|
| string.startswith(str) | 检查字符串是否是以str开头,是则返回True |
| string.endswith(str) | 检查字符串是否是以str结束,是则返回True |
| string.find(str,start=0,end=len(string)) | 检测str是否包含在string中,如果start和end指定范围,则检查是否包含在指定范围,如果是返回开始的索引值,否则返回-1 |
| string.index(str,start=0,end=len(string)) | 跟find()方法类似,只不过如果str不在string会报错 |
| string.replace(old_str,new_str,num=string.count(old)) | 把string中的old_str替换成new_str,如果num指定,则替换不超过num次 |
hello_str = "hello world"
# 1.判断是否以指定字符串开始
print(hello_str.startswith("hello"))
# 2.判断是否以指定字符串结束
print(hello_str.endswith("world"))
# 3.查找指定字符串
# index同样可以查找指定的字符串在大字符串中的索引
print(hello_str.find("llo"))
# index如果指定的字符串不存在,会报错
# find如果指定的字符串不存在,会返回-1
print(hello_str.find("abc"))
# 4.替换字符串
# replace方法执行完成后,会返回一个新的字符串
# 注意:不会修改原有字符串的内容
print(hello_str.replace("world","python"))
print(hello_str)
文本对齐:
| 方法 | 说明 |
|---|---|
| string.ljust(width) | 返回一个原字符串左对齐,并使用空格填充至长度width的新字符串 |
| string.rjust(width) | 返回一个原字符串右对齐,并使用空格填充至长度width的新字符串 |
| center(width) | 返回一个原字符串居中,并使用空格填充至长度width的新字符串 |
去除空白符:
| 方法 | 说明 |
|---|---|
| string.lstrip() | 截掉string左边(开始)的空白字符 |
| string.rstrip() | 截掉string右边(开始)的空白字符 |
| string.strip() | 截掉string左右两边的空白字符 |
拆分和连接:
| 方法 | 说明 |
|---|---|
| string.split(str="",num) | 以str为分隔符拆分string,如果num有指定值,则仅分隔 num+1个子字符串,str默认包含\r,\n,\t和空格 |
| string.join(seq) | 以string作为分隔符,将seq中所有的元素(的字符串表示)合并为一个新的字符串 |
3.字符串的切片
● 切片方法适用于 字符串,列表,元组
● 切片使用索引值来限定范围,从一个大的字符串中切出小的字符串
● 字符串[开始索引:结束索引:步长]
字符串的切片演练:
>>> num_str = "0123456789"
>>> num_str[2:6]
'2345'
>>> num_str[2:]
'23456789'
>>> num_str[0:6]
'012345'
>>> num_str[:6]
'012345'
>>> num_str[:]
'0123456789'
>>> num_str[::2]
'02468'
>>> num_str[1::2]
'13579'
>>> num_str[2:-1]
'2345678'
>>> num_str[-2:]
'89'
>>> num_str[0::-1]
'0'
>>> num_str[-1::-1]
'9876543210'
>>> num_str[-1:-3:-1]
'98'
>>> num_str[::-1]
'9876543210'
>>>