Why is True True True True True True not True in Python

0 votes

Code snippet 1:

a = True, True, True
b = (True, True, True)

print(a == b)

returns True.

Code snippet 2:

(True, True, True) == True, True, True

returns (False, True, True).

Jan 3, 2023 in Python by erzan
• 630 points
665 views

1 answer to this question.

0 votes

Operator precedence. You're actually checking equality between (True, True, True) and True in your second code snippet, and then building a tuple with that result as the first item.

Recall that in Python by specifying a comma-separated "list" of items without any brackets, it returns a tuple:

>>> a = True, True, True
>>> print(type(a))
<class 'tuple'>
>>> print(a)
(True, True, True)

Code snippet 2 is no exception here. You're attempting to build a tuple using the same syntax, it just so happens that the first element is (True, True, True) == True, the second element is True, and the third element is True.

So code snippet 2 is equivalent to:

(((True, True, True) == True), True, True)

And since (True, True, True) == True is False (you're comparing a tuple of three objects to a boolean here), the first element becomes False.

Master the art of Data Science with Python and unlock the power of insights.

answered Jan 4, 2023 by Elton
• 400 points

Related Questions In Python

0 votes
1 answer

Why there is no do while loop in python

There is no do...while loop because there ...READ MORE

answered Aug 6, 2018 in Python by Priyaj
• 58,020 points
7,654 views
0 votes
2 answers

Check a number is Prime or not in Python

There is an efficient way to write ...READ MORE

answered Oct 26, 2018 in Python by Priyaj
• 58,020 points
5,092 views
0 votes
1 answer

How do I check if input string is a valid regular expression or not in Python?

Hi. Good question! Well, just like what ...READ MORE

answered Feb 12, 2019 in Python by Nymeria
• 3,560 points
11,284 views
0 votes
1 answer

Python Error saying "NameError: global name 'true' is not defined"

It's a very small mistake. Change true ...READ MORE

answered Jun 17, 2019 in Python by Vinayak

edited Oct 7, 2021 by Sarfaraz 16,194 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
0 votes
1 answer

Python Math - TypeError: 'NoneType' object is not subscriptable

“TypeError: 'NoneType' object is not subscriptable” is ...READ MORE

answered Jan 4, 2023 in Python by Elton
• 400 points
1,015 views
0 votes
1 answer

What is the data type of (1) in python?

Typecasting is the process of converting one ...READ MORE

answered Jan 4, 2023 in Python by Elton
• 400 points
690 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP