天天看點

python—清單、元祖、字典、字元串的基本使用1. python的資料類型2. 清單的基本使用3. 元組的基本使用 4. 字典的基本使用5. 字元串的基本使用6. 公共方法

Table of Contents

1. python的資料類型

2. 清單的基本使用

3. 元組的基本使用 

4. 字典的基本使用

5. 字元串的基本使用

6. 公共方法

6.1 Python 内置函數

6.2 切片

6.3 運算符

1. python的資料類型

Python 中資料類型可以分為 數字型 和 非數字型

  • 數字型
    • 整型 (

      int

      )
    • 浮點型(

      float

    • 布爾型(

      bool

      • True

        非 0 數

        —— 非零即真
      • False

    • 複數型 (

      complex

      )
      • 主要用于科學計算,例如:平面場問題、波動問題、電感電容等問題
  • 非數字型
    • 字元串
    • 清單
    • 元組
    • 字典
  • Python

    中,所有 非數字型變量 都支援以下特點:
    1. 都是一個 序列

      sequence

      ,也可以了解為 容器
    2. 取值

      []

    3. 周遊

      for in

    4. 計算長度、最大/最小值、比較、删除
    5. 連結

      +

      和 重複

      *

    6. 切片

2. 清單的基本使用

  • List

    (清單) 是

    Python

    中使用 最頻繁 的資料類型,在其他語言中通常叫做 數組
python—清單、元祖、字典、字元串的基本使用1. python的資料類型2. 清單的基本使用3. 元組的基本使用 4. 字典的基本使用5. 字元串的基本使用6. 公共方法
  • ipython3

    中定義一個 清單,例如:

    name_list = []

  • 輸入

    name_list.

    按下

    TAB

    鍵,

    ipython

    會提示 清單 能夠使用的 方法 如下:
In [1]: name_list.
name_list.append   name_list.count    name_list.insert   name_list.reverse
name_list.clear    name_list.extend   name_list.pop      name_list.sort
name_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() 逆序、反轉

3. 元組的基本使用 

  • Tuple

    (元組)與清單類似,不同之處在于元組的 元素不能修改
    • 元組 表示多個元素組成的序列
    • 元組 在

      Python

      開發中,有特定的應用場景
info_tuple = ("zhangsan", 18, 1.75)
           

建立空元組

info_tuple = ()
           

元組中 隻包含一個元素 時,需要 在元素後面添加逗号

info_tuple = (50, )
           
python—清單、元祖、字典、字元串的基本使用1. python的資料類型2. 清單的基本使用3. 元組的基本使用 4. 字典的基本使用5. 字元串的基本使用6. 公共方法
  • ipython3

    中定義一個 元組,例如:

    info = ()

  • 輸入

    info.

    按下

    TAB

    鍵,

    ipython

    會提示 元組 能夠使用的函數如下:
info.count  info.index
           

4. 字典的基本使用

  • dictionary

    (字典) 是 除清單以外

    Python

    之中 最靈活 的資料類型
  • 字典同樣可以用來 存儲多個資料
    • 通常用于存儲 描述一個

      物體

      的相關資訊
  • 字典使用 鍵值對 存儲資料,鍵值對之間使用

    ,

    分隔
    • key

      是索引
    • value

      是資料
    • 鍵 和 值 之間使用

      :

      分隔
    • 鍵必須是唯一的
    • 值 可以取任何資料類型,但 鍵 隻能使用 字元串、數字或 元組
xiaoming = {"name": "小明",
            "age": 18,
            "gender": True,
            "height": 1.75}
           
python—清單、元祖、字典、字元串的基本使用1. python的資料類型2. 清單的基本使用3. 元組的基本使用 4. 字典的基本使用5. 字元串的基本使用6. 公共方法
  • ipython3

    中定義一個 字典,例如:

    xiaoming = {}

  • 輸入

    xiaoming.

    按下

    TAB

    鍵,

    ipython

    會提示 字典 能夠使用的函數如下:
In [1]: xiaoming.
xiaoming.clear       xiaoming.items       xiaoming.setdefault
xiaoming.copy        xiaoming.keys        xiaoming.update
xiaoming.fromkeys    xiaoming.pop         xiaoming.values
xiaoming.get         xiaoming.popitem    
           

5. 字元串的基本使用

string = "Hello Python" ​

for c in string:    

    print(c) ​
           
python—清單、元祖、字典、字元串的基本使用1. python的資料類型2. 清單的基本使用3. 元組的基本使用 4. 字典的基本使用5. 字元串的基本使用6. 公共方法
  • ipython3

    中定義一個 字元串,例如:

    hello_str = ""

  • 輸入

    hello_str.

    按下

    TAB

    鍵,

    ipython

    會提示 字元串 能夠使用的 方法 如下:
In [1]: hello_str.
hello_str.capitalize    hello_str.isidentifier  hello_str.rindex
hello_str.casefold      hello_str.islower       hello_str.rjust
hello_str.center        hello_str.isnumeric     hello_str.rpartition
hello_str.count         hello_str.isprintable   hello_str.rsplit
hello_str.encode        hello_str.isspace       hello_str.rstrip
hello_str.endswith      hello_str.istitle       hello_str.split
hello_str.expandtabs    hello_str.isupper       hello_str.splitlines
hello_str.find          hello_str.join          hello_str.startswith
hello_str.format        hello_str.ljust         hello_str.strip
hello_str.format_map    hello_str.lower         hello_str.swapcase
hello_str.index         hello_str.lstrip        hello_str.title
hello_str.isalnum       hello_str.maketrans     hello_str.translate
hello_str.isalpha       hello_str.partition     hello_str.upper
hello_str.isdecimal     hello_str.replace       hello_str.zfill
hello_str.isdigit       hello_str.rfind
           
提示:正是因為 python 内置提供的方法足夠多,才使得在開發時,能夠針對字元串進行更加靈活的操作!應對更多的開發需求!

1) 判斷類型 - 9

方法 說明
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

2) 查找和替換 - 7

方法 說明
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 次

3) 大小寫轉換 - 5

方法 說明
string.capitalize() 把字元串的第一個字元大寫
string.title() 把字元串的每個單詞首字母大寫
string.lower() 轉換 string 中所有大寫字元為小寫
string.upper() 轉換 string 中的小寫字母為大寫
string.swapcase() 翻轉 string 中的大小寫

4) 文本對齊 - 3

方法 說明
string.ljust(width) 傳回一個原字元串左對齊,并使用空格填充至長度 width 的新字元串
string.rjust(width) 傳回一個原字元串右對齊,并使用空格填充至長度 width 的新字元串
string.center(width) 傳回一個原字元串居中,并使用空格填充至長度 width 的新字元串

5) 去除空白字元 - 3

方法 說明
string.lstrip() 截掉 string 左邊(開始)的空白字元
string.rstrip() 截掉 string 右邊(末尾)的空白字元
string.strip() 截掉 string 左右兩邊的空白字元

6) 拆分和連接配接 - 5

方法 說明
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 中所有的元素(的字元串表示)合并為一個新的字元串

 字元串的切片

  • 切片 方法适用于 字元串、清單、元組
    • 切片 使用 索引值 來限定範圍,從一個大的 字元串 中 切出 小的 字元串
    • 清單 和 元組 都是 有序 的集合,都能夠 通過索引值 擷取到對應的資料
    • 字典 是一個 無序 的集合,是使用 鍵值對 儲存資料 ​
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])
           

6. 公共方法

6.1 Python 内置函數

Python 包含了以下内置函數:

函數 描述 備注
len(item) 計算容器中元素個數
del(item) 删除變量 del 有兩種方式
max(item) 傳回容器中元素最大值 如果是字典,隻針對 key 比較
min(item) 傳回容器中元素最小值 如果是字典,隻針對 key 比較
cmp(item1, item2) 比較兩個值,-1 小于/0 相等/1 大于 Python 3.x 取消了 cmp 函數

注意

  • 字元串 比較符合以下規則: "0" < "A" < "a"

6.2 切片

描述 Python 表達式 結果 支援的資料類型
切片 "0123456789"[::-2] "97531" 字元串、清單、元組
  • 切片 使用 索引值 來限定範圍,從一個大的 字元串 中 切出 小的 字元串
  • 清單 和 元組 都是 有序 的集合,都能夠 通過索引值 擷取到對應的資料
  • 字典 是一個 無序 的集合,是使用 鍵值對 儲存資料

6.3 運算符

運算符 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

注意:在對 字典 操作時,判斷的是 字典的鍵