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

歡迎訪問 生活随笔!

生活随笔

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

python

Python中的条件语句(if,if ... else,if ... elif ... else和嵌套的if)

發布時間:2025/3/11 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python中的条件语句(if,if ... else,if ... elif ... else和嵌套的if) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Conditional statements decide the flow of program execution. In programming whenever we need to make execute any special blocks based on the decision then we use the conditional statements.

條件語句決定程序執行的流程。 在編程中,只要我們需要根據決策執行任何特殊的塊,就使用條件語句 。

No need to be confused about it because we are using it in our daily life when we need to make some decisions and based on these decisions, we decide what should we have to do next. Similar situations can occur while programming also where we need to make some decisions based on conditions given and based on these decisions we will execute the block of code.

無需對此感到困惑,因為當我們需要做出一些決定時,我們就在日常生活中使用它,并根據這些決定來決定下一步該做什么。 在編程時也會發生類似情況,在這種情況下,我們需要根據給定的條件做出一些決策,并根據這些決策執行代碼塊。

Conditional statements available in the Python are,

Python中可用的條件語句是

  • if statements

    如果陳述

  • if...else statements

    如果...其他陳述

  • if...elif...else statements

    if ... elif ... else語句

  • Nested if statements

    嵌套if語句

  • 1)Python if語句 (1) Python if statement)

    It is one of the most common conditional statements in which some conditions are provided and if the condition is true then block under the if the condition will be executed.

    它是最常見的條件語句之一,其中提供了一些條件,如果條件為true,則在條件將被執行時阻塞。

    Syntax:

    句法:

    if condition:# what we want to execute here.

    Example:

    例:

    # input age age=int(input('what is your age: '))# checking the condition if age>=18:print('You are Grown-up now !')

    Output

    輸出量

    RUN 1: what is your age: 21 You are Grown-up now !RUN 2: what is your age: 15

    2)Python if ... else語句 (2) Python if...else statement )

    In the above, we have seen that if the condition is true then block under if will execute then one thing is, comes to our mind that what happens when the condition will be false. So, to overcome this problem we are using if...else statements.

    在上面的內容中,我們已經看到,如果條件為true,則在條件執行時阻塞,然后發生一件事,我們想到的是,條件為false時會發生什么。 因此,為了克服這個問題,我們使用了if ... else語句 。

    Syntax:

    句法:

    if condition:# what we want to execute here.else:# what we want to execute here.

    If the condition is true, then it will execute the block of if statements otherwise else statement.

    如果條件為true ,則它將執行if語句的塊,否則執行else語句。

    Example:

    例:

    # input age age=int(input('what is your age: '))# checking the condition if age>=18:print('You are Grown-up now !') else:print('You are Young!')

    Output

    輸出量

    RUN 1: what is your age: 21 You are Grown-up now !RUN 2: what is your age: 15 You are Young!

    3)Python if ... elif ... else語句 (3) Python if...elif...else statement)

    These conditional statements use where we have to check multiple conditions in the program. If these will not true that is false then the else blocks only execute.

    這些條件語句用于我們必須檢查程序中的多個條件的地方。 如果這些都不是 ,則返回true ,否則else塊僅執行。

    Syntax:

    句法:

    if condition:# what we want to execute here.elif conditions:# what we want to execute here.else:# what we want to execute here.

    Example:

    例:

    # input the age n=int(input('Enter marks: '))# checking the conditions if n>=90:print('Excellent') elif n<90 and n>=75:print('Passed') else:print('Fail')

    Output

    輸出量

    RUN 1: Enter marks: 95 ExcellentRUN 2: Enter marks: 80 PassedRUN 3: Enter marks: 63 Fail

    4)Python嵌套的if語句 (4) Python Nested if statement)

    As we all have familiar with the word nested which means one statement inside another statement same in the programming nested if statements mean an if statement inside another if statement that is we can place one if statements inside another if statements.

    眾所周知,嵌套一詞意味著在編程中相同的另一個語句中的一個語句,嵌套的if語句意味著另一個if語句內部的if語句,我們可以將一個if語句放在另一個if語句中。

    Syntax:

    句法:

    if condition:# what we want to execute here.if condition:# what we want to execute here.else:# what we want to execute here.

    Note: In Python, true and false are written as True and False. Since Python follow the indentation rule so the statements must write under the indentations of if statements or other.

    注意:在Python中, true和false分別寫為True和False 。 由于Python遵循縮進規則,因此語句必須在if語句或其他語句的縮進下編寫。

    Now, we will see an example based on the above conditional statements which will show us the grade of students.

    現在,我們將基于上述條件陳述看到一個示例,該示例將向我們顯示學生的成績。

    Example:

    例:

    # input the age n=int(input('Enter marks: '))# checking the conditions if n>=75:if n >=95:print('Excellent')else:print('Pass') else:print('Fail')

    Output

    輸出量

    RUN 1: Enter marks: 96 ExcellentRUN 2: Enter marks: 89 PassRUN 3: Enter marks: 69 Fail

    翻譯自: https://www.includehelp.com/python/conditional-statements-if-if-else-if-elif-else-and-nested-if.aspx

    總結

    以上是生活随笔為你收集整理的Python中的条件语句(if,if ... else,if ... elif ... else和嵌套的if)的全部內容,希望文章能夠幫你解決所遇到的問題。

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