current position:Home>Chapter 2: Fundamentals of python-5 Boolean

Chapter 2: Fundamentals of python-5 Boolean

2022-01-30 12:38:29 Small horn

Python Boolean

The goal is

In this article , You will understand Python Boolean data type 、 False and true values .

Python Introduction to Boolean data types

In programming , You often want to check whether the condition is true , And perform some operations according to the results .

To show true and false ,Python Boolean data type is provided . Boolean values have a technical name bool.

Boolean data types have two values :True and False.

Be careful , Boolean value True and False In capital letters (T) and (F) start .

The following example defines two Boolean variables :

is_active = True
is_admin = False
 Copy code 

When you compare two numbers ,Python The result will be returned as a Boolean value . for example :

>>> 20 > 10
True
>>> 20 < 10
False
 Copy code 

Again , Comparing two strings yields a Boolean value :

>>> 'a' < 'b'
True
>>> 'a' > 'b'
False
 Copy code 

bool() function

To determine a value is True still False, have access to bool() function . for example :

>>> bool("Hi")
True
>>> bool (' ')
False
>>> bool(100)
True
>>> bool(0)
False
 Copy code 

It is clear from the output that , Some values are calculated as True, Other values are calculated as False.

False and true values

When a value is calculated as True when , It's true . If a value equals False, It's fake .

Here are Python False values in :

  • Numbers 0(0)
  • An empty string ' '
  • False
  • None
  • An empty list []
  • An empty tuple ()
  • An empty dictionary {}

True values are other true values .

Please note that , In the following article , You will learn about None、 list 、 More information about tuples and dictionaries .

summary

  • Python Boolean data types have two values :True and False.
  • Use bool() Function to test a value is True still False.
  • The false value is calculated as False, The true value is calculated as True.
  • False values are numbers 0、 An empty string 、False、None、 An empty list 、 Empty tuples and empty dictionaries . True value is not false value .

copyright notice
author[Small horn],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201301238262042.html

Random recommended