# When would I want to use a boolean?

### Question

What are some common uses for booleans?

Unlike other data types we’ve learned about like numbers and strings, the use of booleans might not be as immediately obvious. There are lots of times we want to control how our program behaves with boolean conditions. For example, if we want to continue asking a question until the user provides valid input, we might use a boolean variable that is set to False and only becomes True when their input is checked and is valid.

9 Likes

Hi, do you have any real example of the use of Booleans? I believe True/False term is something that might be important, but still don’t get it when to use it on real case…

Thanks…

2 Likes

Two things to understand about booleans, `primitive` and `expression`.

primitive

``````True
False
``````

Primitives are literal.

expressions

``````a == b
a or b
a and b
``````

where the entire thing is an expression, but also where `a` and `b` can be literals or expressions.

Expressions yield a value, and boolean expressions yield a boolean.

`````` a + b  =>  a value (the sum of a and b)
s + t  =>  a value (the concatenation of two strings)

a == b  =>  a boolean (True or False)
a or b   => a value, `a` or `b` whichever is True first (`b` if both False).
a and b  =>  a value, `a` if it is *falsy*, else `b`.
``````

Expressions are considered truthy when they resolve to a number that is non-zero, a string that is not empty, or not `None`.

``````"A" and "B"
``````

will evaluate to `B` since they are both strings with length greater than zero.

``````1 and 2
``````

will evaluate to `2` since both numbers are non-zero, so the yield with be the last operand.

``````1 or 2
``````

will evaluate to `1` since it is the first non-zero operand.

Some more examples…

``````>>> "" and True
''
>>> "" and False
''
>>> '' or True
True
>>> '' or False
False
>>>
``````

if and while

``````if expression:
print ("%r is True") % expression
else:
print ("%r is False") % expression
``````
``````while expression:
# code block
if condition: break
``````

`expression` above is any expression, but it is evaluated before proceeding to determine truthiness (yields a boolean).

The above may be a little confusing. Ask if you still don’t understand.

15 Likes

I with you i really dont know what i would use it for other than True or False

Well thank you it help alot

1 Like

well, thank you it will help me when in use

So maybe in if statements?

I still have no clue on how to use True and False. The concept of Primitives and Expressions is unclear. Can you explain it in a different way?

Programs are more useful if they can do different things depending on the situation.
Test whether some condition holds, and then run different code based on the result.

3 Likes

I usually think of something like is “is user active”. Think of entering a username and verifying that its active or not, so True or False.

2 Likes

Isn’t this all the more complicating?  Not sure why this is such an issue?? Isn’t it fairly obvious that, as a programmer, you might need statements such as

``````if some_condition:
do something
else:
do something else
``````

… where some_condition is a boolean expression, i.e., an expression that returns True or False.

That’s it. Yes, you do need to take some care about how some_condition is formulated to avoid getting into some of the weeds @mtf was discussing – `if a in (b or c or d):` probably won’t do what you want it to, for instance – but the value of expressions that return True or False seems incontestable.

3 Likes

yop, asking. will this be reviewed in an exercise more… I may understand your explanation then… moving on.

It will come up in other lessons so you should get more practice. Boolean expressions are a fundamental component of programming.

2 Likes

They can learn python if they have an account with codecademy

They_can_learn_code = Yes
print (They can learn code)
True
Type(They can learn code)

print (They cant learn code)
false
Type(They cant learn code)
<class ‘bool’>

If they have an account with codecademy, they can learn python - It shows true
If they dont have an account, they can learn python - it shows false
This is just an example concept, hope it helps.

2 Likes

i guess one use of these booleans might be the security systems…for example when you want to put the right password

I hope this code helps.

# Ask 2 numbers from user

first_number = input("Enter 1st number : ")
second_number = input("Enter 2nd number : ")

# assign the result (which is boolean) to number_equal

number_equal = first_number == second_number

# prints the value of number_equal

print (number_equal)

# If boolean variable is True, print a string

if number_equal == True:

# boolean was used to accomplish this just to give an example, but can be done without the boolean variable by comparing the 2 numbers directly here

print (“The 2 numbers are equal”)
else:
print (“Numbers are not equal”)

I hope this code helps.

# Ask 2 numbers from user - use this on your IDE # first_number = input("Enter 1st number : ") # second_number = input("Enter 2nd number : ") # assign numbers to 2 variables first_number = 2 second_number = 2 # check if the 2 numbers are equal and # assign the result (which is boolean) to number_equal number_equal = first_number == second_number # prints the value of number_equal print (number_equal) # Example of how to use boolean # If boolean variable is True, print both numbers are equal else print not equal if number_equal == True: print ("The 2 numbers are equal") else: print ("Numbers are not equal")
1 Like

It really helps! Thanks!