5.2 Python conditional testing

5.2.3 Checking for inequality

To determine whether two values ​​are not equal, use the exclamation point in combination with the equal sign (!=) , where the exclamation point means no , as is the case in many programming languages. Let's use another if statement to demonstrate how to use the inequality operator. We'll store the requested pizza toppings in a variable, and then print a message indicating whether the toppings requested by the customer were anchovies:

requested_topping = 'mushrooms'

if requested_topping != 'anchovies':
    print("Hold the anchovies!")

The third line of code compares the value of requested_topping with 'anchovies', and if they are not equal, Python returns True, which in turn executes the code immediately following the if statement; if these two values ​​are equal, Python returns False, so The code immediately following the if statement is not executed. Since the value of requested topping is not 'anchovies', execute the print statement:

Hold the anchovies!

Most conditional expressions you write check whether two values ​​are equal, but sometimes it's more efficient to check whether two values ​​are not equal.

5.2.4 Comparing numbers

Checking values ​​is very simple, for example, the following code checks whether a person is 18 years old:

>>>age = 18
>>>age == 18
True

You can also check if two numbers are not equal, for example, the following code prints a message if the provided answer is incorrect:

answer = 17

if answer != 42:
    print("That is not the correct answer.Please try again!")

answer(17) is not 42, the condition on the third line is met, so the indented code block is executed:

That is not the correct answer.Please try again!

Conditional statements can contain various mathematical comparisons, such as less than, less than or equal to, greater than, greater than or equal to:

>>>age = 19
>>>age < 21
True
>>>age <= 21
True
>>>age > 21
False
>>>age >= 21
False

Various mathematical comparisons are available in if statements, which allow you to directly check the condition of interest.

5.2.5 Checking multiple conditions

You may want to check multiple conditions at the same time, for example, sometimes you need to perform the corresponding action when two conditions are true, and sometimes you only need to perform the corresponding action when one condition is true. In these cases, the keywords and and or can come to your rescue .

1. Use and to check multiple conditions

To check whether two conditions are true, use the keyword and to combine the two conditional tests; if each test passes, the entire expression is True; if at least one test fails, the entire expression The formula is False.

For example, to check whether two persons are at least 21 years old, use the following test:

>>>age_0 = 22
>>>age_1 = 18
>>>age_0 >= 21 and   age_1 >= 21
False
>>>age_1 = 22
>>> age_0 >= 21 and age_1 >= 21
True

In the first line, we define two variables for storing age: age_o and age_1. On the third line, we check to see if both variables are greater than or equal to 21; the test on the left passes, but the test on the right fails, so the result of the entire conditional expression is False. On the fifth line, we change age_1 to 22 so that the value of age_1 is greater than 21, so both tests pass, resulting in the result of the entire conditional expression being True. Each test can be enclosed in a separate pair of parentheses to improve readability, but it is not required. If you use parentheses the test will look like this:

(age_0 >= 21) and (age_1 >= 21)
  1. Use or to check multiple conditions

The or keyword also allows you to check multiple conditions, but at least one condition is met to pass the entire test. Expressions using or evaluate to False only if neither test passes.

The following checks the ages of two people again, but only if at least one of them is at least 21 years old:

>>>age_0 = 22
>>>age_1 = 18
>>>age_0 >= 21 or age_1 >= 21
True
>>>age_0 = 18
>>>age_0 >= 21 or age_1 >= 21
False

5.2.6 Checking whether a specific value is contained in a list

Sometimes it is necessary to check whether a list contains a specific value before performing an operation . For example, before ending the user's registration process, it may be necessary to check whether the username he provided is already included in the list of usernames. In a map application, it may be necessary to check whether the location submitted by the user is included in the list of known locations. ** To determine whether a specific value is already included in the list, use the keyword in. **Let's look at some code you might write for a pizzeria: This code first creates a list of the pizza toppings the user ordered, and then checks to see if the specific topping is included in that list.

>>>requested_toppings = ['mushrooms','onions','pineapple']
>>>'mushrooms' in requested_toppings
True
>>>'pepperoni' in requested_toppings
False

5.2.7 Checking if a specific value is not contained in a list

There are also times when it is important to be sure that a particular value is not included in the list; in such cases, the keyword not in can be used. For example, if you have a list of users who are banned from commenting on a forum, you can check to see if a user is banned before allowing a user to submit a comment:

banned_users = ['andrew','carolina','david']
user = 'marie'

if user not in banned_users:
    print(user.title()+",you can post a response if you wish.")

The third line of code is self-explanatory: if the value of users is not contained in the list banned_users, Python will return True, and then execute the indented line of code.

User 'marie' is not included in the list banned_users, so she will see a message inviting her to comment:

Marie,you can post a response if you wish.

Guess you like

Origin blog.csdn.net/Allen1862105/article/details/129202003
5.2
5.2