True in Python python, false condition judgment

Source: Internet
Author: User

Programmers with programming experience know the wording of conditional statements:

Take C + + as an example:

if (condition) {    dosomething ();}
The syntax for conditional judgments in Python 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

>>> condition= ' >>> print ' True ' if condition Else ' False ' false>>> condition= ' test ' >> > print ' true ' if condition Else ' False ' true
2. Testing for the original group

>>> condition= () >>> print ' True ' if condition Else ' False ' false>>> condition= ($) >> > print ' true ' if condition Else ' False ' true
3. Testing for the list

>>> condition=[]>>> print ' True ' if condition Else ' False ' false>>> condition=[' A ', ' B ']> >> print ' true ' if condition Else ' False ' true
4. Testing for Dictionaries

>>> condition={}>>> print ' True ' if condition Else ' False ' false>>> condition={' k ': ' V '}> >> print ' true ' if condition Else ' False ' true
5. Test for None

>>> condition=none>>> print ' True ' if condition Else ' false ' false
6. Test for Set ()

>>> condition=set () >>> print ' True ' if condition Else ' False ' false>>> condition.add (' a ') >>> print ' true ' if condition Else ' False ' true


True in Python python, false condition judgment

Related Article

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.