This article mainly introduces the usage of True and False condition judgment in Python. the instance analyzes the condition judgment usage for different data types and has some reference value, for more information about how to use True and False conditions in Python, see the example in this article. Share it with you for your reference. The specific analysis is as follows:
Programmers with programming experience know how to write conditional statements:
Take C ++ as an example:
The code is as follows:
If (condition)
{
DoSomething ();
}
The write rules for condition judgment statements in Python are as follows:
The code is as follows:
If (condition ):
DoSomething ()
When is the condition in a condition statement true or false?
In C ++/Java and other advanced languages, if the condition value is 0 or the referenced object is a null pointer, the condition is False.
In Python, if the condition is '', (), [], {}, None, set (), the condition is Flase; otherwise, the condition is True.
The following is a test statement for Python:
1. string testing
The code is as follows:
>>> Condition =''
>>> Print 'true' if condition else 'false'
False
>>> Condition = 'test'
>>> Print 'true' if condition else 'false'
True
2. Tests for the original group
The code is as follows:
>>> Condition = ()
>>> Print 'true' if condition else 'false'
False
>>> Condition = (1, 2)
>>> Print 'true' if condition else 'false'
True
3. test the list
The code is as follows:
>>> Condition = []
>>> Print 'true' if condition else 'false'
False
>>> Condition = ['A', 'B']
>>> Print 'true' if condition else 'false'
True
4. test the dictionary
The code is as follows:
>>> Condition = {}
>>> Print 'true' if condition else 'false'
False
>>> Condition = {'K': 'V '}
>>> Print 'true' if condition else 'false'
True
5. None testing
The code is as follows:
>>> Condition = None
>>> Print 'true' if condition else 'false'
False
6. test set ()
The code is as follows:
>>> Condition = set ()
>>> Print 'true' if condition else 'false'
False
>>> Condition. add ('A ')
>>> Print 'true' if condition else 'false'
True
I hope this article will help you with Python programming.