True and False conditions in Python determine instance analysis, and pythonfalse

Source: Internet
Author: User

True and False conditions in Python determine instance analysis, and pythonfalse

This article describes the True and False conditions in Python. 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:
Copy codeThe Code is as follows: if (condition)
{
DoSomething ();
}
The write rules for condition judgment statements in Python are as follows:
Copy codeThe 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
Copy codeThe 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
Copy codeThe 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
Copy codeThe 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
Copy codeThe 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
Copy codeThe Code is as follows: >>> condition = None
>>> Print 'true' if condition else 'false'
False
6. Test set ()
Copy codeThe 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.