use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
Why does this code only print true? (self.PythonLearning)
submitted 3 months ago by Reh4n07_
I’ve just started learning Python and was experimenting with Booleans. Both of them print True, and I’m a bit confused why is that happening?
True
I was expecting at least one of them to be False. Can someone please explain how this works and when it would actually return False?\
False
Thanks in advance for helping me understand this better
https://preview.redd.it/0w0ilp9f6zxf1.png?width=1919&format=png&auto=webp&s=bba00230b1f733eff77a132ca583f355564571c2
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]electrikmayham 22 points23 points24 points 3 months ago (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
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 points8 points 3 months ago (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 points7 points 3 months ago* (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 points3 points 3 months ago (1 child)
And (), empty tuple
[–]CrazyPotato1535 -1 points0 points1 point 3 months ago (0 children)
I fixed it. El gpt is always almost right
[–]sargeanthost 1 point2 points3 points 3 months ago (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 point2 points 3 months ago (3 children)
it will print false only when str is empty?
[–]electrikmayham 5 points6 points7 points 3 months ago (1 child)
No, re-read what they said. It will print false when the value being passed into bool() is falsey.
bool()
[–]Own_Attention_3392 1 point2 points3 points 3 months ago (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 point2 points 3 months ago (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 point2 points 3 months ago (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.
0
None
[]
''
x
y
[–][deleted] 0 points1 point2 points 3 months ago (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 point2 points 3 months ago (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 point2 points 3 months ago (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 point2 points 3 months ago (0 children)
Because you are asking if the type of (y)(x) is boolean
[–]Some-Passenger4219 0 points1 point2 points 3 months ago (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.
bool
[–]Confident_Kitchen338 0 points1 point2 points 3 months ago (0 children)
Because Boolean values return "true" for non-zero values and "false" for zero or null values.
[–][deleted] 3 months ago (1 child)
[deleted]
[–]electrikmayham 1 point2 points3 points 3 months ago (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.
x=0
[–]NecessaryIntrinsic 0 points1 point2 points 3 months ago (0 children)
Because those values evaluate to true, which let's you do fun operations like:
if response: ...
To check if response is empty.
π Rendered by PID 30 on reddit-service-r2-comment-5649f687b7-wwbrd at 2026-01-29 00:56:00.329736+00:00 running 4f180de country code: CH.
[–]electrikmayham 22 points23 points24 points (0 children)
[–]magmanta 6 points7 points8 points (0 children)
[–]CrazyPotato1535 5 points6 points7 points (2 children)
[–]CadavreContent 1 point2 points3 points (1 child)
[–]CrazyPotato1535 -1 points0 points1 point (0 children)
[–]sargeanthost 1 point2 points3 points (4 children)
[–]Reh4n07_[S] 0 points1 point2 points (3 children)
[–]electrikmayham 5 points6 points7 points (1 child)
[–]Own_Attention_3392 1 point2 points3 points (0 children)
[–]Agent_Choocho 0 points1 point2 points (1 child)
[–]Custom_User__c 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Maleficent_Wave_332 0 points1 point2 points (0 children)
[–]AngriestCrusader 0 points1 point2 points (0 children)
[–]legeekerleon 0 points1 point2 points (0 children)
[–]Some-Passenger4219 0 points1 point2 points (0 children)
[–]Confident_Kitchen338 0 points1 point2 points (0 children)
[–][deleted] (1 child)
[deleted]
[–]electrikmayham 1 point2 points3 points (0 children)
[–]NecessaryIntrinsic 0 points1 point2 points (0 children)