Python笔记--变量
根据《Python编程入门经典》(James Payne著,张春晖译)整理。
元组--tuple
不可更改的数据序列(可以包含多种数据类型)。元组写在小括号里,元素之间用逗号隔开。定义只有一个元素的元组时,必须加逗号(为了避免与其他类型混淆)。
>>> filter = ("string", "filled", "by a", "tuple") >>> print(filter) ('string', 'filled', 'by a', 'tuple')
>>> type((50,)) <class 'tuple'> >>> type((50)) <class 'int'>
访问元素:使用下标
>>> filter[1] 'filled'
获取长度:len()
>>> len(filter)
4
引用:一个元组可以引用另一个元组
>>> b = ("aaa", 123) >>> a = (b, "lll") # a[0]是一个元组 >>> a (('aaa', 123), 'lll')
在引用其他元组的情况下的访问:
>>> a[0] ('aaa', 123) >>> a[0][0] 'aaa' >>> a[0][1] 123
删除:del
>>> a ('a', 'ab', 123) >>> del a >>> a # a被删除了,无法访问 Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'a' is not defined
切片:tuple[start:stop:step],start省略不写就是0,stop省略不写就是最后一个元素的下标,step省略不写就是1
>>> a = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) >>> a[::] (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) >>> a[::2] (1, 3, 5, 7, 9) >>> a[4::2] (5, 7, 9)
最大值:max()
>>> a = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) >>> max(a) 10
最小值:min()
>>> a = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) >>> min(a) 1
列表--list
可以更改的数据序列。列表用方括号创建。元组和列表的主要区别在于,元组创建后不可以修改,列表创建后可以修改。
>>> breakfast = ["coffee", "tea", "toast", "egg"]
访问元素:下标
>>> breakfast[0] 'coffee' >>> breakfast[1] 'tea' >>> breakfast[2] 'toast' >>> breakfast[3] 'egg'
修改元素:下标
>>> breakfast[0] = "milk" >>> breakfast ['milk', 'tea', 'toast', 'egg']
末端添加一个元素:append
>>> breakfast.append("waffles") >>> breakfast ['milk', 'tea', 'toast', 'egg', 'waffles']
末端添加多个元素:extend
>>> breakfast.extend(["juice", "decaf", "oatmeal"]) >>> breakfast ['milk', 'tea', 'toast', 'egg', 'waffles', 'juice', 'decaf', 'oatmeal']
长度:len
>>> len(breakfast)
8
插入:insert
>>> breakfast.insert(1, "beef") >>> breakfast
删除:remove
>>> breakfast.remove("beef") >>> breakfast ['milk', 'tea', 'toast', 'egg', 'waffles', 'juice', 'decaf', 'oatmeal']
删除:下标
>>> del breakfast[0] >>> breakfast ['tea', 'toast', 'egg', 'waffles', 'juice', 'decaf', 'oatmeal']
排序:sort
>>> breakfast.sort() >>> breakfast ['decaf', 'egg', 'juice', 'oatmeal', 'tea', 'toast', 'waffles']
反转:reverse
>>> breakfast ['decaf', 'egg', 'juice', 'oatmeal', 'tea', 'toast', 'waffles'] >>> breakfast.reverse() >>> breakfast ['waffles', 'toast', 'tea', 'oatmeal', 'juice', 'egg', 'decaf']
字典--dict
以名称索引的分组数据。字典由花括号创建。字典中的一个元素由键和键值组成(key:value)。字典不允许有完全相同的两个索引名(键),但是可以由相同的键值。
>>> menus_specials = {} # 空字典 >>> menus_specials["breakfast"] = "Canadian ham" # breakfast是索引名, Canadian ham是其对应的值 >>> menus_specials["lunch"] = "tuna surprise" >>> menus_specials["dinner"] = "Cheeseburger Deluxe" >>> menus_specials {'breakfast': 'Canadian ham', 'lunch': 'tuna surprise', 'dinner': 'Cheeseburger Deluxe'}
输出:使用索引名输出值
>>> menus_specials["breakfast"] 'Canadian ham'
获取索引和键值:keys, values
>>> menus_specials.keys() dict_keys(['breakfast', 'lunch', 'dinner']) >>> menus_specials.values() dict_values(['Canadian ham', 'tuna surprise', 'Cheeseburger Deluxe'])
获取所有的项:items
>>> menus_specials.items() dict_items([('breakfast', 'Canadian ham'), ('lunch', 'tuna surprise'), ('dinner', 'Cheeseburger Deluxe')])
删除:通过key删除,del
>>> del menus_specials["breakfast"] >>> menus_specials {'lunch': 'tuna surprise', 'dinner': 'Cheeseburger Deluxe'}
合并:update
>>> sports = {} >>> sports["walking"] = 2 >>> menus_specials.update(sports) >>> menus_specials {'lunch': 'tuna surprise', 'dinner': 'Cheeseburger Deluxe', 'walking': 2}
清除元素:clear
>>> menus_specials.clear() >>> menus_specials {}

浙公网安备 33010602011771号