1、while回圈

使用while列印1.2.3.4.5.6.8.9.10 #快速注釋Ctrl+?
count = 1 while count <= 10: if count == 7: count = count + 1 pass #表示過,不執行下面程式 else: print(count) count = count + 1 print('end')
count = 1 while count <= 10: if count != 7: #!=表示不等于 print(count) count = count + 1 print('end')
錯誤示例 count = 1 while count <= 10 and count != 7: print(count) count = count + 1 print('end')
關鍵字:break終止當前回圈
continue #結束本次回圈,直接開始下個回圈
while True:#True首字需大寫,否則報錯name 'true' is not defined(名稱“true”沒有定義) print(666) break #終止當前回圈 print('end')
通過break實作1~10 while True: print(count) if count==10: break count = count+1 print('end')
while True: print('你好') while True: print(666) break
關鍵字:continue本次回圈如果遇到continue,則不再繼續下面程式,而是回到while條件位置
實作1234568910 count = 1 while count <= 10: if count == 7: count = count + 1 continue else: print(count) count = count + 1 print('end')
課外知識:else在while中的應用(不需要記)
count = 1 while count <= 10: print(count) count = count + 1 continue else:#當不再滿足while后的條件時,觸發,或 條件=False print('else代碼塊') print('end')
count = 1 while True: print(count) if count == 10: break count = count + 1 else:#當不再滿足while后的條件時,觸發,或 條件=False print('else代碼塊') print('end')
2、字串格式化
%s:代指字串
%d:代指數字
#字串格式化存在的意義 name = input('姓名') do = input('在干什么:') template = "我是%s ,%s,"%(name,do,) print(template)
template = "我是%s ,年齡%d,職業%s,"%('alex',25,'engineer',) print(template)
特殊:當需要列印%時,要多加一個百分號(%%)
name = 'alex' template = '%s手機的電量是100%%'%(name) print(template)
name = input('請輸入姓名:') age = input('請輸入年齡:') job = input('請輸入職業:') hobby = input('請輸入愛好:') msg = ''' ---------info of alex li-------- name: %s age: %s job: %s hobby: %s ---------end-------''' print(msg%(name,age,job,hobby))
2-1:.format
name = '我叫{0},年齡{1}'.format('xxx',99) print(name)
3、運算子
輸出1-100內的所有偶數
n=1 while n<101: temp= n % 2#取模 - 回傳除法的余數 if temp==0: print(n) else: pass n=n+1
求1-2+3-4+5...99的所有數的和 n=0 a=0 #a是之前所有數的總和 while n<100: print(n) temp=n%2 if temp==0: a=a-n else: a=a+n n=n+1 print(a)
Python算術運算子
以下假設變數a為10,變數b為21:
| 運算子 | 描述 | 實體 |
|---|---|---|
| + | 加 - 兩個物件相加 | a + b 輸出結果 31 |
| - | 減 - 得到負數或是一個數減去另一個數 | a - b 輸出結果 -11 |
| * | 乘 - 兩個數相乘或是回傳一個被重復若干次的字串 | a * b 輸出結果 210 |
| / | 除 - x 除以 y | b / a 輸出結果 2.1 |
| % | 取模 - 回傳除法的余數 | b % a 輸出結果 1 |
| ** | 冪 - 回傳x的y次冪 | a**b 為10的21次方 |
| // | 取整除 - 向下取接近除數的整數 |
>>> 9//2 4 >>> -9//2 -5 |
Python賦值運算子
以下假設變數a為10,變數b為20:
| 運算子 | 描述 | 實體 |
|---|---|---|
| = | 簡單的賦值運算子 | c = a + b 將 a + b 的運算結果賦值為 c |
| += | 加法賦值運算子 | c += a 等效于 c = c + a |
| -= | 減法賦值運算子 | c -= a 等效于 c = c - a |
| *= | 乘法賦值運算子 | c *= a 等效于 c = c * a |
| /= | 除法賦值運算子 | c /= a 等效于 c = c / a |
| %= | 取模賦值運算子 | c %= a 等效于 c = c % a |
| **= | 冪賦值運算子 | c **= a 等效于 c = c ** a |
| //= | 取整除賦值運算子 | c //= a 等效于 c = c // a |
| := | 海象運算子,可在運算式內部為變數賦值,Python3.8 版本新增運算子, |
在這個示例中,賦值運算式可以避免呼叫 len() 兩次: if (n := len(a)) > 10:
print(f"List is too long ({n} elements, expected <= 10)")
|
Python邏輯運算子
Python語言支持邏輯運算子,以下假設變數 a 為 10, b為 20:
| 運算子 | 邏輯運算式 | 描述 | 實體 |
|---|---|---|---|
| and | x and y | 布爾"與" - 如果 x 為 False,x and y 回傳 False,否則它回傳 y 的計算值, | (a and b) 回傳 20, |
| or | x or y | 布爾"或" - 如果 x 是 True,它回傳 x 的值,否則它回傳 y 的計算值, | (a or b) 回傳 10, |
| not | not x | 布爾"非" - 如果 x 為 True,回傳 False ,如果 x 為 False,它回傳 True, | not(a and b) 回傳 False |
# 對于or,如果遇到
# 第一個值如果是轉換成布林值如果是真,則value=https://www.cnblogs.com/ZBHH/p/第一值;
# 第一個值如果是轉換成布林值如果是假,則value=https://www.cnblogs.com/ZBHH/p/第二值;
# 如果有多個or條件,則從左到右依次進行上述流程,
value1 = 0 or 1
value2 = 8 or 10
value3 = 0 or 9 or 8
print(value)
#答案:1、8、9 # 對于and,如果遇到 # 第一個值如果是轉換成布林值如果是True,則value=https://www.cnblogs.com/ZBHH/p/第二值; # 第一個值如果是轉換成布林值如果是False,則value=https://www.cnblogs.com/ZBHH/p/第一值; # 如果有多個and條件,則從左到右依次進行上述流程, value1 = 1 and 9 value2 = 1 and 0 value3 = 0 and 7 value4 = 0 and "" value5 = 1 and 0 and 9 print(value)
#答案:9、0、0、0、0、0
#綜合 #先看and在看or;如果有括號先算括號內容,優先級關系()>not>and>or,同一優先級從左往右計算,
value1 = 1 and 9 or 0 and 8
print(value)
#答案:9
補充
運算子補充
in
value = https://www.cnblogs.com/ZBHH/p/"我是中國人" #判斷‘中國’是否在value所代指的字串中,‘中國’是否是value所代指的字串的子序列 v1 = '中國' in value #示例 content = input('請輸入內容:') if '退錢' in content: print('包含敏感字符:') ##示例 while True: content = input('請輸入內容:') if '退錢' in content: print('包含敏感字符:') else: print(content) break
not in
not 2 > 1 not 2 > 1 #錯誤 not 2 > 1 #正確
4、編碼
ascii:
unicode:
ecs2
ecs4
utf-8:
utf-16:
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/181251.html
標籤:Python
上一篇:如何使用python將大量資料匯出到Excel中的小技巧之一
下一篇:2.pandas入門介紹
