Python小白學習之路—while、for循環、運算
Python裏面的循環跟其他語言裏的循環基本一致,只是書寫格式不同。
1.for循環: for [循環條件]:[循環語句] 滿足循環條件,則執行循環語句,執行一次判斷一次,不滿足則結束循環。
簡單的循環: in range() 表示在什麽範圍裏
1 # 循環 自動累計加一
2 # 一個參數代表從0開始到多少
3 for i in range(10):
4 print("One:",i)
5 # 兩個參數代表範圍
6 for i in range(0,5):
7 print("Two:",i)
8 # 三個參數代表範圍以及隔幾次循環執行語句一次
9 for i in range(0,10,2):#2表示隔幾個輸出一次
10 print("Thress",i)
D:\Python\python.exe D:/Pyproject/Day1/for.py
One: 0
One: 1
One: 2
One: 3
One: 4
One: 5
One: 6
One: 7
One: 8
One: 9
Two: 0
Two: 1
Two: 2
Two: 3
Two: 4
Thress 0
Thress 2
Thress 4
Thress 6
Thress 8
Process finished with exit code 0
2.while循語句: while [True/False]: 循環語句 條件為真則執行循環語句,為假則不進入循環。
1 names = "Sunshine"
2 # 如果條件返回值為真(True)則執行循環語句
3 while names == ‘Sunshine‘:
4 print("條件為真,名字是Sunshine。")
5 # 執行完語句後退出循環
6 break
7
8 while names == ‘Others‘:
9 print("條件為真,名字是Others。")
10 # 執行完語句後退出循環
11 break
D:\Python\python.exe D:/Pyproject/Day1/while.py 條件為真,名字是Sunshine。 Process finished with exit code 0
如果不加break的話,因為循環條件一直為真,則會一直循環,執行輸出語句。(一般Python有限制,好像最多只能執行998次)。
break的作用是終斷循環,也就是退出整個循環,要註意的時,如果有多個循環嵌套的話,break只能退出當前所在的循環。如果講話上面的break替換成continuou的話,還是會一直執行循環語句,continuou的作用是結束本次循環,開始下次循環,當前循環下continuou後面的語句不會執行。
Ex:學完循環語句和if條件語句後,可以用來寫一個猜年齡的小程序,要求如下:
(1)猜測年齡,猜的年齡大了,就提示bigger
(2)猜測年齡,猜的年齡小了,就提示smaller
(3)猜對了,輸出年齡,並祝賀
1 # 正確年齡
2 age_of_oidboy = 56
3 while True:
4 # 輸入猜測年齡
5 guess_age = int(input("guess age:"))
6 # 猜對了
7 if guess_age == age_of_oidboy:
8 print("Yes,you got it.")
9 break
10 # 猜大了
11 elif guess_age > age_of_oidboy:
12 print("Think smaller...")
13 # 猜小了
14 else:
15 print("Think bigger...")
View Code
結果:
D:\Python\python.exe D:/Pyproject/Day1/for.py
guess age:15
Think bigger...
guess age:57
Think smaller...
guess age:56
Yes,you got it.
Process finished with exit code 0
什麽是運算?
運算,數學上,運算是一種行為,通過已知量的可能的組合,獲得新的量。運算的本質是集合之間的映射
1.邏輯運算(返回值為布爾值類型)
假設:a,b,c = 3,5,7
1.and Ex: a > 2 and c < 7 -->flase
2.or Ex: a > 2 or c = 8 -->true
3.not Ex: if not a > 2 and c < 7:print("ddd") 輸出結果:ddd
1 # 邏輯運算 2 a,b,c = 3,5,7 3 # and運算 兩個條件都滿足才為真 4 print("and:",a > 2 and c < 7) 5 # or運算 滿足其中任意一個條件即為真 6 print("or:",a > 2 or c == 2) 7 # not運算 如果不是a<2 並且c>5,則不執行 8 if not a < 2 and c > 5: 9 print("執行成功")
結果:
D:\Python\python.exe D:/Pyproject/bogle.py and: False or: True 執行成功 Process finished with exit code 0
2.成員運算(返回值為布爾值類型)
(1)in 運算 判斷在不在裏面
現在有列表 a[1,2,3,4]
in: 1 in a 表示:1是列表a裏面的一員。結果:True
not: 5 not in a 表示:5不是列表a裏面的一員。 結果:False
1 # 創建一個列表 2 a = [1,2,3,4] 3 # in 運算 4 print("In運算,1是a的一員:",1 in a) 5 # not 運算 6 print("Not運算,5不是a的一員:",4 not in a)
結果:
D:\Python\python.exe D:/Pyproject/bogle.py
In運算,1是a的一員: True
Not運算,5不是a的一員: False
Process finished with exit code 0
(2)is 運算 判斷是不是什麽的一員
a[1,2,3,4]
names = ‘hello world!"
is type(a) is list 表示:a的類型是一個列表-->true
is not type(names) is not str 表示:names的類型不是一個字符串類型-->false
1 # 創建一個列表和字符串 2 a = [1,2,3,4] 3 names = "hello world!" 4 # is 運算 5 print("is運算,a的類型是一個列表:",type(a) is list) 6 # is not 運算 7 print("is運算,names不是字符串類型:",type(names) is not str)
結果:
D:\Python\python.exe D:/Pyproject/bogle.py is運算,a的類型是一個列表: True is運算,names不是字符串類型: False Process finished with exit code 0
3.位運算(轉換成二進制後的運算)
現在有a,b,c三個值分別為60,13,0
a = 60 (00111100),
b = 13 (00001101),
c = 0 (00000000)
進行如下運算:
(1)& 與運算 a & b =00001100-->12(00001010) (同為1,則為真1)
(2)| 或運算 a | b =00111101-->61(00111101)(有一個為1,則為1)
(3)^ 異運算 a ^ b =00110001-->49(0011001)(相同為0,不同為1)
(4)~ 取反運算 ~ a =1 11000011-->-195(最前面的為符號位,也會取反)
(5)<< 左移運算 64 << 2 =256 64(00100000)左移2位後:256(10000000)
(6)>> 右移運算 64 >> 2 =16 64(00100000)右移2位後:16(000001000)
1 a = 60 2 b = 13 3 c = 0 4 print("&運算:",a & b ) 5 print("|運算:",a | b) 6 print("^運算:",a ^ b) 7 print("~運算:",~a) 8 print(">>運算:",64 >> 2) 9 print("<<運算:",64 << 2)
結果:
D:\Python\python.exe D:/Pyproject/bogle.py &運算: 12 |運算: 61 ^運算: 49 ~運算: -61 >>運算: 16 <<運算: 256 Process finished with exit code 0
計算機中能表示的最小單位,是一個二進制位(bit)
計算機中能存儲的最小單位,是一個二進制位(bit)
8 bit(比特) = 1 byte(字節)
計算機i中的字符最少要用一字節來表示
1024byte = 1kbyte
1024kbyte = 1mbyte
1024mbyte = 1gbyte
1024gb = 1Tb
三元運算:
三元運算符是軟件編程中的一個固定格式,語法是“條件表達式?表達式1:表達式2”。使用這個算法可以使調用數據時逐級篩選。(百度百科參考)
1.語法:條件表達式?表達式1:表達式2
說明:問號前面的位置是判斷的條件,判斷結果為bool型,為true時調用表達式1,為false時調用表達式2。
其邏輯為:“如果為條件成立或者滿足則執行表達式1,否則執行第二個。”
2.也可以用if...else...
a,b,c=1,3,5
d = a if a>b else c (如果a>b則d=a,否則d=c) -->d=5
Python小白學習之路—while、for循環、運算