PYTHON之路(第二篇):PYTHON基礎資料型別 (Elementary Data Type),python第二篇
一、基礎
1、編碼
UTF-8:中文佔3個位元組
GBK:中文佔2個位元組
Unicode、UTF-8、GBK三者關係
2、input()函數
n = input(" ")>>>hello>>>n>>>'hello'
n = input(" ")>>>10>>>n>>>'10'
輸入數字10,這裡的n是字串'10',而非數字10
這裡如果
n * 10將輸出
'10101010101010101010'
如果將字串轉換數字,可以用Int( )
new_n = int(n)
3、while迴圈、continue、break
while 條件陳述式1: 功能代碼1else 條件陳述式2: 功能代碼2
while迴圈也可以加else
例子:使用while迴圈輸入 1 2 3 4 5 6 8 9 10
n = 1while n < 11: if n == 7 : pass else: print(n) n = n + 1
或者
count = 1while count < 11 if count == 7: count = count + 1 continue print(count) count = count + 1
當while執行到if count ==7時,遇到continue,下面的print語句和count=count + 1不會被執行,重新跳回while語句
再比如
count = 1while count < 11: count = count + 1 continue print('123')print('end')
這裡的print('123')永遠不能被執行到
第二個例子
count = 1while count < 11: count = count + 1 print(count) break print('123')print('end')
輸出結果
2end
這裡的print('123')也不能被執行到,遇到break語句直接跳出迴圈,只能執行一次迴圈,即輸出一次print(count)語句
此程式完整執行過程如
總結:continue終止當前迴圈進行下次迴圈,break終止整個迴圈
4、算術運算子
+ - * / % ** //
加 減 乘 除 取餘 乘方 取整數商
5、字串
name = "馬大帥"if "馬" in name : print("ok")else: print("error")
'馬大帥' 稱為字串
'馬' 成為一個字元
'馬大'或者'大帥'稱為子字串,也可以叫做子序列,注意這裡的字元要連續的,而'馬帥'不能稱之為子字串
6、成員運算:
判斷某個字元在某個字串用in 或者not in
name = "馬大帥"if "嗎" not in name : print("ok")else: print("error")
7、布爾值
if語句和while語句都使用布爾值作為條件。
布爾值只有兩種情況:
真 True 假 False
if 條件判斷語句 功能代碼某塊
這裡的條件判斷語句最終會產生一個布爾值,或者是True 或者False
name = "馬大帥"p ="嗎" not in nameprint(p)if p: print("ok")else: print("error")
輸出結果
Trueok
布爾操作符:and or not
在 Python 看來,只有以下內容會被看作假(注意冒號括弧裡邊啥都沒有,連空格都不要有!):False None 0 "" '' () [] {}
其他一切都被解釋為真!
舉個例子
i = 10while i: print ("我愛學習!")print("end")
輸出結果
我愛學習!我愛學習!我愛學習!我愛學習!我愛學習!我愛學習!我愛學習!我愛學習!我愛學習!...(這裡代表一直輸出"我愛學習")
這個程式會一直輸出"我愛學習",除非按下CTRL+C停止執行程式
而print("end")語句永遠不會被執行到。
再比如
i = 10while i: print ("我愛學習!",i) i = i -1print("end")
輸出結果
我愛學習! 10我愛學習! 9我愛學習! 8我愛學習! 7我愛學習! 6我愛學習! 5我愛學習! 4我愛學習! 3我愛學習! 2我愛學習! 1end
通過觀察"我愛學習"後的數字變化,我們可以看到,這個迴圈的執行過程,當i迴圈到0時 ,即while 0 :,0為False,終止迴圈。開始執行
print("end")語句。
8、比較子:判斷大小符號
== 等於
> 大於
< 小於
>= 大於等於
<= 小於等於
!= 不等於
9、運算的優先順序
先計算括弧內,複雜的運算式推薦使用括弧
一般的執行順序:從左至右
布爾運算優先順序
從高到低:not and or
例子:
user = 'nicholas'psswd ='123'v = user == 'nicholas' and passwd == '123' or 1 == 2 and pwd == '9876'print(v)
分析:
v = true and true or
此時不用繼續計算即可得出v為真的結果,不用考慮布爾運算的優先順序,注意這個運算是從左至右的,**而非看到and自動進行運算而後從左至右運算**
一些結論:
從左至右
(1)第一個運算式 or
True or ————>>得出結果True
(2)第一個運算式 and
True and ————>>繼續運算
(3)第一個運算式 or
False or ————>>繼續運算
(4)第一個運算式 and
False and ————>>得出結果False
即**短路邏輯**
短路邏輯
運算式從左至右運算,若 or 的左側邏輯值為 True ,則短路 or 後所有的運算式(不管是 and 還是 or),直接輸出
or 左側運算式 。
運算式從左至右運算,若 and 的左側邏輯值為 False ,則短路其後所有 and 運算式,直到有 or 出現,輸出 and 左側運算式到
or 的左側,參與接下來的邏輯運算。
若 or 的左側為 False ,或者 and 的左側為 True 則不能使用短路邏輯。
10、賦值運算子
>=簡單的賦值運算子 c = a + b 將 a + b 的運算結果賦值為 c
+=加法賦值運算子c += a 等效於 c = c + a
-=減法賦值運算子c -= a 等效於 c = c - a
*=乘法賦值運算子c *= a 等效於 c = c * a
/=除法賦值運算子c /= a 等效於 c = c / a
%=模數賦值運算子c %= a 等效於 c = c % a
**=冪賦值運算子c **= a 等效於 c = c ** a
//=取整除賦值運算子c //= a 等效於 c = c // a
二、基礎資料型別 (Elementary Data Type)
(1)數字 int
a = 1
a = 2
int整型(整數類型)
python3中用int表示,沒有範圍
python2中int有一定範圍
超過一定範圍,Python2中有長整型即long
python3中只有整型,用int,取消了long類型
**①**、int()將字串轉換為數字
a = "123"type(a)b = int(a)print(b)type(b)
輸出
<class 'str'>123<class 'int'>
type()即可查看變數類型
但是
a = "123n"
b = int(a)
此時是無法用int()轉換字串為數位。
num = "c"v = int(num,base = 16) print(v)
注釋: v = int(num,base = 16) 將num以16進位看待,將num轉為10進位的數字
②bit_lenght
當前數位二進位,至少用n位表示
age = 5r = age.bit_length()#當前數位二進位,至少佔用了n位表示print(r)
輸出結果
3
即5在二進位中表示為101,至少需要3個位置來表示
(2)字串 str
a ='hello'
a= 'ssssdda'
字串函數介紹
a--capitalize()
# capitalize() 首字母大寫test = "lingou"v1 = test.capitalize( )print(v1)
輸出結果
Lingou
b--casefold( )、lower()
#lower() 所有變小寫
# casefold( ) 所有變小寫,與lower相比casefold更牛逼,很多未知(不是英文的,如法文、德文等)的對相應變小寫
#lower() 方法只對ASCII編碼,也就是‘A-Z’有效,對於其他語言(非漢語或英文)中把大寫轉換為小寫情況只能用 casefold() 方法。
test = "LinGou"v2 = test.casefold( )print(v2)v3 =test.lower()print(v3)
輸出結果
lingoulingou
c--center( )
#center( ) 設定寬度,並將內容置中,這裡的"*"可以不寫,預設為空白。
#這裡的30是總寬度,單位位元組
test = "LinGou"v4 = test.center(30,"*" )print(v4)
輸出結果
************LinGou************
空白情況
test = "LinGou"v5 = test.center(30 )print(v5)
輸出結果
LinGou
注意這裡的"LinGou"左右兩邊都有空白的位元組
d--count( )
#count( ) 去字串中尋找,尋找子序列的出現次數
#count(sub[, start[, end]])
#count( 子序列,尋找的開始位置,尋找的結束位置)
#count( sub, start=None, end=None) None預設表示此參數不存在
test = "LinGouLinGengxin"v6 = test.count("in" )print(v6)v7 = test.count("in",3,6)#這裡的3,6 是對字串"LinGouLinGengxin"的索引編碼,從第三個開始到第六個結束#L i n G o u L i n G e n g x i n#0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15print(v7)v8 = test.count("in",3)#從第3個位置開始找print(v8)
輸出結果
302
e--endswith()、startswith()
#endswith() 以什麼什麼結尾
#startswith()以什麼什麼開始
test = "LinGouLinGengxin"v9 = test.endswith("in" )v10 = test.startswith("in")print(v9)print(v10)
輸出結果
TrueFalse
f--find()、index()
#find()從開始往後找,找到第一個之後,擷取其索引位置
#index()功能同上,index找不到,報錯,一般建議用find()
test = "LinGouLinGengxin"v11 = test.find("in" )v12 = test.find("XING" )v13 = test.index("in")# v14 = test.index("XING" )print(v11)print(v12)print(v13)#print(v14)
輸出結果
1-11
取消v14 = test.index("XING" )和print(v14)的注釋後運行程式會直接報錯
因為index找不到"XING"
g--format()
#format()格式化,將一個字串中的預留位置替換為指定的值
# { }就是預留位置,通過format將佔位符替換為指定的值
test = "I am {name}"print(test)v15 = test.format(name = "LinGou" )print(v15)
輸出結果
I am {name}I am LinGou
-第二個
test = "I am {name},age{a}"print(test)v16 = test.format(name = "LinGou",a = 19 )print(v16)
輸出結果
I am {name},age{a}I am LinGou,age19
-第三個
test = "I am {0},age{1}"print(test)v17 = test.format("LinGou",19 )
print(v17)
輸出結果
I am {0},age{1}I am LinGou,age19
當預留位置有數字代表,format函數裡不再需要具體的name =""
這裡是按照先後順序替換的。
第四個
#format_map()格式化,傳入的值
# 書寫格式{"name":"LinGou","a":19}
test = "I am {name},age {a}"print(test)v18 = test.format_map({"name":"LinGou","a":19} )v19 = test.format(name = "LinGou",a = "19")print(v18)print(v19)
輸出結果
I am {name},age {a}I am LinGou,age 19I am LinGou,age 19
f--isalnum( )
#isalnum( )字串中是否只包含 字母和數字
test = "LinGou"v20 = test.isalnum( )print(v20)test2 = "LinGou+"v21 = test2.isalnum( )print(v21)
輸出結果
TrueFalse