Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why '2'<'1'== False output False in python3? [duplicate]

Tags:

python-3.x

'2'<'1'== False #False
('2'<'1')== False #True
'2'<('1'== False) #error

code in python3 we know operator precedence in python3 https://docs.python.org/3/reference/expressions.html#operator-precedence

like image 935
wzf wu Avatar asked Nov 05 '20 05:11

wzf wu


People also ask

Why a '>' A is true in Python?

This is because on the ASCII (American Standard Code For Information Interchange) CHART the letter "a" equates to 97 (in decimal values) while the letter "b" equates to 98 (in the decimal values).

Is negative 1 true in Python?

Numbers. In Python, the integer 0 is always False , while every other number, including negative numbers, are True .

Is False the same as 0 in Python?

Python assigns boolean values to values of other types. For numerical types like integers and floating-points, zero values are false and non-zero values are true. For strings, empty strings are false and non-empty strings are true.

How to check if true false in Python?

Answer: There 3 ways to check if true false in Python. Let’s see the syntax for an If statement using a boolean. Example code if true false example code. Simple python example code. Boolean values are the two constant objects False and True. It has the value False.

What is true and false in JavaScript?

All other values are considered true — so objects of many types are always true. Operations and built-in functions that have a Boolean result always return 0 or False for false and 1 or True for true, unless otherwise stated. (Important exception: the Boolean operations or and and always return one of their operands.)

Why is 2'<false) an illegal operation in Python?

first ('1' == False) is evaluated which is False but now the operation is '2'<False which is an illegal operation in python It is because the default behaviour for equality comparison (== and !=) is based on the identity of the objects, and as object False and object '1' does not share the same identity thus the result will be False

What is the difference between true and false in Boolean values?

On the other hand, True is treated as 1 in many cases (and False as 0) which you can see when you do stuff like: Boolean values are the two constant objects False and True. They are used to represent truth values (although other values can also be considered false or true).


Video Answer


1 Answers

In case 1 :-

'2'<'1'== False

it is evaluated as '2'<'1' and '1' == False according to the operator chaining {thanks @ymonad to provide this link}

which will evaluated to be False

in case 2 :-

('2'<'1')== False

as () have higher precedence so will be evaluated first. so the expression will be reduced to False == False which will evaluate to be True

in case 3 :-

'2'<('1'== False)

first ('1' == False) is evaluated which is False but now the operation is '2'<False which is an illegal operation in python


EDIT:

To answer a question raised by @snr in the comment section

The vital question is why '1'== False is valid while '2'< bool is not

It is because the default behaviour for equality comparison (== and !=) is based on the identity of the objects, and as object False and object '1' does not share the same identity thus the result will be False

whereas a default behaviour of other comparison (<, >, <=, and >=) is not provided thus an attempt to do so raises TypeError


you can find this in documentation link provided by OP (under the heading value-comparisions)

like image 169
Arsenic Avatar answered Dec 11 '22 20:12

Arsenic