?面向讀者:所有人
?所屬專欄:零基礎(chǔ)入門Pythonhttps://blog.csdn.net/arthas777/category_12455877.html
Python if語句
Python if語句的流程圖
Python if語句示例
Python If-Else Statement
Python if else語句的流程圖
使用Python if-else語句
列表理解中的Python if-else語句
Python中的嵌套If語句
Python嵌套if語句的流程圖
Python嵌套if語句示例
Python if elif else Ladder
Python if elif else梯形圖的流程圖
if-elif-else梯形圖
Python if elif else梯形圖示例
Short Hand if語句
Python if簡寫示例
Short Hand if else語句
Python if else簡寫示例
在現(xiàn)實生活中,當(dāng)我們需要做出一些決定時,我們會根據(jù)這些決定決定下一步應(yīng)該做什么。類似的情況也出現(xiàn)在編程中,我們需要做出一些決定,并根據(jù)這些決定執(zhí)行下一塊代碼。Python語言中的條件語句決定程序執(zhí)行流的方向(控制流)。
?
Python中的控制流類型
Python控制流語句如下:
if語句
if-else語句
嵌套的if語句
if-elif-else梯子
?
Python if語句
if語句是最簡單的決策語句。它用于決定是否執(zhí)行某個語句或語句塊。
語法:
if condition:
# Statements to execute if
# condition is true
這里,評估后的條件將是真或假。如果該語句接受布爾值&如果該值為true,則它將執(zhí)行下面的語句塊,否則不執(zhí)行。
正如我們所知,python使用縮進(jìn)來識別塊。因此,if語句下的塊將被識別,如下例所示:
if condition:
statement1
statement2
# Here if the condition is true, if block
# will consider only statement1 to be inside
# its block.
Python if語句的流程圖
Python if語句的流程圖
Python if語句示例
由于if語句中存在的條件為false。因此,執(zhí)行if語句下面的塊。
# python program to illustrate If statement
i = 10
if (i > 15):
????print("10 is less than 15")
print("I am Not in if")
Output:?
I am Not in if
Python If-Else Statement
單獨的if語句告訴我們,如果條件為真,它將執(zhí)行語句塊,如果條件是假,它將不會執(zhí)行。但是,如果條件為false,我們想做其他事情,那么當(dāng)if條件為false時,我們可以將else語句與if語句一起使用來執(zhí)行代碼塊。
Python的語法If Else:
if (condition):
# Executes this block if
# condition is true
else:
# Executes this block if
# condition is false
Python if else語句的流程圖
Python的流程圖是else語句
使用Python if-else語句
else語句后面的代碼塊在調(diào)用不在塊中的語句(沒有空格)后,如果if語句中的條件為false,則執(zhí)行該代碼塊。
Output:?
i is greater than 15 i'm in else Block i'm not in if and not in else Block
列表理解中的Python if-else語句
在這個例子中,我們在列表理解中使用if語句,條件是如果列表的元素是奇數(shù),則其數(shù)字和將被存儲,否則將不被存儲。
# python program to illustrate If else statement
#!/usr/bin/python
i = 20
if (i < 15):
????print("i is smaller than 15")
????print("i'm in if Block")
else:
????print("i is greater than 15")
????print("i'm in else Block")
print("i'm not in if and not in else Block")
# Explicit function
def digitSum(n):
????dsum = 0
????for ele in str(n):
????????dsum += int(ele)
????return dsum
# Initializing list
List = [367, 111, 562, 945, 6726, 873]
# Using the function on odd elements of the list
newList = [digitSum(i) for i in List if i & 1]
# Displaying new list
print(newList)
Output : [16, 3, 18, 18] |
Python中的嵌套If語句
嵌套的if是另一個if語句的目標(biāo)if語句。嵌套的if語句表示在另一個if語句中的if語句。是的,Python允許我們在if語句中嵌套if語句。即,我們可以將一個if語句放在另一個if聲明中。
Syntax:?
if (condition1):
# Executes when condition1 is true
if (condition2):
# Executes when condition2 is true
# if Block is end here
# if Block is end here
Python嵌套if語句的流程圖
Python嵌套if語句的流程圖
Python嵌套if語句示例
在這個例子中,我們在代碼中顯示嵌套的if條件,所有的if條件都將逐一執(zhí)行。
# python program to illustrate nested If statement
i = 10
if (i == 10):
???
????#? First if statement
????if (i < 15):
????????print("i is smaller than 15")
?????????
????# Nested - if statement
????# Will only be executed if statement above
????# it is true
????if (i < 12):
????????print("i is smaller than 12 too")
????else:
????????print("i is greater than 15")
?
Output:? i is smaller than 15 i is smaller than 12 too |
Python if elif else Ladder
在這里,用戶可以在多個選項中進(jìn)行決定。if語句是自上而下執(zhí)行的。一旦控制if的條件之一為true,則執(zhí)行與該if相關(guān)聯(lián)的語句,并繞過梯形圖的其余部分。如果所有條件都不為真,那么將執(zhí)行最后的else語句。
Syntax:?
if (condition):
statement
elif (condition):
statement
.
.
else:
statement
Python if elif else梯形圖的流程圖
if-elif-else梯形圖
Python if elif else梯形圖示例
在該示例中,我們顯示了單個if條件和多個elif條件,以及單個else條件。
# Python program to illustrate if-elif-else ladder
#!/usr/bin/python
i = 20
if (i == 10):
????print("i is 10")
elif (i == 15):
????print("i is 15")
elif (i == 20):
????print("i is 20")
else:
????print("i is not present")
?
Output:? i is 20 |
Short Hand if語句
只要if塊內(nèi)只有一條語句要執(zhí)行,就可以使用簡寫if。該語句可以與if語句放在同一行。
Syntax:?
if condition: statement
Python if簡寫示例
在給定的示例中,我們有一個條件,即如果數(shù)字小于15,則將執(zhí)行進(jìn)一步的代碼。
# Python program to illustrate short hand if
i = 10
if i < 15: print("i is less than 15")
?
Output: i is less than 15 |
Short Hand if else語句
這可以用于在單行中編寫if-else語句,其中if和else塊中都只需要一條語句。
Syntax:
statement_when_True if condition else statement_when_False
Python if else簡寫示例
?
在給定的例子中,如果數(shù)字是15,我們將打印True,否則將打印False。
?文章來源:http://www.zghlxwxcb.cn/news/detail-755284.html
# Python program to illustrate short hand if-else
i = 10
print(True) if i < 15 else print(False)
Output:?文章來源地址http://www.zghlxwxcb.cn/news/detail-755284.html
True
到了這里,關(guān)于【零基礎(chǔ)入門Python】Python If Else流程控制的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!