python if statement, boolean operation

one:

    if statement:

    Function: Let the program selectively execute a certain statement or some statements according to the conditions

    grammar:

    if truth expression 1:

            Statement block 1...

    elif truth expression 2:

            Statement block 2....

        ............

    else:

            Statement block n.........

    illustrate:

        1. The truth expression judgment will be carried out from top to bottom. If one of the truth expressions is True, the statement block is executed, and then the execution of the if statement is ended. If all the truth expressions are False. The statement in the else clause is executed.

        2. The elif clause can have 0, 1, or more

        3. The else clause can have 0 or more

Nesting of if statements

The if statement itself is a compound statement composed of multiple statements

An if statement can be nested as a statement inside another statement

Notice:

  • 1. A colon (:) should be used after each condition, indicating that the next block of statements to be executed after the condition is met.

  • 2. Use indentation to divide statement blocks, and statements with the same indentation number together form a statement block.

  • 3. There is no switch-case statement in Python.


For example: input a positive integer, determine whether the number is a base number or an even number, and print the result.

n = int(input("Please enter a number: ")
if n % 2 == 0:
        print(n, "is even")
else:
        print(n, "is odd")
operation result:
[root@localhost data]# ./test.py   
Please enter a number: 4
4 is even
[root@localhost data]# ./test.py 
Please enter a number: 3
3 is odd
[root@localhost data]#

Two: Conditional expression:

    Syntax: expression1 if truth expression else expression2

    effect:

        If the boolean value of the truth expression is True, then execute expression 1 and return a reference to the resulting object, otherwise execute expression 2 and return a reference to the object.

    Such as:

#Shopping mall promotion, 20 off over 100

money = int(input("Please enter the item amount:"))
pay = money - 20 if money >= 100 else money
print("Need to pay:", pay, "yuan")

[root@localhost data]# ./test.py 
Please enter the item amount: 200
Need to pay: 180 yuan
[root@localhost data]# ./test.py 
Please enter the item amount: 100
Need to pay: 80 yuan
[root@localhost data]# ./test.py 
Please enter the item amount: 80
Need to pay: 80 yuan
[root@localhost data]#

Three: pass statement:

effect:

Often used to fill in grammatical blanks

num = int(input("Please enter a number 1-4:"))
if 1 <= num <= 4:
    pass
else:
    print("Incorrect input")
###########
[root@localhost data]# ./test.py 
Please enter a number 1-4:3
[root@localhost data]# ./test.py 
Please enter a number 1-4:7
wrong input
[root@localhost data]#

---------------------------------------------------------------------------------------------

Boolean operations:

operator:

not and   or

Boolean not operation: not

Syntax: not x

Function: perform boolean negation on x, if bool(x) is True, return False, otherwise return True


Boolean and operation: and

Syntax: x and y 

Note: x, y represent expressions.

Function: Returns the false value object first, when the boolean value of x is false, it returns x, otherwise it returns y.

Indication: True and True # Return True

  True and False #return False

  False and True #return False

  Fales and False #return False

x = int(input("Please enter a month:"))
if 1 <= x and x <= 12:
        print("valid month")
else:
        print("The month is not valid")
 ###
[root@localhost data]# ./test.py 
Please enter a month: 1
legal month
[root@localhost data]# ./test.py 
Please enter a month: 8
legal month
[root@localhost data]# ./test.py 
Please enter a month: 13
month is illegal

Boolean or operator: or

Syntax: x or y

Function: return the truth object first, when x is True, return x, otherwise return y

Indication:

True or True # True

True or False #True

Flase or True # True

Flase or False # False

x = int(input("Please enter a month:"))
if x < 1 or x > 12:
	print("Incorrect input")
##
[root@localhost data]# ./test.py 
Please enter a month: 13
wrong input
[root@localhost data]#


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324634656&siteId=291194637