算術運算:+ - * / ** % //javascript
賦值運算:= += -= *= /= %= //=java
算術運算符和賦值運算符結果是值python
比較運算:== < > <= >= != <> linux
成員運算:in not in(判斷某個東西是否在某個東西里麪包含)git
邏輯運算:and or not 正則表達式
比較運算符、成員運算符、邏輯運算符結果是布爾值api
and or執行順序:dom
先計算括號內的ide
從前到後 :結果是True,遇到or,不往下走了,結果爲True優化
結果是True,遇到and,繼續往下走
結果是False,遇到or,繼續往下走
結果是False,遇到and,不往下走了,結果爲False
對於比較運算(== < > <= >= != <> )或in 、not,結果都是布爾值
整形 int
字符串 str
列表 list
元祖 tuple
字典 dict
布爾值(真True 假False)bool
int 整形 long長整形
python3裏全部的整形不管大小、長短都是int,python2裏有int和long
1 a = '123' 2 print(type(a),a) 3 b = int(a) 4 print(type(b),b)
1 num = 'a' 2 b = int(num, base=16) 3 print(b)
1 age = 5 2 r = age.bit_length() 3 print(r)
test='alext'
v = test.center(20,'*')
print(v)
20代指總長度,*表明空白位置填充,一個字符,無關緊要
test = 'alextlert'
v = test.find('rt', 5, 8)
print(v)
5,8表示的是大於等於5小於8
test = 'i am {name}, age {a}'
v = test.format(name = 'alex', a=19)
print(v)
test = 'i am {0}, age {1}'
v = test.format('alex', 19)
print(v)
test = 'i am {name}, age {a}'
v = test.format_map({'name': 'alex', 'a': 19})
print(v)
test = 'qwe123_=='
v = test.isalnum()
print(v)
test = 'username\temail\tpassword\npiaopiao\t1101@qq.com\t12345\npiaopiao\t1101@qq.com\t12345'
v = test.expandtabs(20)
print(v)
-isalpha 判斷是不是字母&漢字,輸出結果爲布爾值
test = '②'
v1 = test.isdecimal()
v2 = test.isdigit()
print(v1, v2)
\t 製表符 \n換行
test = 'asd\t'
v1 = test.isprintable()
print(v1)
test = 'asd\t '
v1 = test.isspace()
print(v1)
test = 'we are family we are family'
v = '>'.join(test)
print(v)
test = 'we are family'
v = test.ljust(30, "*")
print(v)
test = ' asfjk '
v = test.rstrip()
print(v)
test = 'asfjk'
v = test.rstrip('rtk')
print(v)
v = "ashkljbvnkio"
m = str.maketrans('aeiou', '12345')
v1 = v.translate(m)
print(v1)
test = "ashklsjbvnskio"
v1 = test.partition('s')
v2 = test.rpartition('s')
v3 = test.split('s')
v4 = test.split('s', 2)
v5 = test.rsplit('s', 2)
print(v1, v2, v3, v4, v5)
test = 'asd\nsdfgk\njkleror\n'
v = test.splitlines(True)
print(v)
test = 'asd\nsdfgk\njkleror\n'
v = test.startswith('ab')
print(v)
test = 'asd\nsdfgk\njklERor\n'
v = test.swapcase()
print(v)
test = 'cyxcyxcyxcyx'
v = test.replace('yx', 'pp', 2)
print(v)
test = 'asd\nsdfgk\njklERor\n'
v = test[10]
print(v)
test = 'asd\nsdfgk\njklERor\n'
v = test[0:5]
print(v)
test = 'asd\nsdfgk\njklERor\n'
v = test[0:-1]
print(v)
test = 'lalala'
v = len(test)
print(v)
注:len join for循環 索引 切片在其餘數據類型中也能用
test = '獲取當前字符串由幾個字符組成'
for cyx in test:
print(cyx)
也能夠用while循環實現
test = '獲取當前字符串由幾個字符組成'
index = 0
while index < len(test):
cyx = test[index]
print(cyx)
index +=1
for cyx in test:
print(cyx)
break
for cyx in test:
continue
print(cyx)
11個必須記住:join split find strip upper lower replace len for 循環 索引 切片
字符串在內存中一旦建立就不可修改,若是要修改或拼接,那在內存中會從新建立一個新的字符串(全部語言都是這樣的)
在python2中當即建立100個數,但在python3中不會當即建立,只有在對其進行for循環時纔會建立,屬於一個優化機制
v = range(100)
print(v)
v = range(100)
for item in v:
print(item)
v = range(0, 100, 5)
for item in v:
print(item)
5是步長
e.g.將文字對應的索引打印出來
test = input('>>>')
for item in range(0, len(test)):
print(item, test[item])
一、執行 Python 腳本的兩種方式
python 解釋器+文件名
pyhton 解釋器直接運行
./1.py(linux系統,賦予可執行權限)
二、簡述位、字節的關係
1字節=8位
計算機處理時以一個字節爲單位,存儲時以位爲單位
三、簡述 ascii、unicode、utf-‐八、gbk 的關係
ascii 8位
Unicode 至少16位
utf8 能用多少位就用多少位
gbk 適用於中文
四、請寫出 「李傑」 分別用 utf-‐8 和 gbk 編碼所佔的位數
6字節
4字節
python 3計算字符 len(‘理解’)=2 for 循環時輸出的時兩個字符
python2計算字節 len(‘理解’)= 6 for循環時輸出的是六個字節
五、Pyhton 單行註釋和多行註釋分別用什麼?
#
""" """(推薦用
''' '''
六、聲明變量注意事項有那些?
數字、字母、下劃線
不能數字開頭
必定不能跟關鍵字重複
推薦不能用python內置的東西,由於一用會把原來的內置東西覆蓋掉
七、若有變量 n1 = 5,請使用 int 的提供的方法,獲得該變量最少能夠用多少個二進制位表示?
n1 = 5
v = n1.bit_length()
print(v)
八、布爾值分別有什麼?
True False(python裏首字母要大寫)
true false(javascript裏首字母小寫)
''空字符串是假
' '只要字符串裏有東西即爲真
0假
其餘真
-int 字符串轉換爲數字
-str 能夠實現數字轉換成字符串
-bool 數字或 字符串轉換爲布爾值
總結:新值=int/str/bool(...)
九、閱讀代碼,請寫出執行結果
a = "alex"
b = a.capitalize()
print(a)
print(b)
請寫出輸出結果:
alex Alex
十、寫代碼,有以下變量,請按照要求實現每一個功能
name = " aleX"
a. 移除 name 變量對應的值兩邊的空格,並輸入移除後的內容
name = " aleX"
v = name.strip()
print(v)
b. 判斷 name 變量對應的值是否以 "al" 開頭,並輸出結果
name = " aleX"
v = name.startswith('al')
print(v)
c. 判斷 name 變量對應的值是否以 "X" 結尾,並輸出結果
name = " aleX"
v = name.endswith('X')
print(v)
d. 將 name 變量對應的值中的 「l」 替換爲 「p」,並輸出結果
name = " aleX"
v = name.replace('l', 'p')
print(v)
e. 將 name 變量對應的值根據 「l」 分割,並輸出結果。
name = " aleX"
v = name.partition('l')
print(v)
name = " aleX"
v = name.split('l')
print(v)
f. 請問,上一題 e 分割以後獲得值是什麼類型(可選)?
帶[]是列表
g. 將 name 變量對應的值變大寫,並輸出結果
name = " aleX"
v = name.upper()
print(v)
h. 將 name 變量對應的值變小寫,並輸出結果
name = " aleX"
v = name.lower()
print(v)
name = " aleX"
v = name.casefold()
print(v)
i. 請輸出 name 變量對應的值的第 2 個字符?
name = " aleX"
v = name[1]
print(v)
j. 請輸出 name 變量對應的值的前 3 個字符?
name = " aleX"
v = name.rstrip('eX')
print(v)
k. 請輸出 name 變量對應的值的後 2 個字符?
name = " aleX"
v = name.lstrip(' al')
print(v)
l. 請輸出 name 變量對應的值中 「e」 所在索引位置?
name = " aleX"
for item in range(0, len(name)):
if name[item] == 'e':
print(item)
else:
pass
m. 獲取子序列,僅不包含最後一個字符。如: oldboy 則獲取 oldbo; root 則獲取 roo
test = input('>>>')
v = test[0: -1]
print(v)
十一、字符串是否可迭代對象?如能夠請使用 for 循環每個元素?
是
name = " aleX"
for item in name:
print(item)
可迭代對象=能夠被for進行循環獲取
類、類型:str int bool
對象:根據str類型建立一個對象,對象賦值給s1變量
s1 = 'alex'
十二、請用代碼實現:
a. 利用下劃線將列表的每個元素拼接成字符串,li = "alexericrain"
b. 利用下劃線將列表的每個元素拼接成字符串,li = ['alex', 'eric', 'rain'](可選)
li = 'alexericrain'
v = '_'.join(li)
print(v)
li = ['alex', 'eric', 'rain']
v = '_'.join(li)
print(v)
1三、Python2 中的 range 和 Python3 中的 range 的區別?
在python2中range直接建立數放在內存裏,xrange能夠for循環一個建立時一個
但在python3中不會當即建立,只有在對其進行for循環時纔會建立
for i in range(100, 0, -1) 從大到小建立
1四、實現一個整數加法計算器:
如:
content = input('請輸入內容:') # 如: 5+9 或5+ 9或 5 + 9
content = input('請輸入內容:')
v1, v2 = content.split('+')
v1 = int(v1)
v2 = int(v2)
v = v1 + v2
print(v)
1五、計算用戶輸入的內容中有幾個十進制小數?幾個字母?
如:
content = input('請輸入內容:') # 如:asduiaf878123jkjsfd-‐213928
b = input('請輸入內容')
count = 0
for item in b:
if item.isalpha() == True or item.isdecimal() == True:
count =count + 1
else:
count = count
print(count)
pycharm裏code 欄裏有一個reformat code能夠糾正格式
1六、簡述 int 和 9 等數字 以及 str 和 "xxoo" 等字符串的關係?
int str屬於類
9 "xxoo" 屬於對象
1七、製做趣味模板程序
需求:等待用戶輸入名字、地點、愛好,根據用戶的名字和愛好進行任意現實
如:敬愛可親的 xxx,最喜歡在 xxx 地方幹 xxx
name = input('用戶名')
place = input('地點')
hobby = input('愛好')
test = '敬愛可親的{0},最喜歡在{1}幹{2}'
v = test.format(name, place, hobby)
print(v)
18.製做隨機驗證碼,不區分大小寫
流程:
-‐ 用戶執行程序
-‐ 給用戶顯示須要輸入的驗證碼
-‐ 用戶輸入的值
用戶輸入的值和顯示的值相同時顯示正確信息;不然繼續生成隨機驗證碼繼續等待用戶輸入生成隨機驗證碼代碼示例:
def check_code():
import random
checkcode = ''
for i in range(4):
current = random.randrange(0,4)
if current != i:
temp = chr(random.randint(65,90))
else:
temp = random.randint(0,9)
checkcode += str(temp)
return checkcode
count = 0
while count == count:
code = check_code()
print(code)
user = input('請輸入驗證碼:')
v = user.upper()
if v == code:
print('歡迎進入')
break
else:
count = count + 1
def check_code():
import random
checkcode = ''
for i in range(4):
current = random.randrange(0,4)
if current != i:
temp = chr(random.randint(65,90))
else:
temp = random.randint(0,9)
checkcode += str(temp)
return checkcode
while True:
code = check_code()
print(code)
user = input('請輸入驗證碼:')
v = user.upper()
if v == code:
print('歡迎進入')
break
1九、開發敏感詞語過濾程序,提示用戶輸入內容,若是用戶輸入的內容中包含特殊的字符:
如 "蒼老師" "東京熱",則將內容替換爲 ***
content = input('>>>')
v = content.replace('蒼老師', '***')
v2 = v.replace('東京熱', '***')
print(v2)
20、製做表格
循環提示用戶輸入:用戶名、密碼、郵箱 (要求用戶輸入的長度不超過 20 個字符,若是超過則只有前 20 個字符有效)若是用戶輸入 q 或 Q 表示再也不繼續輸入,將用戶輸入的內容以表格形式打印
s = ''while True: v1 = input('用戶名:') if v1 == 'q' or v1 == 'Q': break v2 = input('密碼:') if v2 == 'q' or v2 =='Q': break v3 = input('郵箱:') if v3 == 'q' or v3 == 'Q': break if len(v1) > 20: v1 = v1[0:20] if len(v2) > 20: v1 = v1[0:20] if len(v3) > 20: v1 = v1[0:20] template = '{0}\t{1}\t{2}\n' v = template.format(v1, v2, v3) s =s + v print(s.expandtabs(20))