Snalk

Snalk » Programming Tutorials » Python » Boolean Variables

Reply
  #1 (permalink)  
Old 10-30-2008, 01:47 PM
Junior Member
 
Join Date: Oct 2008
Posts: 8
Post Boolean Variables

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! '
Reply With Quote
Reply

Bookmarks

Tags
boolean variables

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump



All times are GMT. The time now is 03:04 PM.
Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.