Watch video presentation here and remember to subscribe to our youtube channel;In todays tutorial we will be discussing use of Booleans in python. Since python is an object oriented language then it uses Booleans as objects when comparing values.
Boolean -is expression that has a Boolean value, which are either true or false. For instance, you may use comparison operators(greater than, less than, equal, not equal, greater than or equal to etc.). Expressions my be evaluated either to produce true or false. Look at this examples;
print(15 < 11) print(20 == 30)
In the above example the answer should be either true or false, it compares the two values then gives out the result.
-As we had discussed in our if-else chapter, we saw that Booleans value are also applied to evaluate values, look at this example below;
age = 20 if age > 18: print("Person is eligible for national Id") else: print("Person not eligible for national Id")
In the above example it will compares whether the age defined the person is eligible for national Id or not.
How to evaluate values or variables?
When you wish to evaluate a variable or even a value we uses bool() function which will give out either true or false. Look at these examples;
case 1, print(bool("I am learning Boolean")) Case 2, a = "I am philloh" b = 22 print(bool(a)) print(bool(b))
In the first case we are evaluating a string, you can also use a number instead while in case 2 we are evaluating variables.
Use Of Functions To Return Boolean Value
As we discussed in our previous tutorial on functions we explained how functions can be used to return values. In this part we will see how functions can be used to return a Boolean value. Look at this example;
def my_function(): return True print(my_function())
In other case you may execute code according to the answer given out by the function from the Boolean value. Look at this example below;
def my_function(): return True if my_function(): print("Python is very easy") else: print("Python is not easy")
-You may also use isinstance() function which checks out whether a certain object is of a certain data type. There are other built in functions that are used to return Boolean values, look at this example for better understanding;
a = "Philloh" print(isinstance(a, str))
In the example above it will check whether the variable defined a string or not, in that case it produces true since the object is a string.
Some Cases When Values Are Evaluated to Either True Or False?
There are some cases when values are evaluated to either true or false that may include;
- All numbers are evaluated to true but it is Zero it is evaluated to false.
- All strings/list/tuple/sets are evaluated to true but when they are empty then they are evaluated to false.
Look at this examples;
Case 1, #that are evaluated to true bool("Philloh") bool(["BMW", "Toyota", "Ford"]) Case 2, #that are evaluated to false bool(0) boo() bool("") bool(false)
Thanks for reading through our tutorial, follow us on our youtube channel for more video tutorials and live sessions we hold. As we continue learning about Python hoping to meet you in our next tutorial.