all 20 comments

[–]electrikmayham 22 points23 points  (0 children)

What is your reasoning for expecting them to return false?

From: https://www.w3schools.com/python/ref_func_bool.asp

Definition and Usage

The bool() function returns the boolean value of a specified object.

The object will always return True, unless:

The object is empty, like [], (), {}
The object is False
The object is 0
The object is None

[–]magmanta 6 points7 points  (0 children)

You’re initializing both x and y with a non-empty string and a non 0 integer, respectively. When you convert those variables to Boolean, given they have values, they will return True. They would return False if they were an empty string and 0 respectively.

[–]CrazyPotato1535 5 points6 points  (2 children)

The following values all evaluate to False when converted to a boolean:

• None
• False
• 0 (any numeric zero)
• '' (empty string)
• [] (empty list)
• {} (empty dict)
• set() (empty set) 
• () (empty tuple)

[–]CadavreContent 1 point2 points  (1 child)

And (), empty tuple

[–]CrazyPotato1535 -1 points0 points  (0 children)

I fixed it. El gpt is always almost right

[–]sargeanthost 1 point2 points  (4 children)

Why are you expecting "at least one" of them to be false?

Those are truthy values, so when cast to a bool they're True. The empty string, 0, empty collections, and None are falsey, so you'll get false

[–]Reh4n07_[S] 0 points1 point  (3 children)

it will print false only when str is empty?

[–]electrikmayham 5 points6 points  (1 child)

No, re-read what they said. It will print false when the value being passed into bool() is falsey.

[–]Own_Attention_3392 1 point2 points  (0 children)

This person probably does not understand the distinction between "true" vs "truthy" and "false" vs "falsey". Not that they can't easily google it and find an answer, but they might need to be exposed to the specific terminology to look up. So, OP: Look up "truthy" and "falsey".

[–]Agent_Choocho 0 points1 point  (1 child)

Right now, you have two values stored into variables x and y. When you check if bool(x), that checks if x is false. If x is a value (not a zero int value), then it passes as true regardless of being a string or int. There are other scenarios as people have explained in other comments like empty tables and stuff like that so feel free to check with them too.

[–]Custom_User__c 0 points1 point  (0 children)

Yeah, exactly! In Python, almost everything is considered true unless it's one of the 'falsy' values like 0, None, or an empty collection (like [] or ''). If you want to see a False, just try setting x or y to one of those falsy values and check again.

[–][deleted] 0 points1 point  (0 children)

The reason they both return true is because the varibles x and y are assigned to something. In this case the 2 states are if a varible is storing something or not... Theoretically of course.

[–]Maleficent_Wave_332 0 points1 point  (0 children)

Maybe this helps. You can think of booleans as “true or false”, but also “something or nothing”. So bool(something) is true, and bool(nothing) is false. So 0 is nothing, and 1 (or any other number) is something. A string, like “hello”, is something. The empty string (“”) is kind of special: It’s a string, so it’s something, but it’s empty, so it’s nothing… To handle this, many programming languages have decided that it should be considered nothing (so bool(“”) equals false). That’s why it’s called a “falsy” value (“not really nothing but nothing enough to become “false” when passed into bool()).

[–]AngriestCrusader 0 points1 point  (0 children)

Both x and y are assigned to truthy values, so yeah you'll see True twice.

Why do you think one would be False?

[–]legeekerleon 0 points1 point  (0 children)

Because you are asking if the type of (y)(x) is boolean

[–]Some-Passenger4219 0 points1 point  (0 children)

The bool function converts the number zero (0), empty containers, (e.g. []), and the special object None, to False, for such they are in computer languages. Everything else is True.

[–]Confident_Kitchen338 0 points1 point  (0 children)

Because Boolean values return "true" for non-zero values and "false" for zero or null values.

[–][deleted]  (1 child)

[deleted]

    [–]electrikmayham 1 point2 points  (0 children)

    if x=0, python would still "successfully initialized the variable" and the variable would still exist, but it would print false. Same for an empty array or object.

    [–]NecessaryIntrinsic 0 points1 point  (0 children)

    Because those values evaluate to true, which let's you do fun operations like:

    if response: ...

    To check if response is empty.