https://www.cnblogs.com/JcrLive/p/12235360.html
Python 中數據類型可以分為 數字型 和 非數字型
數字型
int
)float
)bool
)True
非 0 數
—— 非零即真False
0
complex
)非數字型
在 Python
中,所有 非數字型變量 都支持以下特點:
sequence
,也可以理解為 容器[]
for in
+
和 重復 *
List
(列表) 是 Python
中使用 最頻繁 的數據類型,在其他語言中通常叫做 數組[]
定義,數據 之間使用 ,
分隔0
開始注意:從列表中取值時,如果 超出索引范圍,程序會報錯
name_list = ["zhangsan", "lisi", "wangwu"]
ipython3
中定義一個 列表,例如:name_list = []
name_list.
按下 TAB
鍵,ipython
會提示 列表 能夠使用的 方法 如下:In [1]: name_list.name_list.append name_list.count name_list.insert name_list.reversename_list.clear name_list.extend name_list.pop name_list.sortname_list.copy name_list.index name_list.remove
序號 | 分類 | 關鍵字 / 函數 / 方法 | 說明 |
---|---|---|---|
1 | 增加 | 列表.insert(索引, 數據) | 在指定位置插入數據 |
列表.append(數據) | 在末尾追加數據 | ||
列表.extend(列表2) | 將列表2 的數據追加到列表 | ||
2 | 修改 | 列表[索引] = 數據 | 修改指定索引的數據 |
3 | 刪除 | del 列表[索引] | 刪除指定索引的數據 |
列表.remove[數據] | 刪除第一個出現的指定數據 | ||
列表.pop | 刪除末尾數據 | ||
列表.pop(索引) | 刪除指定索引數據 | ||
列表.clear | 清空列表 | ||
4 | 統計 | len(列表) | 列表長度 |
列表.count(數據) | 數據在列表中出現的次數 | ||
5 | 排序 | 列表.sort() | 升序排序 |
列表.sort(reverse=True) | 降序排序 | ||
列表.reverse() | 逆序、反轉 |
del
關鍵字(delete
) 同樣可以刪除列表中元素del
關鍵字本質上是用來 將一個變量從內存中刪除的del
關鍵字將變量從內存中刪除,后續的代碼就不能再使用這個變量了del name_list[1]
在日常開發中,要從列表刪除數據,建議 使用列表提供的方法
In [1]: import keywordIn [2]: print(keyword.kwlist)In [3]: print(len(keyword.kwlist))
關鍵字后面不需要使用括號
函數名(參數)
函數需要死記硬背
對象.方法名(參數)
在變量后面輸入
.
,然后選擇針對這個變量要執行的操作,記憶起來比函數要簡單很多
遍歷 就是 從頭到尾 依次 從 列表 中獲取數據
在 Python
中為了提高列表的遍歷效率,專門提供的 迭代 iteration 遍歷
使用 for
就能夠實現迭代遍歷
# for 循環內部使用的變量 in 列表for name in name_list: 循環內部針對列表元素進行操作 print(name)
Python
的 列表 中可以 存儲不同類型的數據Tuple
(元組)與列表類似,不同之處在于元組的 元素不能修改Python
開發中,有特定的應用場景,
分隔()
定義0
開始info_tuple = ("zhangsan", 18, 1.75)
info_tuple = ()
info_tuple = (50, )
ipython3
中定義一個 元組,例如:info = ()
info.
按下 TAB
鍵,ipython
會提示 元組 能夠使用的函數如下:info.count info.index
有關 元組 的 常用操作 可以參照上圖練習
# for 循環內部使用的變量 in 元組for item in info: 循環內部針對元組元素進行操作 print(item)
- 在
Python
中,可以使用for
循環遍歷所有非數字型類型的變量:列表、元組、字典 以及 字符串- 提示:在實際開發中,除非 能夠確認元組中的數據類型,否則針對元組的循環遍歷需求并不是很多
for in
遍歷 元組()
本質上就是一個元組info = ("zhangsan", 18)print("%s 的年齡是 %d" % info)
list
函數可以把元組轉換成列表list(元組)
tuple
函數可以把列表轉換成元組tuple(列表)
dictionary
(字典) 是 除列表以外 Python
之中 最靈活 的數據類型物體
的相關信息{}
定義,
分隔key
是索引value
是數據:
分隔xiaoming = {"name": "小明", "age": 18, "gender": True, "height": 1.75}
ipython3
中定義一個 字典,例如:xiaoming = {}
xiaoming.
按下 TAB
鍵,ipython
會提示 字典 能夠使用的函數如下:In [1]: xiaoming.xiaoming.clear xiaoming.items xiaoming.setdefaultxiaoming.copy xiaoming.keys xiaoming.updatexiaoming.fromkeys xiaoming.pop xiaoming.valuesxiaoming.get xiaoming.popitem
有關 字典 的 常用操作 可以參照上圖練習
# for 循環內部使用的 `key 的變量` in 字典for k in xiaoming: print("%s: %s" % (k, xiaoming[k]))
提示:在實際開發中,由于字典中每一個鍵值對保存數據的類型是不同的,所以針對字典的循環遍歷需求并不是很多
for in
遍歷 字典物體
的相關信息 —— 描述更復雜的數據信息card_list = [{"name": "張三", "qq": "12345", "phone": "110"}, {"name": "李四", "qq": "54321", "phone": "10086"} ]
"
或者 一對單引號 '
定義一個字符串\"
或者 \'
做字符串的轉義,但是在實際開發中:"
,可以使用 '
定義字符串'
,可以使用 "
定義字符串for
循環遍歷 字符串中每一個字符大多數編程語言都是用
"
來定義字符串
string = "Hello Python"for c in string: print(c)
ipython3
中定義一個 字符串,例如:hello_str = ""
hello_str.
按下 TAB
鍵,ipython
會提示 字符串 能夠使用的 方法 如下:In [1]: hello_str.hello_str.capitalize hello_str.isidentifier hello_str.rindexhello_str.casefold hello_str.islower hello_str.rjusthello_str.center hello_str.isnumeric hello_str.rpartitionhello_str.count hello_str.isprintable hello_str.rsplithello_str.encode hello_str.isspace hello_str.rstriphello_str.endswith hello_str.istitle hello_str.splithello_str.expandtabs hello_str.isupper hello_str.splitlineshello_str.find hello_str.join hello_str.startswithhello_str.format hello_str.ljust hello_str.striphello_str.format_map hello_str.lower hello_str.swapcasehello_str.index hello_str.lstrip hello_str.titlehello_str.isalnum hello_str.maketrans hello_str.translatehello_str.isalpha hello_str.partition hello_str.upperhello_str.isdecimal hello_str.replace hello_str.zfillhello_str.isdigit hello_str.rfind
提示:正是因為 python 內置提供的方法足夠多,才使得在開發時,能夠針對字符串進行更加靈活的操作!應對更多的開發需求!
方法 | 說明 |
---|---|
string.isspace() | 如果 string 中只包含空格,則返回 True |
string.isalnum() | 如果 string 至少有一個字符并且所有字符都是字母或數字則返回 True |
string.isalpha() | 如果 string 至少有一個字符并且所有字符都是字母則返回 True |
string.isdecimal() | 如果 string 只包含數字則返回 True,全角數字 |
string.isdigit() | 如果 string 只包含數字則返回 True,全角數字 、⑴ 、\u00b2 |
string.isnumeric() | 如果 string 只包含數字則返回 True,全角數字 ,漢字數字 |
string.istitle() | 如果 string 是標題化的(每個單詞的首字母大寫)則返回 True |
string.islower() | 如果 string 中包含至少一個區分大小寫的字符,并且所有這些(區分大小寫的)字符都是小寫,則返回 True |
string.isupper() | 如果 string 中包含至少一個區分大小寫的字符,并且所有這些(區分大小寫的)字符都是大寫,則返回 True |
方法 | 說明 |
---|---|
string.startswith(str) | 檢查字符串是否是以 str 開頭,是則返回 True |
string.endswith(str) | 檢查字符串是否是以 str 結束,是則返回 True |
string.find(str, start=0, end=len(string)) | 檢測 str 是否包含在 string 中,如果 start 和 end 指定范圍,則檢查是否包含在指定范圍內,如果是返回開始的索引值,否則返回 -1 |
string.rfind(str, start=0, end=len(string)) | 類似于 find(),不過是從右邊開始查找 |
string.index(str, start=0, end=len(string)) | 跟 find() 方法類似,不過如果 str 不在 string 會報錯 |
string.rindex(str, start=0, end=len(string)) | 類似于 index(),不過是從右邊開始 |
string.replace(old_str, new_str, num=string.count(old)) | 把 string 中的 old_str 替換成 new_str,如果 num 指定,則替換不超過 num 次 |
方法 | 說明 |
---|---|
string.capitalize() | 把字符串的第一個字符大寫 |
string.title() | 把字符串的每個單詞首字母大寫 |
string.lower() | 轉換 string 中所有大寫字符為小寫 |
string.upper() | 轉換 string 中的小寫字母為大寫 |
string.swapcase() | 翻轉 string 中的大小寫 |
方法 | 說明 |
---|---|
string.ljust(width) | 返回一個原字符串左對齊,并使用空格填充至長度 width 的新字符串 |
string.rjust(width) | 返回一個原字符串右對齊,并使用空格填充至長度 width 的新字符串 |
string.center(width) | 返回一個原字符串居中,并使用空格填充至長度 width 的新字符串 |
方法 | 說明 |
---|---|
string.lstrip() | 截掉 string 左邊(開始)的空白字符 |
string.rstrip() | 截掉 string 右邊(末尾)的空白字符 |
string.strip() | 截掉 string 左右兩邊的空白字符 |
方法 | 說明 |
---|---|
string.partition(str) | 把字符串 string 分成一個 3 元素的元組 (str前面, str, str后面) |
string.rpartition(str) | 類似于 partition() 方法,不過是從右邊開始查找 |
string.split(str="", num) | 以 str 為分隔符拆分 string,如果 num 有指定值,則僅分隔 num + 1 個子字符串,str 默認包含 '\r', '\t', '\n' 和空格 |
string.splitlines() | 按照行('\r', '\n', '\r\n')分隔,返回一個包含各行作為元素的列表 |
string.join(seq) | 以 string 作為分隔符,將 seq 中所有的元素(的字符串表示)合并為一個新的字符串 |
字符串[開始索引:結束索引:步長]
注意:
[開始索引, 結束索引)
=> 開始索引 >= 范圍 < 結束索引
起始
位開始,到 結束
位的前一位 結束(不包含結束位本身)1
,如果連續切片,數字和冒號都可以省略演練需求
末尾
的字符串開始
~ 5 位置 的字符串末尾 - 1
的字符串答案
num_str = "0123456789"# 1. 截取從 2 ~ 5 位置 的字符串print(num_str[2:6])# 2. 截取從 2 ~ `末尾` 的字符串print(num_str[2:])# 3. 截取從 `開始` ~ 5 位置 的字符串print(num_str[:6])# 4. 截取完整的字符串print(num_str[:])# 5. 從開始位置,每隔一個字符截取字符串print(num_str[::2])# 6. 從索引 1 開始,每隔一個取一個print(num_str[1::2])# 倒序切片# -1 表示倒數第一個字符print(num_str[-1])# 7. 截取從 2 ~ `末尾 - 1` 的字符串print(num_str[2:-1])# 8. 截取字符串末尾兩個字符print(num_str[-2:])# 9. 字符串的逆序(面試題)print(num_str[::-1])
Python 包含了以下內置函數:
函數 | 描述 | 備注 |
---|---|---|
len(item) | 計算容器中元素個數 | |
del(item) | 刪除變量 | del 有兩種方式 |
max(item) | 返回容器中元素最大值 | 如果是字典,只針對 key 比較 |
min(item) | 返回容器中元素最小值 | 如果是字典,只針對 key 比較 |
cmp(item1, item2) | 比較兩個值,-1 小于/0 相等/1 大于 | Python 3.x 取消了 cmp 函數 |
注意
描述 | Python 表達式 | 結果 | 支持的數據類型 |
---|---|---|---|
切片 | "0123456789"[::-2] | "97531" | 字符串、列表、元組 |
運算符 | Python 表達式 | 結果 | 描述 | 支持的數據類型 |
---|---|---|---|---|
+ | [1, 2] + [3, 4] | [1, 2, 3, 4] | 合并 | 字符串、列表、元組 |
* | ["Hi!"] * 4 | ['Hi!', 'Hi!', 'Hi!', 'Hi!'] | 重復 | 字符串、列表、元組 |
in | 3 in (1, 2, 3) | True | 元素是否存在 | 字符串、列表、元組、字典 |
not in | 4 not in (1, 2, 3) | True | 元素是否不存在 | 字符串、列表、元組、字典 |
> >= == < <= | (1, 2, 3) < (2, 2, 3) | True | 元素比較 | 字符串、列表、元組 |
注意
in
在對 字典 操作時,判斷的是 字典的鍵in
和 not in
被稱為 成員運算符成員運算符用于 測試 序列中是否包含指定的 成員
運算符 | 描述 | 實例 |
---|---|---|
in | 如果在指定的序列中找到值返回 True,否則返回 False | 3 in (1, 2, 3) 返回 True |
not in | 如果在指定的序列中沒有找到值返回 True,否則返回 False | 3 not in (1, 2, 3) 返回 False |
注意:在對 字典 操作時,判斷的是 字典的鍵
Python
中完整的 for 循環
的語法如下:for 變量 in 集合: 循環體代碼else: 沒有通過 break 退出循環,循環結束后,會執行的代碼
students = [ {"name": "阿土", "age": 20, "gender": True, "height": 1.7, "weight": 75.0}, {"name": "小美", "age": 19, "gender": False, "height": 1.6, "weight": 45.0},]find_name = "阿土"for stu_dict in students: print(stu_dict) # 判斷當前遍歷的字典中姓名是否為find_name if stu_dict["name"] == find_name: print("找到了") # 如果已經找到,直接退出循環,就不需要再對后續的數據進行比較 breakelse: print("沒有找到")print("循環結束")