文章预览
lists-列表操作 列表索引的某个项目可以重新赋值 举例: nums = [8, 8, 8, 8] nums[3] =6 print(nums) Result: >>> [8, 8, 8, 6] >>> 填空 创建一个列表list, 重新赋值第二个元素并打印 nums = [34, 45, 65] nums[1] = 22 print(nums) [34, 22, 65] list-列表乘 & 加 列表list 可以添加也可以相乘,字符串也一样 举个栗子: nums = [2, 2, 4] print(nums + [2, 3, 4]) print(nums * 2) 结果: >>> [2, 2, 4, 2, 3, 4] [2, 2, 4,2, 2, 4 ] >>> 列表和字符串在很多方面都是相似的——字符串可以被看作是无法更改的字符列表 List-检测-in 用in语句来检查列表中是否有某个项目,如果返回 True 则有,返回False则没有 words = ["spam", "egg", "spam", "sausage"] print("spam" in words) print("egg" in words) print("tomato" in words) 结果: >>> True True False >>> in运算符还用于确定字符串是否是另一个字符串的子字符串。 填空 这段代码的运行结果是多少? nums = [10, 9, 8,
………………………………