1. Condition test
The core of each if statement is an expression that evaluates to True or FALSE, which is called a conditional test . Python determines whether the code in the IF statement is executed based on the condition test's value is true or false. The condition test is true, and execution is not performed.
1.1. Check if a specific value is included in the list
1Banned_users = ['Andrew','Carolina','David']2user ='Marie'3 4 ifUserinchbanned_users:5 Print(User.title () +", you can post a response if you wish.")6 ifUser not inchbanned_users:7 Print(User.title () +"doesn ' t exist in list.")
1.2. Boolean expressions
Boolean expression The result is either true or false. Empty strings, lists, tuples, dictionaries, and so on are false.
1 bool ([]) # 2 bool ({}) # 3 bool ([]) # 4 bool () # 5 bool (0) # 6 7 bool (1) # value True 8 bool ( -1) #
1.3, If-elif-else
1 if< condition judgment 1>:2< Executive 1>3 elif< condition judgment 2>:4< Executive 2>5 elif< condition judgment 3>:6< Executive 3>7 Else:8< Executive 4>9 Ten #Example Oneage = Int (input ("Please input your age:")) A - ifAge < 4:#below 4 years old free -Price =0 the elifAge < 18:#4~17岁 5 Bucks . -Price = 5 - elifAge < 65:#18~64岁 10 bucks . -Price = 10 + Else:#half-price greater than or equal to 65 years old -Price = 5 + A Print("Your Admission cost is $"+ STR (price) +".")
1.4. Test multiple conditions
If-elif-else is powerful, but only suitable for situations where only one condition is met: After passing the test, Python skips the rest of the tests. If you sometimes need to care about all the detection conditions, you cannot use the IF-ELIF-ELSE structure, but you should use multiple if statements.
1 #If a customer points two ingredients, make sure to include ' mushrooms ' and ' extra cheese ' in their pizza. 2Requested_toppings = ['Mushrooms','Extra Cheese']3 4 if 'Mushrooms' inchrequested_toppings:5 Print("Adding mushrooms.")6 if 'Pepperoni' inchrequested_toppings:7 Print("Adding Pepperoni.")8 if 'Extra Cheese' inchrequested_toppings:9 Print("Adding extra Cheese:")Ten One Print("\nfinished Making your pizza!")
2. Use the IF statement to process the list
2.1. Handling Special elements
1Requested_toppings = ['Mushrooms','Green Peppers','Extra Cheese']2 3 forRequested_toppinginchrequested_toppings:4 ifRequested_topping = ='Green Peppers':#If you order a green pepper, the print green pepper is gone. 5 Print("Sorry,we is out of green peppers right now.")6 Else:7 Print("Adding"+ requested_topping +".")8 9 Print("\nfinished Making your pizza!")
2.2. Make sure the list is not empty
1Requested_toppings = []2 3 ifRequested_toppings:#the loop body is executed for the real4 forRequested_toppinginchrequeted_toppings:5 Print("Adding"+ requested_topping +".")6Print("\nfinished Making your pizza!")7 Else:8 Print("You get a plain pizza!")
2.3. Use multiple lists
1Available_toppings = ['Mushrooms','Olives','Green Peppers','Pepperoni','Pineapple','Extra Cheese']2Requested_toppings = ['Mushrooms','French Fries','Extra Cheese']3 4 forRequested_toppinginchrequested_toppings:5 ifRequested_toppinginchavailable_toppings:6 Print("Adding"+ requested_topping +". ')7 Else:8 Print("Sorry, we don ' t have"+ requested_topping +".")9 Ten Print("\nfinished Making your pizza!")
05-python-if statements