Python基本语法
Python基本数据类型
Python数据类型 | |
基本的数据类型 | 举例 |
数字类型(Numbers) | 1, 3.14, 1.23e9, 1+2j, |
字符串类型(String) | 'abc', "abc" |
布尔类型(Boolean) | True, False |
列表(List) | [1, 2, 'abc'] |
元组(Tuple) | (1, 2, 'abc') |
集合(Set) | {1, 2, 3} |
字典(Dict) | {'name': 'xxx', 'age': 29} |
空类型 | None |
列表数据的常用方法
列表中的元素可以是不同类型的. 有一些常用的方法总结如下:
列表数据的常用方法 |
|
直接赋值 |
>>>classmates=['xiao', 'hong', 'zhang', 'hua', True, 1] >>>classmates ['xiao', 'hong', 'zhang', 'hua', True, 1] |
求长度 |
>>>len(classmates) 6 |
索引访问 |
>>>classmates[0] 'xiao' >>>classmates[-1] 1 >>>classmates[0:3] ['xiao', 'hong', 'zhang'] >>>classmates[-3:-1] ['hua', True] |
添加元素 |
>>>classmates.append('new value') >>>classmates ['xiao', 'hong', 'zhang', 'hua', True, 1, 'new value'] |
插入元素 |
>>>classmates.insert(1, 'insert value') >>>classmates ['xiao', 'insert value', 'hong', 'zhang', 'hua', True, 1, 'new value'] |
删除元素 |
>>>classmates.pop() 'new value' >>>classmates.pop(1) 'insert value' >>>classmates ['xiao', 'hong', 'zhang', 'hua', True, 1] |
列表的遍历:
>>> classmates ['xiao', 'hong', 'zhang', 'hua', True, 1] # 方法1, 直接遍历获取值 >>> for value in classmates: ... print(value) ... xiao hong zhang hua True 1 # 方法2, 先获取索引然后获取值 >>> for index in range(len(classmates)): ... print(index, '-->', classmates[index]) ... 0 --> 'xiao' 1 --> 'hong' 2 --> 'zhang' 3 --> 'hua' 4 --> True 5 --> 1 # 方法3, 利用enumerate方法获取索引和值 >>> for index, value in enumerate(classmates): ... print(index, '-->', value) ... 0 --> 'xiao' 1 --> 'hong' 2 --> 'zhang' 3 --> 'hua' 4 --> True 5 --> 1
元组数据的常用方法
元组的常用方法和上述的列表差不多, 不同的是元组的元素一旦初始化以后就不能修改了, 这里的不能修改是指元素的指向不能修改。 示例如下:
tuple1 = (1, 'abc', [1,2])
上述示例中, 元组tuple1有三个元素, 分别指向了1, 'abc', 和[1,2]。
对元组来说,不能进行如下的修改(因为会改变元素的指向):
>>>tuple1[0] = 2 # 不能这样修改, 因为会改变第0个元素的指向, 使得他指向了一个新的对象2 Traceback (most recent call last): File "<stdin>", line1, in <module> TypeError: 'type' object does not support item assignment
但可以进行如下的修改(不改变元素的指向):
>>> tuple1[2][0] = 'new value' # 第二个元素的指向任然是该列表, 没有改变, 改变的只是列表内部的值 >>> tuple1 (1, 'abc', ['new value', 2])
字典数据的常用方法
字典数据常用方法 | |
直接赋值 |
>>> d = {'michel': 93, 'bob': 77, 'tracy': 80} >>> d {'bob': 77, 'michel': 93, 'tracy': 80} |
新增元素, 获取与修改元素 |
>>> d['michel'] = 100 >>> d['adam'] = 60 >>> d {'bob': 77, 'michel': 100, 'adam': 60, 'tracy': 80} |
判断key值 |
>>> 'bob' in d # 用in来判断是否有这个key True >>> d.get('xiao', -1) # 用get方法来获取key对应的值, 如果没有key, 则返回-1 -1 |
删除元素 |
>>> d.pop('bob') 77 >>> d {'michel': 100, 'adam': 60, 'tracy': 80} |
字典的遍历:
# keys的遍历 >>> for key in d: ... print(key) ... michel adam tracy >>> for key in d.keys(): ... print(key) ... michel adam tracy # values的遍历 >>> for value in d.values(): ... print(value) ... 100 60 80 # key 和 values的遍历 >>> for key, value in d.items(): ... print(key, '-->', value) ... michel --> 100 adam --> 60 tracy --> 80
集合数据的常用方法
集合和字典类似, 是一组key的集合, 但不存储value值. 在集合set中, 元素是不能重复的.
集合常用操作 | |
创建集合 |
>>> s = set([1,1,1,2,3]) >>> s {1,2,3} |
添加元素 |
>>> s.add(4) >>> s {1,2,3,4} |
删除元素 |
>>> s.remove(4) >>> s {1,2,3} |
交集, 并集等 |
>>> s1 = set([1,2,3]) >>> s2 = set([2,3,4]) >>> s1 & s2 {2,3} >>> s1 | s2 {1,2,3,4} |
不可变对象
https://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/00143167793538255adf33371774853a0ef943280573f4d000
Python中的常用内置方法
MetaClass
装饰器
参考链接:
https://foofish.net/python-decorator.html