r/PythonLearning • u/Ichangedtoacat • Oct 19 '25
I made a little python question thing cause i got bored.
25
Oct 19 '25
Yes regardless of x because 6 != 0
4
u/Melodic_Editor3467 Oct 19 '25
can you please explain the logic here in more detail? why are you stated 6 is not equal to zero? is this not obvious? so 5 is assigned to X therefore X is equal to 5 or 6 which results in Yes. If 0 is assigned to X then this would result in No. Am I missing something here?
2
u/SkeletoaBro Oct 19 '25
This had me giggling while reading. Does that make me a bad person?
2
u/Melodic_Editor3467 Oct 19 '25
you know understanding truthy and falsy values is crucial for writing effective conditional statements, but this the thing, I don't understand any of this because the last time a attempted to learn python was 6 years ago. I'm just trying to work it out using basic logic in my head. I kind of want to start learning python again now. LoL.
2
u/Ichangedtoacat Oct 19 '25
Damn this comment chain went on very long lol, nice to know some people are learning from it lol.
2
Oct 19 '25
I don't think there's that much of a trick to truth and falsity, if a number is zero or a container like list/dict/string is empty then they are false, otherwise they are true. I think many people don't really understand types when they start out and python hides them from you somewhat so its not the best starting point imo
10
u/bazub Oct 19 '25
If “x==5 or 6” is interpreted as “(x==5) or (6==truthy)”,not as “x == (either 5 or 6)”
1
u/pimp-bangin Oct 19 '25
Search "python evaluation order" or "python operator precedence" to get a deeper understanding.
This article looks pretty decent https://www.geeksforgeeks.org/python/precedence-and-associativity-of-operators-in-python/
6
2
u/eenbob Oct 19 '25
I think because it are separate conditions. But condition 6 is always true? Because auto convert 6 to true or false type and kabang. Confusion?
2
Oct 19 '25
Yeah he's using the wrong type of conditional logic in this if statement. Correct me if I'm wrong, but it should be
if x == 5 or x == 6:
1
0
Oct 19 '25
[deleted]
1
u/Melodic_Editor3467 Oct 19 '25
why?
1
u/deceze Oct 19 '25
Because
or 6is a separate condition that has nothing to do withx, and6is always truthy.1
u/Depnids Oct 19 '25
This was my immediate thought, but I was thinking maybe if it was a trick question, and something more fucked up happened: «5 or 6» evaluated to true, because both are truthy. And then x == [truthy thing] would be x == 1 which would then be false.
Would x == (5 or 6) behave like this?
1
Oct 19 '25
x == (5 or 6) -> x == True or True -> x == True -> x (as a boolean). X will be cast into a bool whenever you compare it to a bool, so its value doesn't need to be exactly one, just non-zero
1
22
12
u/deceze Oct 19 '25
This would’ve been more “challenging” if you initialized x to something other than 5 or 6…
7
u/RTK_x Oct 19 '25
The condition states: if (x equals 5) or (6 is true) So even if x doesn't equal 5 it would print yes since any number except zero translates into TRUE.
4
1
u/Vigintillionn Oct 19 '25
I also show this each time in my first session to first bachelor students learning python. You should however initialize x to something else than 5 or 6, then I ask them to raise their hands if they think “No” gets printed and nearly everyone always thinks it does. It shows how important to is to properly explain truthy and falsey values
0
u/Ichangedtoacat Oct 19 '25
Yep ik, i wanted to make a simple question just out of boredom but good to know it can also be used as a teaching tool.
1
1
1
1
u/QueryQueryConQuery Oct 20 '25
A better one would of been
x = 7
if x == 5 or 6:
print("Yes")
else:
print("No")
Would of been a little harder
3
u/JhonMHunter Oct 19 '25
Am I missing something? Cause he sets x to 5 then asks if x is 5 if yes then print. Seems simple enough?