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) co ...
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')
課外知識:(不需要記)
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))
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,如果遇到 value1 = 0 or 1 value2 = 8 or 10 value3 = 0 or 9 or 8 print(value) # 第一個值如果是轉換成布爾值如果是真,則value=第一值; # 第一個值如果是轉換成布爾值如果是假,則value=第二值; # 如果有多個or條件,則從左到右依次進行上述流程。 # 對於and,如果遇到 # 第一個值如果是轉換成布爾值如果是True,則value=第二值; # 第一個值如果是轉換成布爾值如果是False,則value=第一值; # 如果有多個and條件,則從左到右依次進行上述流程。 value1 = 1 and 9 value2 = 1 and 0 value3 = 0 and 7 value4 = 0 and "" value5 = 1 and 0 and 9 print(value) #綜合 #先看and在看or;如果有括弧先算括弧內容。優先順序關係()>not>and>or,同一優先順序從左往右計算。 value1 = 1 and 9 or 0 and 8 print(value)
4、編碼
ascii:
unicode:
ecs2
ecs4
utf-8:
utf-16: