True and False conditions in Python determine instance analysis

Source: Internet
Author: User
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.

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.