Python Learning (4) Data Structure-bool, pythonbool

Source: Internet
Author: User

Python Learning (4) Data Structure-bool, pythonbool
Python Boolean bool

Boolean values in python are represented by constants True and False. Note the case sensitivity.

The numeric int float comparison operator returns the bool type. The boolean type is usually used in the if and while statements.

 

True or False

The following values are determined to be False:

  • None
  • False
  • Zero of any numeric type, for example, 0, 0.0, 0j.
  • Any empty sequence, for example, '', (), [].
  • Any empty mapping, for example ,{}.
  • 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.

Except for the above expressions, all other expressions are determined to be True. This should be noted that it is quite different from other languages.

1 print (bool () 2 print (bool (False) 3 print (bool (0), bool (0.0), bool (0j )) 4 print (bool (""), bool (), bool ([]), bool ({}) 5 class alfalse (): 6 def _ bool _ (self): # defines the _ bool _ () method, always returns False 7 return False 8 f = alfalse () 9 print (bool (f) 10 class alzero (): 11 def _ len _ (self): # defines the _ len _ () method, always return 012 return 013 zero = alzero () 14 print (bool (zero) 15 class justaclass (): 16 pass17 c = justaclass () 18 print (bool (c )) # generally, True is returned for all class instances.

 

Boolean operator and or not

 

Operation Result
X or y IfXIs false, thenY, ElseX
X and y IfXIs false, thenX, ElseY
Not x IfXIs false, thenTrue, ElseFalse

 

Note that all values are in lower case: and or not; note that the priority of Boolean operations is lower than the expression. not a = B is equivalent to not (a = B). If a = not B, a syntax error occurs.

1 print (True and False), (False and False), (True and True) 2 print (True or False), (False or False ), (True or True) 3 print (not True), (not False) 4 print (1> 1 and 1 <1) # The expression is equal to print (1> 1) and (1 <1) 5 print (1> 1 and 1) <1) # brackets, 6 print (True and 1) # True and number. We do not recommend this. The returned number is 7 print (True and 111) 8 print (False and 2) # False and number, return False 9 print (not 1 = 1) 10 T = True11 F = False12 # print (T = not F) #13 print (T = (not F) is reported. # brackets are required.

 

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.