Python Booleans

A boolean value in computer science is a datatype which can have one of the two values ‘true’ or ‘false’ which represents the truth values of logics.

In python, the boolean values are written as ‘true’ and ‘false’ (considering the case sensitivity of t and f).

When we evaluate any expression, we would get any one of the two values – ‘True’ or ‘False’.

Example

print(12 > 7)

print(5 == 4)

Output

True

False

Note : The boolean expressions are mainly associated with conditional statements. 

The conditional statements work on the grounds of boolean values. If the boolean value of any expression written in a conditional statement is true, the control shifts inside that block.

Example 1

x = 10

if(x > 7):
    print("x is greater than 7")

Output

x is greater than 7

Example 2

x = 4

if(x > 7):
    print("x is greater than 7")
else:
    print("x is smaller than 7")

Output

x is smaller than 7

Boolean Evaluation

bool() function :

The bool() function in python is used to evaluate a value. It returns true and false accordingly. A value in python is evaluated to true if it has some content.

For example-

print(bool("sagar"))
print(bool([1,2,3,4]))
print(bool(12))
print(bool(1.4))

Output

True

True

True

True

Given below some cases in which the bool function returns false-

  • If an empty element is passed, such an an empty list or an empty tuple.
  • If zero is passed.
  • If a false value is passed.
  • If nothing is passed.

isinstance() function :

In python, there is an inbuilt function which is used to find if an object is of a particular data type.

x = "hello world"

print(isinstance(x, str))

Output

True

Boolean Strings

There are many inbuilt functions in python which can be used to check whether a string is of any specific type or contains any specific characters.

  • isalpha() – Checks whether all characters are alphabets.
  • isalnum() – Checks whether all characters are numbers.
  • isdigit() – Checks whether all characters are digits.
  • istitle() – Checks whether string contains title words.
  • isupper() – Checks whether string contains upper case characters.
  • islower() – Checks whether string contains lower case characters.
  • isspace() – Checks whether string contains spaces.
  • endswith() – Checks whether string ends with a particular character.
  • startswith() – Checks whether string starts with a particular character.

Example

mywords = "Hello buddy"

print(mywords.isalnum())

print(mywords.isalpha())

print(mywords.isdigit())

print(mywords.istitle())

print(mywords.isupper())

print(mywords.islower())

print(mywords.isspace())

print(mywords.endswith('y'))

print(mywords.startswith('H'))

Output

False

False

False

False

False

False

False

True

True

Functions can return boolean values

We can create functions that returns boolean value.

The function given below returns True if the passed argument is greater than 10, otherwise it returns False.

def func(num):
    if num > 10:
        return True
    else:
        return False

print(func(17))

Output

True