r/PythonLearning • u/saikat_hossen • 7d ago
Python Boolean
I'm having some trouble understanding Python boolean logic, it’s so simple to let it be confusing. Can anyone explain it to me in their own language?
2
Upvotes
r/PythonLearning • u/saikat_hossen • 7d ago
I'm having some trouble understanding Python boolean logic, it’s so simple to let it be confusing. Can anyone explain it to me in their own language?
2
u/Lirianov 6d ago edited 6d ago
Booleans are very simple, its either True or False.
All of the below are considered False by Python:
NoneFalse0&0.0""(empty string)[],(),{}(empty list, set, or dict)Basically anything thats empty is False, all else are True.
looking at your code, you have:
which is just taking a input from the user and saving it as a
stringthen:
here you're hardcoding the boolean
Truetohas_NIDthen your
ifstatement:First thing I noticed is that your first
printstatement is not indented 4 spaces. Also, yourage == 18is incorrect for two reasons.First, you're comparing a
stringto aintwhich will always beFalse. Correct it by changing the value ofageto aint.Next, you're checking if the user is 18 only, what if they are older? Someone who's 25 and has_NID will be told no. Correct it by checking if the person's age is equal or greater than 18.
Lastly, it seems you switched the two
printstatements around. Theifstatement shouldprint('You are eligible for electing')and theelseshouldprint('You are not eligible for electing').BTW, is it supposed to be "electing" or "voting"?
Full correct code: