The examples in this paper describe the usage of true,false conditional judgment in Python. Share to everyone for your reference. The specific analysis is as follows:
Programmers with programming experience know the wording of conditional statements:
Take C + + as an example:
The code is as follows:
if (condition)
{
DoSomething ();
}
The syntax for conditional judgments in Python is as follows:
The code is as follows:
if (condition):
DoSomething ()
So when is the condition in the conditional statement true and false?
In high-level languages such as C++/java, the condition is false if the value of the condition is 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
The code is 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
The code is as follows:
>>> condition= ()
>>> print ' True ' if condition Else ' False '
False
>>> (condition=)
>>> print ' True ' if condition Else ' False '
True
3. Testing for 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. Testing for Dictionaries
The code is as follows:
>>> condition={}
>>> print ' True ' if condition Else ' False '
False
>>> condition={' k ': ' V '}
>>> print ' True ' if condition Else ' False '
True
5. Test for None
The code is as follows:
>>> Condition=none
>>> print ' True ' if condition Else ' False '
False
6. Test for 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
Hopefully this article will help you with Python programming.