标签:start step for false inf 介绍 方法 tar range
1 str1 = ‘aa‘ 2 str2 = ‘bb‘ 3 4 list1 = [1, 2] 5 list2 = [10, 20] 6 7 t1 = (1, 2) 8 t2 = (10, 20) 9 10 dict1 = {‘name‘: ‘Python‘} 11 dict2 = {‘age‘: 30} 12 13 # +: 合并 14 print(str1 + str2) 15 print(list1 + list2) 16 print(t1 + t2) 17 18 # print(dict1 + dict2) # 报错:字典不支持合并运算
操作结果如下:
1 aabb 2 [1, 2, 10, 20] 3 (1, 2, 10, 20)
1 str1 = ‘a‘ 2 list1 = [‘hello‘] 3 t1 = (‘world‘,) 4 5 # *:复制 6 print(str1 * 5) 7 8 # 打印10个-: 9 print(‘-‘ * 10) 10 11 print(list1 * 5) 12 13 print(t1 * 5)
操作结果如下:
1 aaaaa 2 ---------- 3 [‘hello‘, ‘hello‘, ‘hello‘, ‘hello‘, ‘hello‘] 4 (‘world‘, ‘world‘, ‘world‘, ‘world‘, ‘world‘)
1 str1 = ‘abcd‘ 2 list1 = [10, 20, 30, 40] 3 t1 = (100, 200, 300, 400) 4 dict1 = {‘name‘: ‘Python‘, ‘age‘: 30} 5 6 # in 和 not in 7 # 1. 字符a是否存在 8 print(‘a‘ in str1) 9 print(‘a‘ not in str1) 10 11 # 2. 数据10是否存在 12 print(10 in list1) 13 print(10 not in list1) 14 15 # 3. 100是否存在 16 print(100 not in t1) 17 print(100 in t1) 18 19 # 4. name是否存在 20 print(‘name‘ in dict1) 21 print(‘name‘ not in dict1) 22 print(‘name‘ in dict1.keys()) 23 print(‘name‘ in dict1.values())
操作结果如下:
1 True 2 False 3 True 4 False 5 False 6 True 7 True 8 False 9 True 10 False
1 str1 = ‘abcdefg‘ 2 list1 = [10, 20, 30, 40, 50] 3 t1 = (10, 20, 30, 40, 50) 4 s1 = {10, 20, 30, 40, 50} 5 dict1 = {‘name‘: ‘TOM‘, ‘age‘: 18} 6 print(len(str1)) 7 print(len(list1)) 8 print(len(t1)) 9 print(len(s1)) 10 print(len(dict1))
操作结果如下:
1 7 2 5 3 5 4 5 5 2
1 str1 = ‘abcdefg‘ 2 list1 = [10, 20, 30, 40, 50] 3 t1 = (10, 20, 30, 40, 50) 4 s1 = {10, 20, 30, 40, 50} 5 dict1 = {‘name‘: ‘TOM‘, ‘age‘: 18} 6 7 # del 目标 或del(目标) 8 #del str 9 #print(str1) str不存在,报错 10 11 # del(list1) 12 print(list1) 13 del(list1[0]) 14 print(list1) 15 16 #del s1 17 #print(s1) 18 19 #del dict1 20 #print(dict1) 21 del dict1[‘name‘] 22 print(dict1)
1 str1 = ‘abcdefg‘ 2 list1 = [10, 20, 30, 40, 50] 3 4 # max() : 最大值 5 # print(max(str1)) 6 # print(max(list1)) 7 8 # min() : 最小值 9 print(min(str1)) 10 print(min(list1))
1 # range(start, end, step) 2 # print(range(1, 10, 1)) 3 # for i in range(1, 10, 1): 4 # print(i) 5 6 # for i in range(1, 10): 7 # print(i) 8 9 # for i in range(1, 10, 2): 10 # print(i) 11 12 13 for i in range(10): 14 print(i) 15 16 # 1. 如果不写开始,默认从0开始 17 # 2. 如果不写步长,默认为1
1 list1 = [‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘] 2 3 # enumerate 返回结果是元组,元组第一个数据是原迭代对象的数据对应的下标,元组第二个数据是原迭代对象的数据 4 # for i in enumerate(list1): 5 # print(i) 6 7 for i in enumerate(list1, start=2): 8 print(i)
操作结果如下:
1 (2, ‘a‘) 2 (3, ‘b‘) 3 (4, ‘c‘) 4 (5, ‘d‘) 5 (6, ‘e‘)
标签:start step for false inf 介绍 方法 tar range
原文地址:https://www.cnblogs.com/lzy820260594/p/11792946.html