Can I use True and False like normal variables?

Question

True and False seem like valid variable names, so can these be used like normal variables in Python?

Answer

No, True and False cannot be used like other variables. Reassigning these will result in a SyntaxError.

In Python, True and False are known as reserved words or keywords, which means they cannot be used as ordinary identifiers and cannot be reassigned like a variable.

This is an important implementation, because it would prevent issues like accidentally updating the values to something different, like True = 100 or False = True which would be an inconvenience when we need to utilize them in a program later on.

21 Likes

ā€œTrueā€ and ā€œFalseā€ are reserved words, however ā€œtrueā€ and ā€œfalseā€ are not, the first letter capitalization appears to be the difference.

23 Likes

Without quotes, though. True and False. Their numeric equivalents are 1 and 0.

int(True)     # 1
int(False)    # 0
26 Likes

We can also go in the opposite direction:

>>> bool(1)
True
>>> bool(0)
False

Also note this:

>>> bool(7)
True
>>> bool([])
False
>>> bool(None)
False
>>> bool({})
False
>>> bool(())
False
>>> bool([0])
True
14 Likes

I used quotation marks not to indicate a string, but to specify the words being referred to.

I can name a variable:
true = 10

but I can not name a variable:
True = 10

My apologies for any confusion caused by using quotation marks.

7 Likes

No problem; I thought as much but felt the need to interject. Happily, @appylpye added to the topic to bring about balance.

Point taken. We cannot redefine a primitive.

1 = 0

'a' = 'b'
3 Likes

I see that even in the forum syntax is important. Always learning and improving! :grinning::grinning::grinning:

9 Likes

While there is absolutely nothing wrong with using true as a variable, i will rather avoid it so as to avoid room for confusion (even though it shouldn’t cause any confusion). Simplicity and precision makes our code better

9 Likes

This is a very good point, while it could be used, for all intents and purposes it is a terrible variable name that could very easily lead to confusion and mistakes.

7 Likes

2 posts were split to a new topic: [PENDING] Typo in Python 3 Boolean Variables Lesson

can you explain more of the computer’s reasoning with the later examples? How does it go from binary into determining the examples you showed? It hasn’t been covered yet has it? Thanks!

Will sub in for @appylpye as he is on vacation.

Have you studied the truth values, truthy and falsy? They have a bearing on what those all mean.

Any non-zero number has a truth value of, truthy which translates to boolean, True.

Any data structure when empty is falsy. When not empty, is truthy. Even strings behave this way. Essentially, any empty string or structure if falsy, signals they are empty.

>>> x = ''
>>> if x: print ("Not empty")
else: print ("Empty")

Empty
>>> 
>>> x = 'a'
>>> if x: print ("Not empty")
else: print ("Empty")

Not empty
>>> 

To recap the falsy values:

0, '', "", '''''', """""", [], {}, (), and, None.

Does False fit into that list? Not really. It IS False and needs no evaluation, unlike all those above.


Also note that in Python, and perhaps other languages, a boolean is also an integer, either 0 for False or 1 for True.

>>> 1 == True
True
>>> True == 1
True
>>> 0 == False
True
>>> False == 0
True
>>> 

And.

>>> True + True
2
>>> True - True
0
>>> True * True
1
>>> True / True
1.0
>>> True // True
1
>>> True + False
1
>>> True * False
0
>>> 

And now to throw some true weirdness your way…

>>> def foo(a=[]):
	if a: a += [a[-1]*2]
	else: a += [1]
	return a

>>> foo()
[1]
>>> foo()
[1, 2]
>>> foo()
[1, 2, 4]
>>> foo()
[1, 2, 4, 8]
>>> 

This is truly coming from out in left field as likely none of it has come up yet, so ease up and take a breath. When you get to the unit on default values in functions, come back and take another look. At that time you will be better equipped and can expand on your knowledge with a Google search. This particular example (or one similar) comes up in the Intermediate Python 3 course, so don’t let this distract your progress.

>>> for _ in range(11):
	print (foo())

	
[1]
[1, 2]
[1, 2, 4]
[1, 2, 4, 8]
[1, 2, 4, 8, 16]
[1, 2, 4, 8, 16, 32]
[1, 2, 4, 8, 16, 32, 64]
[1, 2, 4, 8, 16, 32, 64, 128]
[1, 2, 4, 8, 16, 32, 64, 128, 256]
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]
>>> 
4 Likes

okay thanks a lot this was really helpful!

2 Likes

For when you reach this topic, in earnest, this program lets us predict what the next power of two will be. Just add 1 to the results.

>>> def foo(a=[], b=[0]):
	if a: a += [a[-1] * 2]
	else: a += [1]
	b[0] += a[-1]
	return a, b[0]

>>> for _ in range(11):
	print (foo())

	
([1], 1)
([1, 2], 3)
([1, 2, 4], 7)
([1, 2, 4, 8], 15)
([1, 2, 4, 8, 16], 31)
([1, 2, 4, 8, 16, 32], 63)
([1, 2, 4, 8, 16, 32, 64], 127)
([1, 2, 4, 8, 16, 32, 64, 128], 255)
([1, 2, 4, 8, 16, 32, 64, 128, 256], 511)
([1, 2, 4, 8, 16, 32, 64, 128, 256, 512], 1023)
([1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024], 2047)
>>> 

Here is where it gets real weird, and good reason to bookmark this thread…

>>> def foo(c=2, a=[], b=[0]):
	if a: a += [a[-1] * c]
	else: a += [1]
	b[0] += a[-1]
	return a, b[0]

>>> foo(8)
([1], 1)
>>> foo(8)
([1, 8], 9)
>>> foo(8)
([1, 8, 64], 73)
>>> foo(8)
([1, 8, 64, 512], 585)
>>> 

At some point this is no longer weird, but practical. Weird science, as it were. Still trying to make this practical, but it’s been fun. Sums only apply in base 2, if anyone is wondering.

1 Like

So what I’ve gathered is that the following statement is technically not a boolean statement…but don’t dare try to use it among your fellow coders:

ā€œusing ā€˜true’ or ā€˜false’ as variables are bad ideasā€.

1 Like

true = True
false = False
bingo! KEKW

1 Like

you could do
true = True
if you were tired of capitalizing True. you could just put the above line as one of your first instructions to make it easier

If one is really tired of typing we can also use 1. In Python True is also an integer with the value, 1.

>>> a = int(True)
...  
>>> a
...  
1
>>> 

in: Control Flow

Boolean Operators: not

this code is logically correct, but it is still angry with me:

statement_one = not (4 + 5 <= 9)

statement_two = not (8 * 2) != 20 - 4

credits = 120
gpa = 1.8

if credits < 120:
print(ā€œYou do not have enough credits to graduate.ā€)
if gpa < 2.0:
print(ā€œYour GPA is not high enough to graduate.ā€)
if credits < 120 and gpa < 2.0:
print(ā€œYou do not meet either requirement to graduate!ā€)

it says:
Did you add an if statement that checks that credits is not greater than or equal to 120?