美文网首页
5.5 公共方法

5.5 公共方法

作者: 拖延症患者10 | 来源:发表于2023-06-14 16:57 被阅读0次
运算符 Python表达式 结果 描述 支持的数据类型
+ [1, 2] + [3, 4] [1, 2, 3, 4] 合并 字符串列表元组
* ['Hi!'] * 2 ['Hi!', 'Hi!'] 复制 字符串列表元组
in 3 in (1, 2, 3) True 元素是否存在 字符串列表元组字典
not in 4 not in (1, 2, 3) True 元素是否不存在 字符串列表元组字典

5.5.1 公有运算符

示例:
  1. 字符串结合运算符 + 的使用
 my_str1 = "hello "
 my_str2 = "world"
  • 合并出来一个新的字符串
 new_str = my_str1 + my_str2
 print(new_str, type(new_str))           # 结果是:hello world <class 'str'>
  1. 列表结合运算符 + 的使用
 my_list1 = [1, 3]
 my_list2 = [2, 4]
  • 合并出来一个新的列表
 new_list = my_list1 + my_list2
 print(new_list, type(new_list))           # 结果是:[1, 3, 2, 4] <class 'list'>
  1. 元组结合运算符 + 的使用
 my_tuple1 = ("a", "b")
 my_tuple2 = ("c",)
  • 合并出来一个新的元组
 new_tuple = my_tuple1 + my_tuple2
 print(new_tuple, type(new_tuple))           # 结果是:('a', 'b', 'c') <class 'tuple'>
  1. 字符串结合运算符 * 的使用
 my_str = "ab"
  • 对字符串进行复制
 new_str = my_str * 3
 print(new_str, type(new_str))           # 结果是:ababab <class 'str'>
  1. 列表结合运算符 * 的使用
 my_list = ["5", "6"]
  • 对列表进行复制
 new_list = my_list * 2
 print(new_list, type(new_list))           # 结果是:['5', '6', '5', '6'] <class 'list'>
  1. 元组结合运算符 * 的使用
 my_tuple = (2, 4)
  • 对元组进行复制
 new_tuple = my_tuple * 2
 print(new_tuple, type(new_tuple))           # 结果是:(2, 4, 2, 4) <class 'tuple'>

5.5.2 公有关键字

示例:
  1. 字符串结合关键字 in 的使用
 my_str = "abcbcd"
 result = "bc" in my_str
 print(result, type(result))           # 结果是:True <class 'bool'>
  1. 字符串结合关键字 not in 的使用
 result = "bd" not in my_str
 print(result, type(result))           # 结果是:True <class 'bool'>
  1. 字典结合关键字 in 的使用:字典结合innot in使用默认判断的是key
 my_dict = {"name": "艾青", "age": 20, "sex": "女", "address": "北京"}
 result = "name" in my_dict
 print(result, type(result))           # 结果是:True <class 'bool'>
  1. 字典结合关键字 not in 的使用
 # 判断”小乔“这个数据值有没有在在字典中的所有 value 值里面
 result = "小乔" in my_dict.values()
 print(result, type(result))           # 结果是:False <class 'bool'>

5.5.3 内置函数

不需要导入模块和导入包直接使用的函数称为内置函数。

函数 描述
len(item) 计算容器中元素个数
max(item) 返回容器中元素最大值
min(item) 返回容器中元素最小值
del(item) 删除变量
示例:
  1. 内置函数len()操作字符串
 my_str = "abc"
 result = len(my_str)
 print(result, type(result))           # 结果是:3 <class 'int'>
  1. 内置函数len()操作range()函数
 my_range = range(1, 5, 2)  # 结尾不包含,步长为 2,结果 1、3
 result = len(my_range)
 print(result, type(result))           # 结果是:2 <class 'int'>
  1. 内置函数len()操作字典:len()在操作字典数据时,返回的是键值对个数。
 my_dict = {"name": "艾青", "age": 20, "sex": "女", "address": "北京"}
 result = len(my_dict)
 print(result, type(result))           # 结果是:4 <class 'int'>
  1. 内置函数max()操作列表
 my_list = [1, 0, 4]
 max_value = max(my_list)
 print(max_value, type(max_value))           # 结果是:4 <class 'int'>
  1. 内置函数min()操作列表
 min_value = min(my_list)
 print(min_value, type(min_value))           # 结果是:0 <class 'int'>
  1. 内置函数del()的使用:变量一旦被删除后续就不能在使用了。
 name = "艾柠"
 print(name)           # 结果是:艾柠
 del(name)
 print(name)           # 结果是:NameError: name 'name' is not defined
  1. 使用del关键字删除变量,del关键字和del()函数的功能一样。
 name = "艾柠"
 print(name)           # 结果是:艾柠
 del name
 print(name)           # 结果是:NameError: name 'name' is not defined

相关文章

网友评论

      本文标题:5.5 公共方法

      本文链接:https://www.haomeiwen.com/subject/chwcydtx.html