目錄
一、條件語句 if
if-else
if - elif - else
assert
二、迴圈語句
while
while-else
forfor-else
range()
enumerate()函式
break 語句
continue()
pass
推導式一、條件語句
1、if
if expression:
expr_true_suite
2、if -else
二、迴圈語句if expression:
expr_true_suite
else:
expr_false_suite
1、while迴圈
while 迴圈的**塊會一直迴圈執行,直到布林表示式的值為布林假。while 布林表示式:
**塊
while 布林表示式可以帶有<、>、==、!=、in、not in 德運算子
while布林表示式可以是數值之類的條件
while 後寫入乙個非零整數時,視為真值,執行迴圈體;
while後寫入0視為假值,不執行迴圈體;
while 後寫入str、list或任何序列,長度為0則視為真,否則視為假;
試著實現乙個程式,猜一下輸入的數字,要求最多只能猜3次,可以提醒猜大還是猜小了;
count = 0
while count < 3:
temp = input("不妨猜一下小哥哥現在心裡想的是那個數字:")
guess = int(temp)
if guess > 8:
print("大了,大了")
else:
if guess == 8:
print("你是小哥哥心裡的蛔蟲嗎?")
print("哼,猜對也沒有獎勵!")
count = 3
else:
print("小了,小了")
count = count + 1
print("遊戲結束,不玩兒啦!")
2、while-elsestring = 'abcd'
while string:
print(string)
string = string[1:]
當while 布林表示式:
**塊else:
**塊
while
迴圈正常執行完的情況下,執行
else
輸出,如果
while
迴圈中執行了跳出迴圈的語句,比如
break ,
將不執行
else
**塊的內容。
count = 0
while count < 5:
print("%d is less than 5" % count)
count = count + 1
else:
print("%d is not less than 5" % count)
3、for 迴圈count = 0
while count < 5:
print("%d is less than 5" % count)
count = 6
break
else:
print("%d is not less than 5" % count)
6、enumerate()for 迭代變數 in 可迭代物件:
**塊
enumerate(sequence,[start=0])
sequence-- 乙個序列、迭代器或其他支援迭代物件;
start --下標起始位置
返回enumerate物件;
seasons = ['spring', 'summer', 'fall', 'winter']
lst = list(enumerate(seasons))
print(lst)
[(0, 'spring'), (1, 'summer'), (2, 'fall'), (3, 'winter')]
lst = list(enumerate(seasons, start=1)) # 下標從 1 開始
lst[(1, 'spring'), (2, 'summer'), (3, 'fall'), (4, 'winter')]
7、breakfor i, language in enumerate(languages, 1):
print(i, 'i love', language)
print('done!')
1 i love python
2 i love r
3 i love matlab
4 i love c++
done!
跳出迴圈
終止本輪迴圈並開始下一輪迴圈import random
secret = random.randint(1, 10) #[1,10]之間的隨機數
while true:
temp = input("不妨猜一下小哥哥現在心裡想的是那個數字:")
guess = int(temp)
if guess > secret:
print("大了,大了")
else:
if guess == secret:
print("你這樣懂小哥哥的心思啊?")
print("哼,猜對也沒有獎勵!")
break
else:
print("小了,小了")
print("遊戲結束,不玩兒啦!")
8、pass 語句
9、列表推導式
1、編寫乙個python程式來查詢那些可以被7除以5的整數的數字,介於1500和2700之間。**列表推導式**
```python
[ expr for value in collection [if condition] ]
x = [-4, -2, 0, 2, 4]
y = [a * 2 for a in x]
x = [i ** 2 for i in range(1, 10)]
print(x)
x = [(i, i ** 2) for i in range(6)]
print(x)
x = [i for i in range(100) if (i % 2) != 0 and (i % 3) == 0]
print(x)
a = [(i, j) for i in range(0, 3) for j in range(0, 3)]
print(a)
a = [(i, j) for i in range(0, 3) if i < 1 for j in range(0, 3) if j > 1]
print(a)
元組推導式
a = (x for x in range(10))
print(a)
字典推導式
b =
print(b)
集合推導式
c =
print(c)
集合去重 #
d = 'i for i in "i love lsgogroup"'
print(d)
原文輸出:i for i in "i love lsgogroup"
e = (i for i in range(10))
print(e)
at 0x000001312ce99780>
通過next(e)
for each in e:
print(each, end=' ')
1 2 3 4 5 6 7 8 9
s = sum([i for i in range(101)])
print(s) # 5050
s = sum((i for i in range(101)))
print(s) # 5050
for num in range(1500,2700):
if num%35==0: 被7和5同時整除,就是可以被35整除;
print(num)
day2 條件迴圈語句
這次練習在鞏固了if else條件語句,學習了assert斷言 對於迴圈語句內容較多,鞏固了while迴圈 for迴圈 break continue pass等,對range enumerate 推導有了更深一步的掌握。if 語句 if 語句的 塊只有當條件結果為真時才執行,否則將繼續執行緊跟在該 ...
Day 2 條件與迴圈
1 語句 if elif else 2 自上而下,滿足條件時執行對應的塊內語句,後續的elif和else都不再執行 3 語句結束有 4 使用 input 輸入,返回的資料型別為 str 必要時需轉換資料型別 if 條件判斷1 執行1 elif 條件判斷2 執行2 elif 條件判斷3 執行3 els...
DAY2 條件與迴圈
一.條件 關鍵字 if elif else python中用elif代替了else if 注意 1 每個條件後面要使用冒號 表示接下來是滿足條件後要執行的語句塊。2 使用縮排來劃分語句塊,相同縮排數的語句在一起組成乙個語句塊。3 在python中沒有switch case語句。二,迴圈 while迴...