boolean Category Page - PythonForBeginners.com https://www.pythonforbeginners.com Learn By Example Fri, 28 Aug 2020 15:40:53 +0000 en-US hourly 1 https://wordpress.org/?v=5.8.13 https://www.pythonforbeginners.com/wp-content/uploads/2020/05/cropped-pfb_icon-32x32.png boolean Category Page - PythonForBeginners.com https://www.pythonforbeginners.com 32 32 201782279 Booleans, True or False in Python https://www.pythonforbeginners.com/basics/boolean https://www.pythonforbeginners.com/basics/boolean#comments Fri, 21 Sep 2012 02:49:27 +0000 https://www.pythonforbeginners.com/?p=540 What are Boolean? Boolean values are the two constant objects False and True. They are used to represent truth values (other values can also be considered false or true). In numeric contexts (for example, when used as the argument to an arithmetic operator), they behave like the integers 0 and 1, respectively. The built-in function […]

The post Booleans, True or False in Python appeared first on PythonForBeginners.com.

]]>
What are Boolean?

Boolean values are the two constant objects False and True.

They are used to represent truth values (other values can also be considered
false or true).

In numeric contexts (for example, when used as the argument to an
arithmetic operator), they behave like the integers 0 and 1, respectively.

The built-in function bool() can be used to cast any value to a Boolean,
if the value can be interpreted as a truth value

They are written as False and True, respectively.

Boolean Strings

A string in Python can be tested for truth value.

The return type will be in Boolean value (True or False)

Let’s make an example, by first create a new variable and give it a value.


my_string = "Hello World"

my_string.isalnum()		#check if all char are numbers
my_string.isalpha()		#check if all char in the string are alphabetic
my_string.isdigit()		#test if string contains digits
my_string.istitle()		#test if string contains title words
my_string.isupper()		#test if string contains upper case
my_string.islower()		#test if string contains lower case
my_string.isspace()		#test if string contains spaces
my_string.endswith('d')		#test if string endswith a d
my_string.startswith('H')	#test if string startswith H

To see what the return value (True or False) will be, simply print it out.	

my_string="Hello World"

print my_string.isalnum()		#False
print my_string.isalpha()		#False
print my_string.isdigit()		#False
print my_string.istitle()		#True
print my_string.isupper()		#False
print my_string.islower()		#False
print my_string.isspace()		#False
print my_string.endswith('d')		#True
print my_string.startswith('H')		#True

Boolean and logical operators

Boolean values respond to logical operators and / or

>>> True and False
False

>>> True and True
True

>>> False and True
False

>>> False or True
True

>>> False or False
False

Remember that the built-in type Boolean can hold only one of two possible
objects: True or False

The post Booleans, True or False in Python appeared first on PythonForBeginners.com.

]]>
https://www.pythonforbeginners.com/basics/boolean/feed 2 540