1. 程式人生 > >Python 基礎之運算子和基本資料型別

Python 基礎之運算子和基本資料型別

1. 運算子

1.1 結果是具體值(數字或字串)的運算子
1.1.1 算數運算

1.1.2 賦值運算

1.2 結果是布林值的運算子
1.2.1 比較運算

1.2.2 邏輯運算

1.2.3 成員運算

2. 基本資料型別入門
2.1 字串
2.1.1 字串介紹
在 python 中,字串可以用單引號、雙引號、三個單引號和三個雙引號引起來。

name = "勒布朗"
name = 'iverson'
name = '''sb'''
name = """"tsb"""

2.1.2 字串的加法

[[email protected] ~]# vim str.py
n1 = "alex
" n2 = "tsb" n3 = "zhang" n4 = n1 + n2 + n3 print(n4) [[email protected] ~]# python3 str.py # 字串的加法會將字串拼接起來。 alextsbzhang

2.1.3 字串的乘法

[[email protected] ~]# vim strc.py
n1 = "zhang"
n2 = n1 * 10
print(n2) 
[[email protected] ~]# python3 strc.py # 字串的乘法會將字串重複。
zhangzhangzhangzhangzhangzhangzhangzhangzhangzhang
提示:Python 中字串只有乘法和加法,沒有除法和減法。

2.2 數字

在 python 中沒有用引號引起來的阿拉伯數字,例如 age = 15。
2.2.1 數字的加減乘除冪運算

a1 = 10 
a2 = 20 
a3 = a1 + a2
a4 = a2 - a1
a5 = a1 * a2 # 一個 * 表示乘法。
a6 = 10 / 4 # 除法取商(具體即 2.5)。
a6 = 10 //4 # 除法取商(即為 2)。
a7 = 4**4 # 冪運算。
a8 = 58 % 6 # 取模(取出算式的餘數)。

2.2.2 有關將數字字串轉化為數字的問題

  • 使用 print('字串') 的方式只能輸出字串;
  • 使用 input 等待輸入,不輸入永遠等待,如 inp = input ('>>>') 方式輸入的數字也會被認為是字串;
  • 將數字字串轉換為數字:new_inp = int(inp) 可以通過 int 整型轉換。

2.3 布林值
2.3.1 通過舉例引出布林值

name = "布萊恩特"
if "" in name:
print('OK')
else:
print('Error')
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
OK # 執行結果為 OK。
name = "布萊恩特"
if "布恩" in name:
print('OK')
else:
print('Error')
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
Error # 布恩不是上面字串的子序列,所以 Error。
name = "布萊恩特"
if "布恩" not in name:
print('OK')
else:
print('Error')
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
OK # 判斷是否 not in 執行的結果。
判斷某個東西是否在某個東西里包含,即為 in 或者 not in。
此時引出資料型別為布林值,包含真(True)或假(False)。

2.3.2 布林值運用示例

name = "布萊恩特"
v = "布恩" not in name
print(v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
True # 將判斷條件結果賦值給 v,通過 print 得到布林值。

3. 基本資料型別應用詳解
3.1 數字(int)
3.1.1 數字(整型)的介紹
數字,用英文 int 代指,在 Python 3 裡,數字無論有多大,都屬於數字(int)型別。在 Python 2 裡面數字(int)被稱為整型,它有一個範圍,超過這個指定的範圍,就不屬於整形(int)型別了,被稱為長整型(long)。Python 3 裡的所有型別個都是整型(整數型別)。
3.1.2 整型的所有功能都包含在 int 裡面
可通過在 Pycharm 中輸入 int,點選 Ctrl 鍵不放,滑鼠點選 int 可以看到所有整型對應的功能。
3.1.3 整型的魔法
① int 將數字字串轉換為整型數字:

a = "123" 
print(type(a)) # 輸出 a 的資料型別。
b = int(a) # 此步會將字串“123”轉換為整型數字 123。
b = b + 321 # 轉換完成還可做數值運算。
print(type(b)) # 輸出 b 的資料型別。
print(b)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py # 執行結果。
<class 'str'> # 此處輸出了上面 a 字串的資料型別。
<class 'int'> # 此處輸出了上面 b 整型的資料型別。
444 # 此處輸出了轉換為整型並運算後的數字結果。
上面的資料型別和結果可以同時輸出:
a = "123"
print(type(a),a)
b = int(a)
b = b + 321
print(type(b),b)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py 
<class 'str'> 123 # 資料型別和結果同時輸出(字串)。
<class 'int'> 444 # 資料型別和結果同時輸出(整型)。

② bit_length() 當前數字的二進位制至少用幾位二進位制位表示:

例如將數字轉換為二進位制位的位數:
age = 5
r = age.bit_length()
print(r)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
3 # 表示將整型數字 5 轉換為二進位制後有 3 個二進位制位(即 101)。

3.2 字串(str)

3.2.1 字串型別介紹
字串,用英文 str 代指。
3.2.2 字串型別的所有功能都包含在 str 裡面
可通過在 Pycharm 中輸入 str,點選 Ctrl 鍵不放,滑鼠點選 str 可以看到所有字串型別對應的功能。
3.2.3 字串的魔法
① capitalize() 將英文字串的首字母改為大寫:

test = "alex"
v = test.capitalize()
print(v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
Alex # 可以看到 Alex 的名字首字母變成大寫了。

② test.casefold() 和 test.lower() 將英文字串中的大寫轉換為小寫:

test = "aLex"
v1 = test.casefold() # 優先使用,包含很多未知的對應關係。
print(v1)
v2 = test.lower()
print(v2)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
alex
alex

③ center(self, width, fillchar=None) 設定字串長度,並將內容居中。

test = "aLex"
v = test.center(20) # 此處的 20 代表總長度。
print(v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
aLex # 可以看到兩邊加了空格湊夠 20 個字元。
test = "aLex"
v = test.center(20,"*") 
# 後面加 * 則會用星號填充空白位置(可有可無,不寫預設為空格,只能填一個字元)。
print(v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
********aLex********
test = "alex"
v = test.ljust(20,"*") # ljust 表示將內容放在左邊,用指定字串填充其他空白位置。
print(v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
alex****************
test = "alex"
v = test.rjust(20,"*") # rjust 表示將內容放在最右邊,其他位置用指定字元填充。
print(v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
****************alex

④ count(self, sub, start=None, end=None) 在指定字串中尋找包含指定字元或子序列的個數:

test = "aLexalexer"
v = test.count("e")
print(v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
3 # 此處的 3 表示上面字串中含有指定字元 e 的個數為 3 個。
test = "aLexalexer"
v = test.count("ex")
print(v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
2 # 表示在該字串中包含指定子序列的個數為 2 個。
test = "aLexalexer"
v = test.count("ex",5)
print(v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
1 # 從第五個字元開始找指定子序列的個數為 1個。
test = "aLexalexer"
v = test.count("ex",5,6)
print(v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
0 # 在第五個和第六個字元之間找指定子序列的個數為 0。

⑤ endswith(self, suffix, start=None, end=None) 判斷字串以什麼字元結尾,正確為 True,錯誤為 Flase:

startswith(self, suffix, start=None, end=None) 判斷字串以什麼字元開頭,正確為 True,錯誤為 Flase:
test = "alex"
v = test.endswith("a")
print(v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
False # 判斷字串是否以 a 結尾,不是所以輸出 Flase。
test = "alex"
v = test.endswith("ex")
print(v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
True # 判斷字串是否以 ex 結尾,正確所以輸出 True。
test = "alex"
v = test.endswith("ex")
print(v)
v1 = test.startswith("al") # 與之對應的還有 startwith,略。
print(v1)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
True
True

⑥ find(self, sub, start=None, end=None) 從開始往後找指定子序列,找到第一個獲取其位置:

test = "alexalex"
v = test.find('ex')
print(v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
2 # 表示在該字串中的第二個字元後面找到指定子序列。
test = "alexalex"
v = test.find('ex',5,8)
print(v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
6 # 表示在大於等於 5 小於 8 的區間內尋找指定子序列的位置。

⑦ format(self, *args, **kwargs) 格式化,將一個字串中的佔位符替換為指定的值。

test = 'I am {name},my age is {age}'
v = test.format(name='alex',age=20)
print(v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
I am alex,my age is 20 
上面的也可以不指定名字將佔位符(佔位符從 0 開始)替換為指定值:
test = 'I am {0},my age is {1}'
v = test.format('alex',20)
print(v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
I am alex,my age is 20 
有關字典的用法:
test = 'I am {name},my age is {age}'
v1 = test.format(name='alex',age=20)
print(v1)
v2 = test.format_map({"name":'alex',"age":'20'}) # 字典,一個 KRY 對應一個值(鍵值對)。
print(v2)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
I am alex,my age is 20
I am alex,my age is 20

⑧ index(self, sub, start=None, end=None) 尋找字串中的子序列,找不到就報錯:【忽略、用 find】

test = "alexalex"
v = test.index('ex')
print(v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
2 # 此處找到了子序列 ex 的位置,找不到就報錯。

⑨ isalnum(self) 判斷字串是否只包含數字和字母(全是字母或數字也行),不是就 False,是就 True:

test = "dag5發5+__"
v = test.isalnum()
print(v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
False # 可以看到上述字串不是數字和字母的集合。
test = "dag5464" # 可以看到該字串是數字和字母的集合。
v = test.isalnum()
print(v) 
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
True # 判斷出該字串是數字字母的集合,所以輸出為 True。

⑩ expandtabs(self, tabsize=8) \t 補全下面指定字元數:

test = "username\temail\tpassword\nalinuxer\[email protected]\t123\nalinuxer\[email protected]\t123\nalinuxer\[email protected]\t123\n"
v = test.expandtabs(20)
print(v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
username email password
alinuxer [email protected] 123
alinuxer [email protected] 123
alinuxer [email protected] 123
# 表示當遇到 \t 時不夠 20 個字元就用空格補齊 20 個。

⑪ isalpha(self) 判斷字串是否是字母或漢字,是則 True,否則 False。

test = "as2df"
v = test.isalpha()
print(v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
False

⑫ 判斷字串是否為數字:

test = "123"
v1 = test.isdecimal() # 十進位制的數字【用的最多】。
v2 = test.isdigit() # 可識別諸如 ② ③ 這樣的數字。
v3 = test.isnumeric() # 可識別 “二”、“四” 這樣的中文數字。
print(v1,v2,v3)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
True True True

⑬ isprintable(self) 判斷是否存在不可顯示的字元(如:\t、\n):

test = "oiuasdfkj" # 沒有不可顯示的字元。
v = test.isprintable()
print(v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
True # 所以此處顯示為 True。
test = "oiua\tsdfkj" # 此處存在不可顯示的 \n 字元。
v = test.isprintable()
print(v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
False # 所以此處顯示為 False。

⑭ isspace(self) 判斷是否全部是空格。

test = " " # 該字串內只有兩個空格。
v = test.isspace()
print(v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
True # 因為全部是空格,所以此處輸出為 True。

⑮ istitle() 判斷是否是標題:

test = "Return True if all cased characters in S are uppercase and there is"
v1 = test.istitle()
print(v1)
v2 = test.title()
print(v2)
v3 = v2.istitle()
print(v3)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
False # 判斷是否為標題(不是每個首字母都是大寫,所以 False)。
Return True If All Cased Characters In S Are Uppercase And There Is # 轉換為標題。
True # 判斷轉換為標題的內容是否為標題(因為轉換為標題了所以 True)。

⑯ join(self, iterable) 將字串中的每一個元素按照指定分隔符進行拼接:【非常重要】

test = "你是風兒我是沙"
print(test)
t = ' ' # 此處指定空格為分隔符。
v = t.join(test)
print(v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
你是風兒我是沙
你 是 風 兒 我 是 沙
test = "你是風兒我是沙"
print(test)
v = "_".join(test) # 還可以通過此處這種方式指定下劃線為分隔符進行拼接。
print(v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
你是風兒我是沙
你_是_風_兒_我_是_沙

⑰ 判斷是否全部是大小寫和轉換為大小寫

test = "Alex"
v1 = test.islower() # 判斷是否全部為小寫。
v2 = test.lower() # 轉換為小寫。
print(v1, v2)
v1 = test.isupper() # 判斷是否全部為大寫。
v2 = test.upper() # 轉換為大寫。
print(v1,v2)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
False alex
False ALEX

⑱ 預設可移除字串兩端 \t、\n 或空格字元,還可移除指定字元:

test = " alex " # 字串兩端都有一個空格。
v1 = test.lstrip() # 去除左邊的空格。
print(v1)
v2 = test.rstrip() # 去除右邊的空格。
print(v2)
v3 = test.strip() # 去除兩邊的空格。
print(v3)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
alex 
alex
alex
去掉指定字元的操作:(可去掉多個字元)
test = "xalex"
v = test.lstrip('x')
print(v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
alex # 可以看到通過上面指定去除了左邊的 ‘x’ 字元。
還可以匹配字串中的所有內容並去除(優先最多匹配):
test = "alex"
v = test.lstrip('bdxamklex')
print(v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
# 此處全部被匹配去除。

⑲ maketrans 結合 translate 做對應關係替換:

v = "asidufkasd;fiuadkf;adfkjalsdjf"
m = str.maketrans("aeiou", "12345") # 對應關係為 aeiou 對應 12345。
new_v = v.translate(m) # 將字串中的數字通過對應關係做替換。
print(new_v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
1s3d5fk1sd;f351dkf;1dfkj1lsdjf

⑳ partition 從左邊匹配指定字元分割為三部分(匹配到第一個指定字元就分割):

test = "testasdsddfg"
v = test.partition('s')
print(v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
('te', 's', 'tasdsddfg') # 指定 ‘s’ 匹配到第一個 s 就分割為 3 部分。
rpartition 從右邊匹配指定字元分割為三部分 
test = "testasdsddfg"
v = test.rpartition('s')
print(v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
('testasd', 's', 'ddfg') # 從右邊匹配到第一個指定 ‘s’ 字元分割為三份。

㉑ split rsplit 指定分割的字元將字串分割為指定個數的部分:

test = "testasdsddfg"
v1 = test.split('s',2) # 從左邊匹配兩個 ‘s’ 分割。
print(v1)
v2 = test.rsplit('s',2) # 從右邊匹配兩個 ‘s’ 分割。
print(v2)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
['te', 'ta', 'dsddfg']
['testa', 'd', 'ddfg']

㉒ splitlines() 分割,只能根據換行分割,後面可跟 True、False 決定是否保留換行:

test = "asdfadfasdf\nasdfasdf\nadfasdf"
v = test.splitlines(False) # False 表示不保留換行。
print(v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
['asdfadfasdf', 'asdfasdf', 'adfasdf'] 
test = "asdfadfasdf\nasdfasdf\nadfasdf"
v = test.splitlines(True) # True 表示保留換行。
print(v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
['asdfadfasdf\n', 'asdfasdf\n', 'adfasdf']

㉓ startswith endswith 判斷字串是否以 xx 開頭,以 xx 結尾:

test = "backend 1.1.1.1"
v1 = test.startswith('ba') # 判斷是否以 ‘ba’ 開頭。
print(v1)
v2 = test.endswith('a') # 判斷是否以 ‘a’ 結尾。
print(v2)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
True
False

㉔ swapcase 大小寫轉換(大寫換小寫,小寫換大寫):

test = "aLex"
v = test.swapcase()
print(v)
D:\pycharmfiles\first\venv\Scripts\python.exe D:/pycharmfiles/first/day2/buer.py
AlEX

 

----------------- 未完待續 -----------------