Python基礎題 - 2


1. 判斷下列邏輯語句的True,False.
(1) 1 > 1 or 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
True
(2) not 2 > 1 and 3 < 4 or 4 > 5 and 2 > 1 and 9 > 8 or 7 < 6
False

2. 求出下列邏輯語句的值。
(1) 8 or 3 and 46 or 2 and 0 or 9 and 7
8
(2) 0 or 2 and 3 and 4 or 6 and 0 or 3
4

3、下列結果是什么?
(1) 6 or 2 > 1
6
(2) 3 or 2 > 1
3
(3) 0 or 5 < 4
False
(4) 5 < 4 or 3
3
(5) 2 > 1 or 6
True
(6) 3 and 2 > 1
True
(7) 0 and 3 > 1
0
(8) 2 > 1 and 3
3
(9) 3 > 1 and 0
0
(10) 3 > 1 and 2 or 2 < 3 and 3 and 4 or 3 > 2
2

4. 簡述變量命名規范
變量要以數字、下划線或者字母任意組合,且不能以數字開頭;
不能以關鍵字命名;
一般不以中文以及漢字拼音命名;
常量一般全部是大寫;
命名要有意義,不宜過長。

5. name = input('>>>') name變量是什么數據類型?
字符串str

6. if條件語句的基本結構?
(1) if 條件:
代碼塊
(2) if 條件:
代碼塊
else:
代碼塊
(3) if 條件:
代碼塊
elif 條件:
代碼塊
...
else:
代碼塊
(4) if 條件:
代碼塊
if 條件:
代碼塊
else:
代碼塊
else:
代碼塊

7. while循環語句基本結構?
(1) while 條件:
代碼塊
(2) while 條件:
代碼塊
else:
代碼塊
PS:當遇到continue時,跳出本次循環,繼續下次循環;
當遇到break時,直接跳出while循環,且不再執行else語句。

8. 寫代碼:計算 1 - 2 + 3 ... + 99 中除了88以外所有數的總和?
count = 0
sum = 0
power = 0
while count < 99:
count += 1
sum = sum + count*(-1)**power
power += 1
print(sum + 88)


改:計算 1 - 2 + 3 ... - 99 中除了88以外所有數的總和?(正負號規律不變)
count = 0
sum = 0
power = 0
while count < 99:
if count == 87:
count += 1
continue
else:
count += 1
sum = sum + count*(-1)**power
power += 1
print(sum)

9. 用戶登陸(三次輸錯機會)且每次輸錯誤時顯示剩余錯誤次數(提示:使用字符串格式化)
user_name = 'admin'
password = 'admin'
count = 3
while count >= 1:
name = input('請輸入賬號:')
paw = input('請輸入密碼:')
count -= 1
if name == user_name:
if paw == password:
print('登陸成功!')
break
elif count == 0:
print('三次機會已用完,請12小時后重新登錄。')
else:
print('輸入密碼錯誤!還有%s次機會' % (str(count)))
elif count == 0:
print('三次機會已用完,請12小時后重新登錄。')
else:
print('該用戶不存在!還有%s次機會'% (str(count)))

10. 簡述ascii、unicode、utf-8編碼關系?
ASCII碼:美國最初編碼,只有7位,但防止以后增加,所以定為8位,可是一直沒有增加。
unicode編碼:萬國碼,為了解決全球化的文字問題而創建。一個中文用4個字節表示,太浪費(中文9萬多字)
utf-8編碼:一個中文3個字節表示
GBK編碼:只在國內使用,一個中文用2個字節表示

11. 簡述位和字節的關系?
8位(bit) == 一個字節(Byte)
bit,Byte,KB,MB,GB,TB之間的轉換關系:
8b == 1B
1024B == 1KB
1024KB == 1MB
1024MB == 1GB
1024GB == 1TB

12. “老男孩”使用UTF-8編碼占用幾個字節?使用GBK編碼占幾個字節?
“老男孩”使用UTF-8編碼占用9個字節
“老男孩”使用GBK編碼占用6個字節

13. 制作趣味模板程序需求:等待用戶輸入名字、地點、愛好,根據用戶的名字和愛好進行任意實現 如:敬愛可親的xxx,最喜歡在xxx地方干xxx
name = input('請輸入姓名:')
place = input('喜歡的地點:')
hobby = input('你的愛好:')
tem = '敬愛可親的%s,最喜歡在%s地方干%s'%(name, place ,hobby)
print(tem)

14. 等待用戶輸入內容,檢測用戶輸入內容中是否包含敏感字符?如果存在敏感字符提示“存在敏感字符請重新輸入”,並允許用戶重新輸入並打印。敏感字符:“小粉嫩”、“大鐵錘”
message = input('請輸入內容:')
while message == '小粉嫩' or message == '大鐵錘':
print('存在敏感字符請重新輸入')
message = input('請輸入內容:')
else :
print('1')

15. 單行注釋以及多行注釋?
單行注釋:“ ” 或 ' '
多行注釋:“”“ ”“” 或''' '''

16. 簡述你所知道的Python3和Python2的區別?
Python2:由龜叔團隊開發,源碼雜而亂,且有重復內容,違背了Python的宗旨。默認編碼方式是ASCII碼,讀取中文時會亂碼
Python3:由龜叔開發,遵循“優雅,明確,簡單”,默認編碼方式是utf-8,讀取中文時不會亂碼

17. 看代碼書寫結果:
a = 1>2 or 4<7 and 8 == 8
print(a)

結果:True

18. continue 和 break 的區別?
continue:結束本次循環,繼續下一次的循環;
break:直接跳出循環。

19. 看代碼書寫結果:
a = 12 and 127
print(a)

結果:127


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM