日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python中的If,Elif和Else语句

發布時間:2023/11/29 python 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python中的If,Elif和Else语句 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

如果Elif Else聲明 (If Elif Else Statements)

The if/elif/else structure is a common way to control the flow of a program, allowing you to execute specific blocks of code depending on the value of some data.

if / elif / else結構是控制程序流程的常用方法,它使您可以根據某些數據的值執行特定的代碼塊。

如果聲明 (if statement )

If the condition following the keyword if evaluates as true, the block of code will execute. Note that parentheses are not used before and after the condition check as in other languages.

如果關鍵字if之后的條件評估為true ,則代碼塊將執行。 請注意,在條件檢查之前和之后不會像其他語言一樣使用括號。

if True:print('If block will execute!')x = 5if x > 4:print("The condition was true!") #this statement executes

其他陳述 (else statement)

You can optionally add an else response that will execute if the condition is false:

您可以選擇添加一個else響應,如果條件為false該響應將執行:

if not True:print('If statement will execute!') else:print('Else statement will execute!')

Or you can also see this example:

或者您也可以看到以下示例:

y = 3if y > 4:print("I won't print!") #this statement does not execute else:print("The condition wasn't true!") #this statement executes

Note that there is no condition following the else keyword - it catches all situations where the condition was false

請注意, else關鍵字后沒有條件-它捕獲條件為false所有情況

elif聲明 (elif statement)

Multiple conditions can be checked by including one or more elif checks after your initial if statement. Just keep in mind that only one condition will execute:

可以通過在初始if語句之后包含一個或多個elif檢查來檢查多個條件。 請記住,只有一種情況會執行:

z = 7if z > 8:print("I won't print!") #this statement does not execute elif z > 5:print("I will!") #this statement will execute elif z > 6:print("I also won't print!") #this statement does not execute else:print("Neither will I!") #this statement does not execute

Note: only the first condition that evaluates as true will execute. Even though z > 6 is true, the if/elif/else block terminates after the first true condition. This means that an else will only execute if none of the conditions were true.

注意:只有第一個條件為true條件才會執行。 即使z > 6為true , if/elif/else塊if/elif/else在第一個true條件之后終止。 這意味著else僅在沒有條件true的情況下才會執行。

嵌套if語句 (Nested if statements)

We can also create nested if’s for decision making. Before preceding please refer to the href=’https://guide.freecodecamp.org/python/code-blocks-and-indentation’ target=’_blank’ rel=‘nofollow’>indentation guide once before preceding.

我們還可以創建嵌套的if決策。 在開始之前,請先參考href =“ https://guide.freecodecamp.org/python/code-blocks-and-indentation'target ='_ blank'rel ='nofollow'>縮進指南。

Let’s take an example of finding a number which is even and also greater than 10

讓我們以發現一個等于甚至大于10的數字為例

python x = 34 if x % 2 == 0: # this is how you create a comment and now, checking for even.if x > 10:print("This number is even and is greater than 10")else:print("This number is even, but not greater 10") else:print ("The number is not even. So point checking further.")

This was just a simple example for nested if’s. Please feel free to explore more online.

這只是嵌套if的一個簡單示例。 請隨時在線探索更多。

While the examples above are simple, you can create complex conditions using boolean comparisons and boolean operators.

盡管上面的示例很簡單,但是您可以使用布爾比較和布爾運算符創建復雜的條件。

內聯python if-else語句 (Inline python if-else statement)

We can also use if-else statements inline python functions. The following example should check if the number is greater or equal than 50, if yes return True:

我們還可以使用if-else語句內聯python函數。 以下示例應檢查該數字是否大于或等于50,如果是,則返回True:

python x = 89 is_greater = True if x >= 50 else Falseprint(is_greater)

Output

輸出量

> True >

有關if / elif / else語句的更多信息: (More info on if/elif/else statements:)

  • How to get out of if/else hell

    如何擺脫if / else地獄

  • If/else in JavaScript

    JavaScript中的if / else

翻譯自: https://www.freecodecamp.org/news/if-elif-else-statements/

總結

以上是生活随笔為你收集整理的Python中的If,Elif和Else语句的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。