1、執行 Python 腳本的兩種方式
1.python 進入解釋器 2.python 1.py 執行文件 limux里 ./1.py
2、簡述位、字節的關系
8位1個字節。計算機處理以字節為單位,存儲以位為單位。
3、簡述 ascii、unicode、utf-‐8、gbk 的關系
Ascii 最開始 Unicode 接着出現 utf-‐8 unicode壓縮版 ascii 是最早美國用的標准信息交換碼,把所有的字母的大小寫,各種符號用 二進制來表示,共有256中,加入些拉丁文等字符,1bytes代表一個字符, Unicode是為了統一世界各國語言的不用,統一用2個bytes代表一個字符,可以表達2**16=65556個,稱為萬國語言,特點:速度快,但浪費空間, 可以用在內存處理中,兼容了utf-8,gbk,ASCII, utf-8 為了改變Unicode的這種缺點,規定1個英文字符用1個字節表示,1個中文字符用3個字節表示。 特點;節省空間,速度慢,用在硬盤數據傳輸,網絡數據傳輸,相比硬盤和網絡速度,體現不出來的, gbk 是中文的字符編碼,用2個字節代表一個字符。
4、請寫出 “李傑” 分別用 utf-‐8 和 gbk 編碼所占的位數
utf-‐8:6 gbk :4
5、Pyhton 單行注釋和多行注釋分別用什么?
單行注釋:# 多行注釋:""" """
6、聲明變量注意事項有那些?
變量定義的規則:
1.變量名只能是 字母、數字或下划線的任意組合
2.變量名的第一個字符不能是數字
3.以下關鍵字不能聲明為變量名:
['and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'exec', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'not', 'or', 'pass', 'print', 'raise', 'return', 'try', 'while', 'with', 'yield']
7、如有一下變量 n1 = 5,請使用 int 的提供的方法,得到該變量最少可以用多少個二進制位表示?
bt_length
8、布爾值分別有什么?
True , False 字符串 "" ==>假 " " ==>真 數字: 0 ==>假 其他 ==>真 a = "123" a = int(a) a = 123 b = str(a) a = 123 p1 = bool(a) b="i" b2 = bool(b)
9、閱讀代碼,請寫出執行結果a = "alex"
b=a.capitaliz()
print(a)
print(b)
請寫出輸出結果:
alex
Alex
10、寫代碼,有如下變量,請按照要求實現每個功能name = " aleX"
name = " aleX"
a.移除 name 變量對應的值兩邊的空格,並輸入移除后的內容
print(name.strip())
b.判斷 name 變量對應的值是否以 "al" 開頭,並輸出結果
print(name.startswith("al"))
c.判斷 name 變量對應的值是否以 "X" 結尾,並輸出結果
print(name.endswith("X"))
d.將 name 變量對應的值中的 “l” 替換為 “p”,並輸出結果
print(name.replace("l","p"))
e.將 name 變量對應的值根據 “l” 分割,並輸出結果。
print(name.split("l"))
print(name.partition("l"))
f.請問,上一題 e 分割之后得到值是什么類型(可選)?
字典
g.將 name 變量對應的值變大寫,並輸出結果
print(name.upper())
h.將 name 變量對應的值變小寫,並輸出結果
print(name.lower())
i.請輸出 name 變量對應的值的第 2 個字符?
l = len(name) r = range(0,l) for nn in r: if nn == 1: print(name[nn])
j.請輸出 name 變量對應的值的前 3 個字符?
print(name[0:3])
k.請輸出 name 變量對應的值的后 2 個字符?
print(name[3:5])
l.請輸出 name 變量對應的值中 “e” 所在索引位置?
l = len(name) r = range(0,l) for nn in r: if name[nn] == "e": print("e的索引位置為:" + str(nn)) m.獲取子序列,僅不包含最后一個字符。如: oldboy 則獲取 oldbo; root 則獲roo print(name[0:4])
21、字符串是否可迭代對象?如可以請使用 for 循環每一個元素?
for i in 值: print(i) 可迭代對象 == 可以被for循環獲取 類,類型 str 各種方法 ... 對象,根據str類型創建一個對象。s1 int ... a = 123
22、請用代碼實現:
a.利用下划線將列表的每一個元素拼接成字符串,li = "alexericrain"
rint("_".join(li))
b.利用下划線將列表的每一個元素拼接成字符串,li = ['alex', 'eric', 'rain'] (可選)
print("_".join(li))
23、Python2 中的 range 和 Python3 中的 range 的區別?
Python2:range 立即創建 xrange for循環時一個一個創建 Python3:range for循環時一個一個創建 for i in range (0,100,1) : print (i) for i in range (100,0,-1) : print (i)
24、實現一個整數加法計算器:
如:
content = input('請輸入內容:') # 如 : 5+9 或 5+ 9 或 5 + 9
v = input(">>>")
v1,v2 = v.split("+")
v1 = int(v1)
v2 = int(v2)
print(v1 + v2)
24、計算用戶輸入的內容中有幾個十進制小數?幾個字
如:
content = input('請輸入內容:') # 如:asduiaf878123jkjsfd-‐213928
content = input('請輸入內容:') c1 = 0 c2 = 0 for item in content: if item.isdecimal(): c1 += 1
elif item.isalpha(): c2 += 1
print("字母數量:" + str(c2) + "\n數字數量:" + str(c1))
26、簡述 int 和 9 等數字 以及 str 和 "xxoo" 等字符串的關系?
int = 9 str = "xxoo" 類和對象的關系
27、制作趣味模板程序
需求:等待用戶輸入名字、地點、愛好,根據用戶的名字和愛好進行任意現實如:敬愛可親的 xxx,最喜歡在 xxx 地方干 xxx
v = input("請輸入輸入名字、地點、愛好並用“+”隔開:") v1,v2,v3 = v.split("+") content = "敬愛可親的{name},最喜歡在{place} 地方干{like}" c = content.format(name = v1,place = v2,like = v3) print(c)
28、制作隨機驗證碼,不區分大小寫。流程:
-‐ 用戶執行程序
-‐ 給用戶顯示需要輸入的驗證碼
-‐ 用戶輸入的值
用戶輸入的值和顯示的值相同時現實正確信息;否則繼續生成隨機驗證碼繼續等待用戶輸入生成隨機驗證碼代碼示例:
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 code = check_code() while True: code = check_code() print(code) v = input("請輸入驗證碼:") if v == code: print("輸入正確!!!") break
else: print("請重新輸入:")
29、開發敏感詞語過濾程序,提示用戶輸入內容,如果用戶輸入的內容中包含特殊的字符: 如 "蒼老師" "東京熱",則將內容替換為 ***
c = input(">>>") c = c.replace("蒼老師","***") c = c.replace("東京熱","***") print(c)
30、制作表格
循環提示用戶輸入:用戶名、密碼、郵箱 (要求用戶輸入的長度不超過 20 個字符,如果超過則只有前 20 個字符有效) 如果用戶輸入 q 或 Q 表示不再繼續輸入,將用戶輸入的內容以表格形式大隱
s = ""
while True: name = input("用戶名:") if len(name) > 20: print("用戶名長度不超過20字符,如果超過則只有前20個字符有效") if name == "q" or name == "Q": break pwd = input("密碼:") if pwd == "q" or pwd == "Q": break mail = input("郵箱:") if mail == "q" or mail == "Q": break temp = "用戶名\t密碼\t郵箱\n{0}\t{1}\t{2}\n" t = temp.format(name,pwd,mail) s = s + t print(s.expandtabs(20))