Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can someone explain to me what python is doing here? [duplicate]

Tags:

python

>>> 3 > 2 == True
 False  #say what?
>>> (3 > 2) == True
 True
>>> 3 > (2 == True)
 True
>>> 3 > 1 == True
 True
>>> 3 > False
 True

What is Python doing in its godforsaken hidden logics that makes that first statement False, while the rest are True?

like image 203
Andrew Avatar asked Jan 08 '14 17:01

Andrew


People also ask

What is duplicate Python?

Definition and Usage. The duplicated() method returns a Series with True and False values that describe which rows in the DataFrame are duplicated and not. Use the subset parameter to specify if any columns should not be considered when looking for duplicates.

How do you check if there is duplicate in Python?

For this, we will use the count() method. The count() method, when invoked on a list, takes the element as input argument and returns the number of times the element is present in the list. For checking if the list contains duplicate elements, we will count the frequency of each element.

Can Python set duplicate?

A set is an unordered collection of items. Every set element is unique (no duplicates) and must be immutable (cannot be changed).

Which allows duplicates in Python?

Tuple is a collection which is ordered and unchangeable. Allows duplicate members.


1 Answers

This is a chained comparison (see here in the docs), the same way that

>>> 1 < 2 < 3
True

is

>>> (1 < 2) and (2 < 3)
True

In this case, we have

>>> 3 > 2 == True
False

because

>>> (3 > 2) and (2 == True)
False

because

>>> (3 > 2), (2 == True)
(True, False)
like image 57
DSM Avatar answered Oct 15 '22 17:10

DSM