Python 基礎知識之總結(5)

2021-09-13 23:32:53 字數 2540 閱讀 4215

"""

第五天學習,複習前四天的知識點,並在原有知識點基礎上,引入新的內容。

"""#1.變數賦值,切記,變數命名不可以關鍵字命名

a = 10

b = "hello world"

c = ["a", "b", "c"]

d =

d[:2] = 10, 20

# 2.資料型別,可通過type()來獲取變數的型別

# 2.1 數字

a1 = 10 # 整型

b1 = 1.2 # 浮點型

c1, d1 = true, false # 布林型

# 2.2 字串

# 2.2.1字串多種賦值方式

str1 = 'hello world'

str2 = "hello world"

str3 = """

hello

world

"""# 2.2.2字串的方法

s = " hello, world "

s1 = ['127', '0', '0', '1']

s.upper() # 轉大寫

s.isdigit() # 判斷字串是否為數字

s.strip() # 去掉前後空格

s.split(",") # 以逗號切割為列表

".".join(s1) # 將序列中的元素以指定的字元連線生成乙個新的字串

s.index("h") # 字串h的下標是幾

s.count("l") # 統計字元在該字串**現的次數

"*".center(10, "=") # 以*為中心輸出10個=

s.startswith("h") # 字串是否以h開頭

s.endswith("a") # 字串是否以a結尾

# 2.2.3 字串操作符

s = "hello "

p = "world"

print(s + p) # 字串相加 "hello world"

print(s * 5) # 輸出5個字元 "hello hello hello hello hello "

print("a" in s) # 字元"a"是否在s中存在 false

print("h" in s) # 字元"h"是否在s中存在 true

print("a" not in s) # 字元"a"不在s中 true

print("h" not in s) # 字元"h"不在s中 false

2.2.4 字串索引和切片

z = "abcdefghi"

print(z[0]) # 字串列表中的第乙個字元

print(z[2]) # 字串列表中的第三個字元

print(z[-1]) # 字串列表中的倒數第乙個字元

print(z[-2]) # 字串列表中的倒數第二個字元

print(z[:3]) # 從開始取值到下標為3結束 不包含3

print(z[2:5]) # 下標從2開始到5結束 不包含5

print(z[2:-1]) # 下標從2開始到倒數第乙個結束 不包含倒數第乙個

print(z[-1::]) # 從倒數第乙個開始,到結束

print(z[::2]) # 步長為2 也就是間隔為2

print(z[::-1]) # 倒著輸出全部

""" 2、當天總結

(1).變數與賦值

(2).數字型,字元型的相關知識

數字型:整型:a = 10

浮點型:b = 1.23

布林型:c,d = true,false

字元型:

多種賦值

字串的方法:upper()、isdigit()、strip()、split()

join()、index()、count()、center()

startswith()、endswith()

字串操作符:+、*

成員關係:in 、not in , "a" in str

字串索引與切片:list[0]、list[-1]、list[:2]、list[::2]、list[0:3:-1]等

知識回顧

(1).索引 p[2]

(2).切片 p[:2]

(3).成員關係:in

(4).列表:a = ["1","2","3"]

(5).strip() 去掉字串前後空格

(6).變數、賦值:phone = "13812345678"

(7).and 和 or:and是且的關係;or是或的關係

(8).資料型別:布林型(true\false) 字串(str)

(9).幾種內建方法:dir()、help()、print()、len()、isdigit()、type()

(10).條件語句:if...else... if條件的語句塊,前邊的縮排四個空格或乙個tab鍵

"""

Python基礎知識之5

1.檔案的開啟與關閉 新建乙個檔案,檔名為 test.txt f open test.txt w 關閉這個檔案 f.close 2.檔案的讀寫 2.1 寫資料 write 示例如下 f open test.txt w f.write hello world,i am here f.close 2.2...

python基礎知識總結

1 算術運算子 運算子描述例項 加10 20 30 減 10 20 10 乘 10 20 200 除 10 20 0.5 取整除 返回除法的整數部分 商 9 2 輸出結果 4 取餘數 返回除法的餘數 9 2 1 冪 又稱次方 乘方,2 3 8 2 算術運算子 運算子描述 冪 最高優先順序 乘 除 取...

Python基礎知識總結

函式可變引數 可變引數允許你傳入0個或任意個引數,這些可變引數在函式呼叫時自動組裝為乙個tuple。defcalc number sum 0 for x in number sum sum x x return sum 直接呼叫 print calc 5,5,5 列表引數呼叫 num表示把num這個...