python conditional judgment statement (if else)

basic structure

        The basic structure of a judgment statement in python consists of if and else. When the condition after if is true, the statement below if is executed; when the condition after if is false, the statement below else is executed. The true or false conditions here are all based on bool values. We know that the results returned by comparison operations, membership operations, and identity operations are all bool values. Therefore, comparison operations, membership operations, and identity operations can all be used as conditions in judgment statements.

if 1 > 2:
    print('yes')  # 代码缩进为一个Tab键或者4个空格键
else:
    print('no')

        When we only need to execute certain statements when the condition is true, we do not need to execute the statement when the condition is false. Then we can just use if and not else.

if 1 < 2:
    print('yes')

Nested structure

        The nested structure consists of multiple basic structures, and each level of if corresponds to each level of else (else is optional, depending on the specific requirements). Nested structures can be nested infinitely, but it should be noted that many nested structures can be optimized in other ways. When we write code, try not to write too many nested structures. The more readable the code, the worse it is.

        For example, there are three line segments a, b, and c. Please determine whether these three line segments can form a triangle. How to determine a triangle: The sum of any two sides is greater than the third side.

a = int(input('请输入线段a的长度:'))
b = int(input('请输入线段b的长度:'))
c = int(input('请输入线段c的长度:'))
if a + b > c:
    print('线段a + b大于c')
    if a + c > b:
        print('线段a + c大于b')
        if b + c > a:
            print('线段b + c大于a')
            print('三条线段a、b、c可以构成三角形')
        else:
            print('线段a大于等于b + c,不能构成三角形')
    else:
        print('线段b大于等于a + c,不能构成三角形')
else:
    print('线段c大于等于a + b,不能构成三角形')

The execution results are as follows:

We can input some values ​​arbitrarily to determine whether a triangle can be formed. Different inputs may result in different outputs. 

multi-condition structure

        The multi-conditional structure consists of if, elif and else. When the condition after if is true, the statement below if is executed; when the condition after if is false, the condition after the first elif is judged. When the condition after the first elif is true, the first elif is executed. The following statement; when the condition after the first elif is false, judge the condition after the second elif, when the condition after the second elif is true, execute the statement below the second elif; when the second elif When the condition after elif is false, the condition after the third elif is judged, and so on. If all conditions are false, execute the statement below else (else is optional, depending on the requirements).

        For example, let's grade the mathematics scores of primary school students. Those with a score of 90 or more are excellent, those with a score of 80 or more and less than 90 are good, those with a score of more than 60 but less than 80 are average, and those with 60 or more are passing. Scores less than 60 are considered a failure.

score = int(input('请输入分数:'))
if score > 100:
    print('超范围')
elif score >= 90:
    print('优秀')
elif score >= 80:
    print('良好')
elif score >= 60:
    print('一般')
elif score == 60:
    print('及格')
else:
    print('不及格')

The execution results are as follows:

Logical Operators

        Use logical operators to use complex judgment conditions in a judgment statement.

and Perform AND operation, the result is true when both left and right sides are true at the same time, otherwise the result is false
or Perform an OR operation, the result is false if both the left and right sides are false at the same time, otherwise the result is true
not Do not operation, the result is false when the value is true, and the result is true when the value is false.

Logical operator (and)

left right result
real real real
real Fake Fake
Fake real Fake
Fake Fake Fake

        The logical operator (and) is used to perform AND operations. When the values ​​​​on the left and right sides are both true, it returns true, otherwise it returns false.

print(1 < 2 and 5 > 4)  # True
print(1 == 2 and 5 > 4)  # False
print(1 == 2 and 5 == 4)  # False

         The application of logical operator (and) in judgment statements uses one judgment statement to determine whether a triangle can be formed.

a = int(input('请输入线段a的长度:'))
b = int(input('请输入线段b的长度:'))
c = int(input('请输入线段c的长度:'))
if a + b > c and a + c > b and b + c > a:
    print('三条线段a、b、c可以构成三角形')
else:
    print('三条线段a、b、c不能构成三角形')

The execution results are as follows:

Logical operator (or)

left right result
real real real
real Fake real
Fake real real
Fake Fake Fake

        The logical operator (or) is used to perform an OR operation. When the values ​​​​on the left and right sides are both false, it returns false, otherwise it returns true.

print(1 < 2 or 5 > 4)  # True
print(1 == 2 or 5 > 4)  # True
print(1 == 2 or 5 == 4)  # False

        The application of logical operator (or) in judgment statements determines whether the mathematics scores of primary school students exceed the range of 0~100.

score = int(input('请输入分数:'))
if score > 100 or score < 0:
    print('分数超出0~100的范围')

The execution results are as follows:

Logical operator (not)

value result
real Fake
Fake real

        The logical operator (not) is used to perform NOT operations, returning false when the value is true and true when the value is false.

print(not 1 < 2)  # False
print(not 1 == 2)  # True
print(not 1 > 2)  # True

        The application of logical operator (not) in judgment statements determines whether the mathematics scores of primary school students exceed the range of 0~100.

score = int(input('请输入分数:'))
if not 0 <= score <= 100:
    print('分数超出0~100的范围')

The execution results are as follows:

Integrated use

        There are three line segments a, b, and c. Please determine whether these three line segments can form a triangle. If a triangle can be formed, determine whether the triangle formed is equilateral, isosceles or an ordinary triangle.

a = int(input('请输入线段a的长度:'))
b = int(input('请输入线段b的长度:'))
c = int(input('请输入线段c的长度:'))
if a + b > c and a + c > b and b + c > a:
    if a == b == c:
        print('三条线段a、b、c能构成等边三角形')
    elif a == b or a == c or b == c:
        print('三条线段a、b、c能构成等腰三角形')
    else:
        print('三条线段a、b、c能构成普通三角形')
else:
    print('三条线段a、b、c不能构成三角形')

The execution results are as follows:

 

Guess you like

Origin blog.csdn.net/qq_40148262/article/details/131363359