Python Forum
Just some Boolean values musings
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Just some Boolean values musings
#1
Hi!

This is not a question, but just some musings of mine on Boolean values ... that maybe are useful to some other newbies.

I was learning about Boolean values and their use in conditions, and I didn't know that when used in conditions, 0, 0.0, and "" (the empty string) are considered 'False', while all other values are considered 'True'.

I wanted to check the truth in that (no pun intended! Big Grin ), so I made this little program:

name1 = ''
name2 = 0
name3 = 0.0
name4 = 6
name5 = 'John'

if not name1:
    print('This means that name1 = "" (empty string) is considered False, \
so not name1 is considered True.')
if name1:
    print('This means that name1 = "" (empty string) is considered True.')

if not name2:
    print('This means that name2 = 0 is considered False, \
so not name2 is considered True.')
if name2:
    print('This means that name2 = 0 is considered True.')

if not name3:
    print('This means that name3 = 0.0 is considered False, \
so not name3 is considered True.')
if name3:
    print('This means that name3 = 0.0 is considered True.')

if not name4:
    print('This means that name4 = 6 is considered False, \
so not name4 is considered True.')
if name4:
    print('This means that name4 = 6 is considered True.')

if not name5:
    print("This means that name5 = 'John' is considered False, \
so not name5 = 'John' is considered True.")
if name5:
    print("This means that name5 = 'John' is considered True.") 
producing the following self-explanatory output:
Output:
This means that name1 = "" (empty string) is considered False, so not name1 is considered True. This means that name2 = 0 is considered False, so not name2 is considered True. This means that name3 = 0.0 is considered False, so not name3 is considered True. This means that name4 = 6 is considered True. This means that name5 = 'John' is considered True. >>>
Maybe it's helpful to other newbies.

All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#2
(Oct-04-2019, 04:34 AM)newbieAuggie2019 Wrote: 0, 0.0, and "" (the empty string) are considered 'False', while all other values are considered 'True'
Technically the second part of this statement (my bold) is not 100% true - None and empty sequences and container types like list, tuple, dict, etc. are also evaluated False

https://docs.python.org/3/library/stdtypes.html#truth
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
(Oct-04-2019, 05:55 AM)buran Wrote:
(Oct-04-2019, 04:34 AM)newbieAuggie2019 Wrote: 0, 0.0, and "" (the empty string) are considered 'False', while all other values are considered 'True'
Technically the second part of this statement (my bold) is not 100% true - None and empty sequences and container types like list, tuple, dict, etc. are also evaluated False

https://docs.python.org/3/library/stdtypes.html#truth
Another little bit that I've just learned!!! Thank you!

It makes sense, as it seems to me that None, empty sequences and other empty container types like list, tuple, dict, etc. give me the same linguistic feeling as '0', '0.0' or an empty string.

All the best,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need to parse a list of boolean columns inside a list and return true values Python84 4 2,131 Jan-09-2022, 02:39 AM
Last Post: Python84
  effective means to flip boolean values? Exsul 3 4,380 Aug-25-2019, 03:58 PM
Last Post: Exsul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020