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.
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…
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.
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.
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.
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.
# 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")