數(shù)據(jù)類型
- Numbers (數(shù)字)
- String (字符串)
- List (列表)
- Tuple (元組)
- Sets(集合)
- Dictionaries (字典)
變量
python 中變量不需要被聲明,每個變量在使用前必須賦值,變量賦值以后該變量才會被創(chuàng)建
Numbers
- 數(shù)字類型支持 int、float、bool、complex(復(fù)數(shù))
>>> a, b, c, d = 1, 1.2, True, 1+2j
>>> print(type(a), type(b), type(c), type(d))
<class 'int'> <class 'float'> <class 'bool'> <class 'complex'>
- 從上面的例子可以看出 python 可以支持多個變量同時賦值
- 通過使用 type() 可查看當(dāng)前變量的類型
String(字符串)
- 在python中 使用單引號和雙引號 括起來的字符串 (單引號和雙引號沒有什么區(qū)別,如果省事的話就用單引號吧)
>>> a='name'
>>> print(type(a))
<class 'str'>
>>> a='name\n'
>>> print(a)
name
- \n 會輸出換行,如果不想輸出的可以在前面加一個 r
>>> a=r'name\n'
>>> print(a)
name\n
如果想獲取字符串中的某一個字符呢,可以通過下標(biāo)
>>> str='hello word'
>>> print(str[0])
h
- 和大多數(shù)語言一樣 下標(biāo)從 0 開始 遞增,但是還有另外一種可以從右往左
- 從右邊往左從-1開始
>>> str='hello word'
>>> print(str[-1])
d
想要獲取字符串中的一段呢
- 使用冒號分割,前面是起始位置,后面是截止位置
>>> str='hello word'
>>> print(str[0:5])
hello
需要注意的是 python字符串不能被修改,會報錯
>>> str='hello word'
>>> str[0]='a'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
字符串可以用 + 來拼接
>>> str='hello'+'word'
>>> print(str)
helloword
可以使用 * 運算符重復(fù)字符串
>>> str='h'* 3
>>> print(str)
hhh
List (列表)
- 列表是python中使用最頻繁的的數(shù)據(jù)類型
- 寫在方括號之間,逗號分割的元素列表,元素類型可以不同
>>> names=['hello', 1, True, 1.3]
>>> print(name)
['hello', 1, True, 1.3]
獲取List中的一個元素
- 和字符串一樣使用下標(biāo)獲取
>>> names=['hello', 1, True, 1.3]
>>> print(names[0])
hello
獲取List中的幾個元素
- 這個就是切片,和字符串一樣使用方括號,冒號分割
>>> names=['hello', 1, True, 1.3]
>>> print(names[0:3])
['hello', 1, True]
合并倆個數(shù)組呢
- 同樣使用 + 可以合并倆個數(shù)組
>>> n1 = [1, 2, 3]
>>> n1 + [4, 5, 6]
[1, 2, 3, 4, 5, 6]
列表和字符串不一樣是可以修改值的
>>> n1 = [1, 2, 3]
>>> n1[0]=4
>>> print(n1)
[4, 2, 3]
Tuple (元組)
- 元組使用()小括號,元素之間用逗號分割
- 元組的元素類型不限制
- 元組的值和字符串一樣是不可以修改的
>>> a = (1, 1,1, 'name', True)
>>> print(a)
(1, 1, 1, 'name', True)
如何獲取元素和獲取指定元素
>>> a = (1, 1.1, 'name', True)
>>> print(a[0], a[1:3])
1 (1.1, 'name')
合并元組
- 和數(shù)組一樣使用+可以來合并元組
>>> a1 = (1, 2, 3)
>>> a1 + (4, 5, 6)
(1, 2, 3, 4, 5, 6)
Sets(集合)
- 集合是一個無序不重復(fù)元素的集
- 集合可以使用 {} 打括號 和 set來創(chuàng)建
>>> a = {1,1}
>>> print(a)
{1}
- 如果重復(fù),只保留一個
使用set()創(chuàng)建
>>> a = set('abc')
>>> print(a)
{'a', 'c', 'b'}
集合可以用來計算
- a 和 b的差集
>>> a = set('abcdef')
>>> b = set('abc')
>>> a-b
{'d', 'f', 'e'}
- a和b的并集
>>> a = set('abcdef')
>>> b = set('abc')
>>> a|b
{'a', 'e', 'c', 'b', 'd', 'f'}
- a和b之間的交集
>>> a = set('abcdef')
>>> b = set('abc')
>>> a&b
{'a', 'c', 'b'}
- a和b 不同時存在的元素
>>> a = set('abcdef')
>>> b = set('abc')
>>> a^b
{'d', 'f', 'e'}
Dictionaries(字典)
- 它是一個無序的鍵 : 值對集合
- 在同一個字典中,關(guān)鍵字還必須互不相同
- 創(chuàng)建一個字典
>>> dic = {'name':'text', 'age':'18'}
>>> print(dic)
{'name': 'text', 'age': '18'}
- 獲取name的值
>>> dic = {'name':'text', 'age':'18'}
>>> dic['name']
'text'
- 刪除一個元素
>>> dic = {'name':'text', 'age':'18'}
>>> del dic['name']
>>> print(dic)
{'age': '18'}
- 添加一個元素
>>> dic['name'] = 'new'
>>> print(dic)
{'age': '18', 'name': 'new'}
- 返回字典中的所有key值
>>> dic = {'age': '18', 'name': 'new'}
>>> list(dic.keys())
['age', 'name']
- 判斷成員是否存在
>>> 'name' in dic
True
>>> 'name' not in dic
False