The examples in this article describe the use of true,false conditional judgment in Python. Share to everyone for your reference. The specific analysis is as follows:
Programmers who have programming experience know how conditional statements are written:
Take C + + as an example:
Copy Code code as follows:
if (condition)
{
DoSomething ();
}
The conditional judgment statement in Python is written in the following way:
Copy Code code as follows:
if (condition):
DoSomething ()
So when is the condition in the conditional statement true when is false?
In advanced languages such as C++/java, this condition is false if the condition has a value of 0 or if the referenced object is a null pointer.
In Python, if condition is ", (), [],{},none,set (), then the condition is flase, otherwise true.
The following is a test statement for Python:
1. Testing for strings
Copy Code code as follows:
>>> condition= '
>>> print ' True ' if condition Else ' False '
False
>>> condition= ' Test '
>>> print ' True ' if condition Else ' False '
True
2. Testing for the original group
Copy Code code as follows:
>>> condition= ()
>>> print ' True ' if condition Else ' False '
False
>>> condition= (1,2)
>>> print ' True ' if condition Else ' False '
True
3. Test for the list
Copy Code code as follows:
>>> condition=[]
>>> print ' True ' if condition Else ' False '
False
>>> condition=[' A ', ' B ']
>>> print ' True ' if condition Else ' False '
True
4. Test for the dictionary
Copy Code code as follows:
>>> condition={}
>>> print ' True ' if condition Else ' False '
False
>>> condition={' k ': ' V '}
>>> print ' True ' if condition Else ' False '
True
5. Test for None
Copy Code code as follows:
>>> Condition=none
>>> print ' True ' if condition Else ' False '
False
6. Test for Set ()
Copy Code code 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 your Python programming.