|
|||
|
Boolean Variables:-
A Boolean variable can reference one of two values: True or False. Boolean variables are commonly used as flags, which indicate whether specific conditions exist. We have worked with int, float, and str (string) variables. In addition to these data types, Python also provides a bool data type. The boo1 data type allows you to create variables that may reference one of two possible values: True or False. Here are examples of how we assign values to a boo1 variable: hungry = True sleepy = False Boolean variables are most commonly used as flags. A flag is a variable that signals when some condition exists in the program. When the flag variable is set to False, it indicates the condition does not exist. When the flag variable is set to True, it means the condition does exist. For example, suppose a salesperson has a quota of $50,000. Assuming sales references the amount that the salesperson has sold, the following code determines whether the quota has been met: if sales >= 50000.0: sales-quota-met = True else: sales-quota-met = False As a result of this code, the sales - quota-met variable can be used as a flag to indicate whether the sales quota has been met. Later in the program we might test the flag in the following way: if sales-quota-met: print 'You have met your sales quota! ' This code displays 'YOU have met your sales quota! if the boo1 variable s a l e s quota-met is True. Notice that we did not have to use the == operator to explicitly compare the sales-quota-met variable with the value True. This code is equivalent to the following: if sales - quota-met == True: print 'You have met your sales quota! ' |