1.資料型別
1) int: 1,2,3
用於計算。
2) bool:true,false
使用者判斷。
3) str: 'fjdsal' '二哥','`13243','fdshklj'
'戰三,李四,王二麻子。。。。'
儲存少量資料,進行操作。
4) list:[1,2,3,'泰哥','12353234',[1,2,3]]
儲存大量的資料。
5) 元祖:(1,2,3,'第三方',)
唯讀。6) dict:字典
字典7) 集合:
2.int
ctrl+左:檢視類、方法
》i=5
print(i.bit_length()) --->3 # 轉換成二進位制後最小位數
bit_length
1 0000 0001 1
2 0000 0010 2
3 0000 0011 2
3.bool
1) int ----> str
i = 1
s = str(i) # 無條件
2) str ----> int
s = '123'
i = int(s) # 只能是數字
3) int ----> bool
i = 3
b = bool(i) # 只要是0-->false,非0就是true
4) bool----> int # true-->1,false-->0
ps:while true:
pass
while 1: # 效率高
pass
5) str ---> bool
s = "" ---> false
s = "0" ---> true # 非空字串都是true
列表、字典都可轉換成bool值。
4.str
#字串的索引與切片
s = 'a b c d e f g'
0 1 2 3 4 5 6
-7 -6 -5 -4 -3 -2 -1
1) 索引:
s1 = s[0] --> a
s2 = s[2] --> c
s3 = s[-1] --> g
s4 = s[-2] --> f
2) 切片:
s5 = s[0:3] -->abc # 顧頭不顧尾
全取: s5 = s[0:]
s6 = s[ : ]
跳著取: s[首:尾:步長]
s7 = s[0:5:2] --> ace
倒著取: s8 = s[4:0:-1] --> edcb #取不到a
s9 = s[3: :-1] --> dcba #能取到a
倒+跳 : s10= s[3: :-2] --> db
倒取完: s11= s[-1::-1]
s12= s[ ::-1] --> gfedcba
#字串的操作
s = 'a b c d e f'
1) 首字母大寫,其他全小寫:
s1 = s.capitalize() --> abcdef
2) 全大寫:
s2 = s.upper() --> abcdef
3) 全小寫:
s3 = s.lower() --> abcdef
4) 輸入驗證碼,不區分大小寫:
》s = 'aceq1'
you = input('請輸入驗證碼,不區分大小寫')
if s.upper() == you.upper():
print('輸入成功')
else:
print('請重新輸入')
5) 大小寫翻**
s4 = s.swapcase() --> abcdef
6) 每個隔開(特殊字元或者數字)的單詞首字母大寫:
》s = 'alex*egon-wusir'
s5 = s.title()
print(s5) --> alex*egon-wusir
》s = 'fade,crazy*w4rri0r_songsong node_3'
s6 = s.title()
print(s6) --> fade,crazy*w4rri0r_songsong node_3
7) 居中,空白填充:
s7 = s.center(20,'~')--> ~~~~~~~abcdef~~~~~~~
8) \t:
前邊0~3個字元時,用空格補齊到4個
前邊4~7個字元時,用空格補齊到8個
前邊8~11個字元時,用空格補齊到12個
.......
9) 公共方法 查元素個數:
s = '1234567'
l = len(s) --> 7
s = '12as二哥'
l = len(s) --> 6
10) 判斷是否是以什麼為開頭結尾
》s = 'alexwusir'
s8 = s.startswith('alex') --> true
s9 = s.startswith('e',2,5) --> true # 可結合切片,空缺省全部
結尾 endswith
》if s8:
pass
elif s.startswith('bl'):
pass
11) 尋找元素對應下標:
find() : s = 'abcd'
s1= s.find('b') --> 1
s2= s.find('bc') --> 1 # print(s2,type(s2)) --> 1
find 通過元素找索引,找不到返回-1
可結合切片
index(): index通過元素找索引,找不到報錯
首選find()
12) 去空格:
strip(): 預設刪除前後空格,也可用來刪指定元素,前後同時進行,直到卡住。
s = '*a%bcd%'
s1= s.strip('%*') --> a%bcd #與順序無關,a後的%被a卡住過不來
rstrip(): 從右刪
lstrip(): 從左刪
》username = input('請輸入名字:').strip() #名字前後誤輸入空格也可識別
13)數個數,返回個數,可結合切片:
s.count('a')
s.count('ab')
14)分割,拆分【用於資料型別轉換str-->list列表】:
》s = ';abc;def;gh'
s1 = s.split(';') --> ['','abc','def','gh']
若是以空格分,則split()裡面什麼都不加。
以什麼分,結果裡什麼就沒了。
》s = ';alex;wusir;taibai'
s2 = s.split('a') --> [';','lex;wusir;t'.'ib','i']
15) format的三種玩法 格式化輸出:
》s = '我叫{},今年{},愛好{},再說一下我叫{}'.format('太白',36,'girl','太白')
print(s)
》name = input('請輸入名字:')
s = '我叫,今年,愛好,再說一下我叫'.format(name,36,'girl')
print(s)
》name = input('請輸入名字:')
s = '我叫,今年,愛好,再說一下我叫'.format(age=18,name=name,hobby='girl') # 順序可打亂
print(s)
16) 替換:
s = '123ab456ab789'
s1= s.replace('ab','cd') --> 123cd456cd789 # 預設全替
s2= s.replace('ab','cd',1) --> 123cd456ab789 # 替換一次
17) is系列:
name = 'jinxin123'
print(name.isalnum()) # 判斷字元是否由字母或數字組成
print(name.isalpha()) # 判斷字元是否只由字母組成
print(name.isdigit()) # 判斷字元是否只由數字組成
返回t/f
5.for迴圈
s = 'abcdef'
for i in s:
print(i) -->abcdef # 豎著
6.if ...in...
s = 'abcsbdef'
if 'sb' in s:
print('有敏感詞') # 也可not in
Python學習筆記 day0
本部落格標題設計敏感關鍵字0day,違反相關法律法規,現已處理。1.輸入 raw input 這個可以輸入一行。和c 不同,它不能讀取單個整數 我的意思是,它是以字串儲存的。所以沒有什麼諸如 d這種東西。name raw input 這樣就可以讀入乙個字串,相當於gets name 考慮到人性化設計...
前端學習筆記 day0
utf 8 萬國碼 開發時我們使用的字符集都是utf 8 gb2312 國標 通過meta標籤設定網頁字符集,避免亂碼問題。此處為設定字符集,避免亂碼 用來設定網頁的元資料,底層資料,網頁的屬性。元資料不是給使用者看的 它是自結束標籤 常用的屬性 name指定資料的名稱 content指定資料的內容...
python闖關 python闖關 Day05
乙個簡單的 選單 usr bin env python coding utf 8 mymenu 動物 貓 黃貓 花貓 狗 二哈 金毛 植物 樹 大樹 小樹 草 綠草 矮草 menu list list mymenu.keys while true print 編號 center 50,for i i...