Python學(xué)習(xí)02——分支語句和循環(huán)語句

[TOC]

1. 對象的布爾值

python中萬物皆為對象,每個對象都有一個布爾值,通過bool()函數(shù)獲取,其中,以下對象的布爾值為False。

print(bool(False))  # 布爾型
print(bool(0))  # 數(shù)值為0
print(bool(0.0))  # 數(shù)值為0.0
print(bool(''))  # 空字符串
print(bool(""))  # 空字符串
print(bool([]))  # 空列表
print(bool(list()))  # 空列表
print(bool(None))  # None型
print(bool(()))  # 空元組
print(bool(tuple()))  # 空元組
print(bool({}))  # 空字典
print(bool(dict()))  # 空字典
print(bool(set()))  # 空集合

其余的對象,bool()函數(shù)返回的全部為True。

2. 分支語句

2.1 多分支

score = 80

if score > 80:
    print("優(yōu)秀")
elif score > 60:
    print("合格")
else:
    print("不合格")

2.2 嵌套if

score = 80
number = 5

if number % 2 == 0:
    if score > 90:
        print("優(yōu)秀")
    else:
        print("不優(yōu)秀")
else:
    if score > 80:
        print("優(yōu)秀")
    else:
        print("繼續(xù)努力")

2.3 分支表達式

分支表達式的語法為:

x if 判斷條件 else y

其中,如果判斷條件為True,則處理x,否則處理y。

x = 99
print("我是偶數(shù)" if x % 2 == 0 else "我是奇數(shù)")  #我是奇數(shù)

pass語句

什么都不做,占位符。

score = 80
number = 6

if number % 2 == 0:
    pass
else:
    if score > 80:
        print("優(yōu)秀")
    else:
        print("繼續(xù)努力")
        
# 什么都不輸出

3. 循環(huán)語句

3.1 range語句

創(chuàng)建一個順序的列表。

nums = range(10)
print(nums)  # range(0, 10)  打印的是range的對象名稱

print(list(nums))  # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 查看range中的整數(shù)
3.1.1 range(end)
r1 = range(10)  # 從0開始,步長為1,到10結(jié)束,不包括10
print(list(r1))  # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
3.1.2 range(start, end)
r2 = range(1, 10)   # 從1開始,步長為1,到10結(jié)束,不包括10
print(list(r2))  # [1, 2, 3, 4, 5, 6, 7, 8, 9]
3.1.3 range(start, end, step)
r3 = range(1, 10, 2)  # 從1開始,步長為2,到10結(jié)束,不包括10
print(list(r3))  # [1, 3, 5, 7, 9]

3.2 while循環(huán)

# 求1~100之間的偶數(shù)和
sum = 0
index = 0
while index <= 100:
    if index % 2 == 0:
        sum += index
    index += 1
print(sum)  # 2550

3.3 forIn循環(huán)

# 求1~100之間的偶數(shù)和
sum = 0
index = 0
for x in range(1, 101):
    if x % 2 == 0:
        sum += x
print(sum)  # 2550

3.4 循環(huán)控制語句

3.4.1 break

遇見break直接結(jié)束循環(huán)。

total = 10
index = 0
while index < total:
    sum = index + total
    if sum == 15:
        break
    else:
        print("index: ", index)
    index += 1
print("final: ", index)

輸出:

index:  0
index:  1
index:  2
index:  3
index:  4
final:  5
3.4.2 continue

跳過這次循環(huán),執(zhí)行下一次循環(huán)條件。

total = 10
index = 0
while index < total:
    index += 1
    if index == 5:
        continue
    else:
        print("index: ", index)
print("final: ", index)

輸出:

index:  1
index:  2
index:  3
index:  4
index:  6
index:  7
index:  8
index:  9
index:  10
final:  10

3.5 else語句

3.5.1 else搭配if

不滿足if條件時執(zhí)行:

a = 10
if a % 2 == 0:
    print("我是偶數(shù)")  # 打印 我是偶數(shù)
else:
    print("我是奇數(shù)")
3.5.2 else搭配forInwhile循環(huán)

沒有遇到break語句時執(zhí)行。

遇到break語句的情況:

total = 10
index = 0
while index < total:
    index += 1
    if index == 5:
        break
    else:
        print("index: ", index)
else:
    print("else: ", index)

輸出:

index:  1
index:  2
index:  3
index:  4

沒有遇到break語句的情況:

total = 10
index = 0
while index < total:
    index += 1
    if index == 5:
        pass
    else:
        print("index: ", index)
else:
    print("else: ", index)

輸出:

index:  1
index:  2
index:  3
index:  4
index:  6
index:  7
index:  8
index:  9
index:  10
else:  10

3.6 嵌套循環(huán)

輸出 9*9 乘法表:

# 輸出9 * 9 乘法表
for i in range(1, 10):
    s = ""
    for j in range(1, 10):
        s += str(j) + " * " + str(i) + " = " + str(i * j) + "  "
        if i == j:
            print(s)

輸出:

1 * 1 = 1  
1 * 2 = 2  2 * 2 = 4  
1 * 3 = 3  2 * 3 = 6  3 * 3 = 9  
1 * 4 = 4  2 * 4 = 8  3 * 4 = 12  4 * 4 = 16  
1 * 5 = 5  2 * 5 = 10  3 * 5 = 15  4 * 5 = 20  5 * 5 = 25  
1 * 6 = 6  2 * 6 = 12  3 * 6 = 18  4 * 6 = 24  5 * 6 = 30  6 * 6 = 36  
1 * 7 = 7  2 * 7 = 14  3 * 7 = 21  4 * 7 = 28  5 * 7 = 35  6 * 7 = 42  7 * 7 = 49  
1 * 8 = 8  2 * 8 = 16  3 * 8 = 24  4 * 8 = 32  5 * 8 = 40  6 * 8 = 48  7 * 8 = 56  8 * 8 = 64  
1 * 9 = 9  2 * 9 = 18  3 * 9 = 27  4 * 9 = 36  5 * 9 = 45  6 * 9 = 54  7 * 9 = 63  8 * 9 = 72  9 * 9 = 81  

嵌套循環(huán)中遇到break或者continue只作用于本層循環(huán)。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容