python第二天課程: %s, while else, 初始編碼, 邏輯運算
格式化輸出:
佔位符:%
字符類型:s
數字類型: d
在格式輸出時要用百分號:%%
#格式化輸出 #% s d """ name = input('請輸入你姓名') age = input('請輸入年齡') height = input('請輸入身高') msg = '我叫%s, 今年%s 身高%s'%(name,age,height) print(msg) name = input('請輸入你的名字') age = input('請輸入你的年齡') job = input('請輸入你的職業') hobbie = input('請輸入你的愛好') msg = '''------------ info of %s ----------- Name : %s Age : %d job : %s Hobbie: %s ------------- end -----------------''' % (name, name, int(age), job, hobbie) print(msg) name = input('請輸入你姓名') age = input('請輸入年齡') height = input('請輸入身高') msg = '我叫%s, 今年%s 身高%s 學習進度 3%%'%(name,age,height) print(msg) """
編碼:
ascii只能顯示英文,特殊字符,數字
萬國碼:unicode 最開始16位,發現中文不夠變成32位(4個字節)
占用資源
升級:utf-8 utf-16 utf-32
utf-8:最少用一個字節,8位表示一個英文
歐洲16位,2個字節
亞洲24位, 3個字節
gbk:中國國產,只能用於中文和ascii碼的文字
While else:
如果有break不執行else
count = 0 while count <= 5: count += 1 if count == 3: break print("Loop" , count) else: print('循環正常執行完啦') print("----- out of while loop -----")
邏輯運算:
and or not 優先級 :not > and > or
非零轉化成bool是True 零轉化成bool是False
X or Y X是True 則返回X X是False則取Y
XandY 與 XorY 相反
HW:
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
3).1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8 and 4 > 6 or 3 < 2
False
2.求出下列邏輯語氣的值
1).8 or 3 and 4 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).5 and 9 or 10 and 2 or 3 and 5 or 4 or 5
9
3.下列結果是什麽?
1).6 or 2 > 1
6
2).3 or 2 > 1
3
3).0 or 5 > 4
True
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.簡述變量命名規範
1).是由字母下劃綫任意組合而成,數字不能在前
2).不能和python的關鍵字相同
3).不能使用中文和拼音
4).常量用大寫來區分
5.name = input( ">>>") name 變量是什麽數據類型?
name 變量是str(字符串)
6.if 條件的基本結構?
if 條件:
結果
7.while循環語句基本結構?
設定的初始值
while 條件:
結果
8.(1).寫代碼:計算1-2+3...+99中除了88意外所有數的縂和?
''' count = 1 sum = 0 while count < 100: if count == 88: count = count +1 continue elif count % 2 == 0: sum = sum - count else: sum = sum + count count = count + 1 print(sum) '''
(2).計算1-2+3...-99中除了88意外所有數的縂和?
''' i = 0 j = -1 sum = 0 while i < 99: i = i + 1 if i == 88: continue else: j = -j sum = sum + i * j print(sum) '''
9.用戶登錄(三次輸錯機會)且每次輸錯誤時顯示剩餘錯誤次數(提示:使用字符串格式化)
''' i = 3 while i > 0: username = input('請輸入你的名字') password = int(input('請輸入你的密碼')) if username =='鹹魚哥' and password == 123: print('登錄成功') break else: i = i - 1 msg = "登錄錯誤還有%s次機會"%(i) print(msg) '''
10.簡述 ascii, unicode, utf-8編碼關係
第一代是ascii經過升級成unicode再升級成utf-8
11.簡述位和字節的關係
簡述位是 0 1 (兩種)
字節 是由8個連續的字節組成的
13.製作趣味模板程序需求:等待用戶輸入名字,地點,愛好,根據
用戶的名字和愛好進行任意現實 如:敬愛可親的XXX, 最喜歡在XXX地方乾XXX
''' name = input('請輸入名字') place = input('請輸入地方') hobbie = input('請輸入愛好') msg = """敬愛可親的%s, 最喜歡在%s地方乾%s"""%(name,place,hobbie) print(msg) '''
14.等待用戶輸入内容,檢測用戶輸入内容中是否包含敏感字符?如果存在
敏感字符提示"存在敏感字符請重新輸入",并且允許用戶重新輸入并打印。
敏感字符:"小粉嫩","大鐵錘"
''' name = input('請輸入名字') place = input('請輸入地方') hobbie = input('請輸入愛好') msg = """敬愛可親的%s, 最喜歡在%s地方乾%s"""%(name,place,hobbie) while True: if name == '小粉嫩' or name == '大鐵錘' or place == '小粉嫩' or place == '大鐵錘' or hobbie == '小粉嫩' or hobbie == '大鐵錘': print('存在敏感字符請重新輸入') break else: print(msg) '''
15.單行注釋以及多行注釋?
單行注釋 '注釋内容' "注釋内容"
多行注釋 '''注釋内容(長段)''' """住宿内容(長段)"""
16.簡述你所知道的python 3和python 2的區別?
python 3是python2的升級版 有了統一的標準,去除了重複的代碼
Python 2 代碼不標準,重複代碼太多
17.看代碼書寫結果
a = 1 > 2 or 4 < 7 and 8 ==8
執行不了沒有標識a
18.continue 和 break的區別
continue:有else會執行,結束這次結果重新回到loop
break:有else不會執行,結束整個loop