Catalog
1. Conditions for testing
1.1 Check for equality , Use ==
2.2 When checking for equality , Consider case
2.3 The check is not equal !=
2.4 Check multiple conditions and perhaps or
2.5 Check whether the list contains specific values , Keywords available in
2.6 Check that the list does not contain specific values , Keywords available not in
3.if sentence
3.1 if Sentence structure :
3.2 if-else structure :
3.3 if-elif-else structure :
3.4 The use of multiple elif Code block :
3.5else Code blocks can be omitted
3.6 Test multiple conditions
4. Use if Statement processing list
5. Set up if Sentence format
Equality returns True, Unequal return False
If you want equality , Variable values can be converted to lowercase and then compared , Here's the picture
2.4.1 Use and Check multiple conditions , If every test passes , The whole expression is True ; If at least one test fails , The whole expression is False. Examples are as follows :
2.4.1 Use or Check multiple conditions , But as long as at least one condition is satisfied , You can pass the whole test . Only if both tests fail , Use or The expression of is False. Examples are as follows :
if conditional_test: // If the condition is true, execute
do something
if conditional_test:
do something
else:
do something
if conditional_test:
do something
elif:
do something
else:
do something
And 3.3 Agreement , Don't repeat
Python There is no demand for if-elif There must be... Behind the structure else Code block . In some cases ,else Code blocks are useful ; And in other cases , Use one elif Statement to deal with specific situations more clearly
Use a series of independent if sentence . If you just want to execute a block of code , Just use if-elif-else structure
stay for You can add... To the statement if sentence , Before inspection , First judge whether the list is empty :
names = []
if names:
for names in names:
print("aaa")
else:
print("No name")
In such as == 、>= and <= Add a space on each side of the comparison operator
The learning content of this article comes from 《Python Programming : From introduction to practice 》