Boolean operations and True and false values

Source: Internet
Author: User
In Python, any object can be judged by its true or false value: True,false

In the If or while condition judgment, the following case value is false:


1.None

2.Flase

3. The case of a value of 0, such as: 0,0.0,0j

4. All empty sequences, such as: ", (), []

5. All empty mapping, such as: {}

6.instances of user-defined classes, if the class defines a __bool__ () or __len__ () method,

When that method returns the integer zero or bool value False.


All other values is considered true-so objects of many types is always true.



Returns a Boolean result of 0 or flase for false in operation and built-in functions

1 or True indicates true

The Boolean operations in Python are as follows:

Print (' x or Y, if x is False,then y, else X ') x, y = 2, 0print (' {} or {} = {} '. format (x, y, x or y)) x1, y1 = 0, 10pri NT (' {} or {} = {} '. Format (x1, y1, x1 or y1)) x2, y2 = 0, 0print (' {} or {} = {} '. Format (x2, y2, x2 or y2))  print (' # ' * 5  0) print (' X and Y, if X is False,then x, else y ') print (' {} and {} = {} '. format (x, y, x and y)) x1, y1 = 0, 10print (' {} and {} = {} '. Format (x1, y1, x1 and y1)) x2, y2 = 0, 0print (' {} and {} = {} '. Format (x2, y2, X2 and y2))  print (' # ' * 50) Print (' Not X--if x is False,then true,else false ') x = 2print (' not {} = {} '. Format (x, not x)) x = 0print (' not {} = {} ') . format (x, not X))

Operation Result:

>>>

X or Y, if x is False,then y, else x

or 0 = 2

or 10 = 10

or 0 = 0

##################################################

X and y if x is False,then x, else y

and 0 = 0

and 10 = 0

and 0 = 0

##################################################

Not X-if x is False,then true,else false

Not 2 = False

Not 0 = True

>>>

  • 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.