1. 程式人生 > >python 基礎知識練習題

python 基礎知識練習題

Coding 命名 制作 輸入 多行 with 愛好 文件 lob

技術分享圖片
# -*- coding:utf-8 -*-
# 1、判斷下列邏輯語句的True,False.

# 1)
a = 1 > 1 or 3 < 4 or  4 > 5 and 2 > 1  and 9 > 8  or 7 < 6
print(a)
True

# 2)
b = not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
print(b)
False

# 2、求出下列邏輯語句的值。

# 1)
c = 8 or 3 and 4 or 2 and 0 or
9 and 7 print(c) 8 # 2) d = 0 or 2 and 3 and 4 or 6 and 0 or 3 print(d) 4 # 3、下列結果是什麽? # 1) print(6 or 2 > 1) # X為真,返回X,所以返回6 # 2) print(3 or 2 > 1) # X為真,返回X,所以返回3 # 3) print(0 or 5 < 4) # X為假,返回y,所以返回False # 4) print(5 < 4 or 3) # X為假,返回y,所以返回3 # 5) print(2 > 1 or 6) # X為真,返回x,所以返回True
# 6) print(3 and 2 > 1) # X為真,返回y,所以返回True # 7) print(0 and 3 > 1) # X為假,返回x,所以返回0 # 8) print(2 > 1 and 3) # X為真,返回y,所以返回3 # 9) print(3 > 1 and 0) # X為真,返回y,所以返回0 # 10) print(3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2) # 返回2 # 4. 簡述變量命名規範 # # (1)變量是由數字,字母,下劃線,任意組合. # (2)變量不能以數字開頭.
# (3)變量不能是python的關鍵字. # [‘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‘] # (4)變量可描述性. # (5)變量不能是中文. # (7)變量不能過長. # 5. name = input(“>>>”) name變量是什麽數據類型? # 字符串 str # 6. if條件語句的基本結構? # (1) # if 條件: # 代碼塊 # else: # 代碼塊 # # (2) # if 條件: # 代碼塊 # elif 條件: # 代碼塊 # else: # 代碼塊 # (3) # if 條件: # 代碼塊 # if 條件: # 代碼塊 # else: # 代碼塊 # # 7. while循環語句基本結構? # while True: # print() # # 8. 寫代碼:計算 1 - 2 + 3 ... + 99 中除了88意外所有數的總和? count = 1 sum = 0 while count < 100: if count % 2 == 1: sum = sum + count else: if count == 88: count += 1 continue else: sum = sum - count count += 1 print(sum) # 9. ?戶登陸(三次輸錯機會)且每次輸錯誤時顯示剩余錯誤次數(提示:使?字符串格式化) count = 3 while count > 0: name = input("請輸入用戶名=") password = input("請輸入密碼=") if name == "xue" and password == "123": print("登陸成功,用戶名為%s,請輸入密碼%d" % (name,int(password))) break else: count -= 1 if count == 0: b = input("登陸失敗,還想再嘗試嗎?/y") if b == "y": count = 3 else: print("還剩" + str(count) + "") else: print("NO") # 10. 簡述ascii、unicode、utf-8編碼關系? # # 計算機: # 儲存文件,或者是傳輸文件,實際上是010101010 # 計算機創建初期,美國,二進制 # # (對照表): # ascii # # 因為全球語言很多,ascii不足以存儲這麽多對應關系,創建了一個超級密碼本:萬國碼unicode # 8 位 == 1個字節. # hello h一個字符,e一個字符,he就不是一個字符. # 中國:中是一個字符,國是一個字符. # unicode : # 創建之初,16位,2個字節,表示一個字符. # 英文: a b c 六個字節 一個英文2個字節 # 中文 中國 四個字節 一個中文用2個字節 # 改成 32位,4個字節,表示一個字符. # a 01000001 01000010 01000011 00000001 # b 01000001 01000010 01100011 00000001 # 中 01001001 01000010 01100011 00000001 # 浪費資源. # 對Unicode進行升級: utf-8 # utf-8 用最少用8位數,去表示一個字符. # 英文: 8位,1個字節表示. # 歐洲文字: 16位,兩個字節表示一個字符. # 中文,亞洲文字: 24位,三個字節表示. # utf-16 用最少用16位數. # # gbk: # 國標,只能中國人自己用, 一個中文用16位,兩個字節表示. # # 11. 簡述位和字節的關系? # # utf-8 用最少用8位數,去表示一個字符. # 英文: 8位,1個字節表示. # 歐洲文字: 16位,兩個字節表示一個字符. # 中文,亞洲文字: 24位,三個字節表示. # utf-16 用最少用16位數. # # gbk: # 國標,只能中國人自己用, 一個中文用16位,兩個字節表示. # # 12. “?男孩”使?UTF-8編碼占??個字節?使?GBK編碼占?個字節? # 3*3 = 9(“?男孩”使?UTF-8編碼占?) # 3*2 = 6(使?GBK編碼占) # # 13. 制作趣味模板程序需求:等待?戶輸?名字、地點、愛好,根據?戶的名字和愛好進?任意現實 # 如:敬愛可親的xxx,最喜歡在xxx地??xxx name1 = input("輸入用戶名") space1 = input("輸入地點") habby1 = input("輸入愛好") a = ("敬愛可親的%(name)s,最喜歡在%(space)s,幹%(habby)s") %{"name":name1,"space":space1,"habby":habby1} print(a) # 14. 等待?戶輸?內容,檢測?戶輸?內容中是否包含敏感字符?如果存在敏感字符提示“存在敏感字符請重新輸?”, # 並允許?戶重新輸?並打印。敏感字符:“?粉嫩”、“?鐵錘” flag = True count = 1 while flag: content = input("輸入內容=") if "xiaofennen" in a or "datiechui" in content: print("存在敏感字符請重新輸?") else: print("輸入成功") flag = False count += 1 # 15. 單?註釋以及多?註釋? # # 單行註釋 適用於語句註釋 # 多行註釋 使用於段落註釋 # # 16. 簡述你所知道的Python3和Python2的區別? # # python2,python3區別大環境下: # python2: # 1,源碼都含有php,Java,C,等語言的規範陋習, # 2,重復代碼特別多. # # python3: # 源碼很規範,清晰,簡單,符合python的宗旨. # # # 17. 看代碼書寫結果: a = 1>2 or 4<7 and 8 == 8 print(a) True # 18.continue和break區別? # continue 是結束本次循環,進行下次循環 # break 跳出循環,break以下的代碼將不再執行。
View Code

python 基礎知識練習題