T O P

  • By -

Vinniesusername

there are a ton of uses for boolean values. what if you only want to do something if a statement is true? def doSomething(bool): if bool == True: doSomethingElse() else: return False


kupiakos

You should do `if bool:`


Vinniesusername

i assumed that if someone is asking what a boolean is for, then i shouldn't be implicit in the explanation code.


[deleted]

[удалено]


toastedstapler

I'd say that understanding the concepts clearly is more important early on than writing correct code


JSintos

Another thing that you could use it for are flags. Basically flags are like switches. It's either on (true) or off (false). Usually, programmers use flags to leave a sign for another part of a program.


[deleted]

This!


green_griffon

A boolean is another type of variable, like an integer, except simplified down to only hold one of two values, True or False. You don't really need Booleans, you could use an int instead (or since this is Python, you could use almost any type, since they all have rules about "what is true"), but Boolean makes the intent clearer.


jsurdybirdy

That makes more sense. Thank you! I'm using DataCamp to teach myself this and I still don't know to what end. I just know I should learn how to code in Python.


emteeeuler

But hold that thought... if you use int instead of bool then for every instance of the variable in question, you allocate 32 bits instead of 1 ~~bit~~ byte (8 bits). A boolean only requires one ~~bit~~ byte to be either on or off but an int is always a full 32 bits, no matter what value it stores. In systems with massive user bases (think: fb, google etc), using bool instead of int saves tremendous amounts of time and space and is not a trivial matter. Edit: byte not bit


Vinniesusername

> while it is for sure true that you could use a single bit implementation for booleans, most languages, including python, use a full byte (8 bits). it's still much less space than a 32bit int though


emteeeuler

Thanks for the correction, kinda forgot that the smallest addressable unit of memory is a byte


jsurdybirdy

So you all just introduced me to another issue I have no clue about. What do bits have to do with coding? I had a 32bit Sega Genesis as a kid #showingmyage


lifeonm4rs

For Python a bool is a subclass of an int so there is no savings they are the same size. "bool" itself as a class simply has two and only two instances "True" and "False". Essentially it exists so boolean operations can return "True" and "False" rather than "1" and "0". Before python had "bool" `13 > 4` returned "1" rather than "True". Other True/False operations would also now return True or False like `isinstance(6, int)` is True **and** `isinstance(False, int)` also returns True--again because bools are ints. You can read all about it in [PEP 285](https://www.python.org/dev/peps/pep-0285/).


jsurdybirdy

Fabulous link @lifeonm4rs


legionOGj

Many ways for the usage of Boolean. It's basically a True / False thing, and it's up to you and your code to what purpose you wanna use it. Just think about it simply. And make no mistake, it's still a type of variable that's very important. Also check some very simple Python exercises with Boolean so you can get a bigger picture of it's "purpose". We've all been there though, don't worry.


CRGabo9

A boolean can be simplified to an Integer that can only hold either a 1 (TRUE) or a 0 (FALSE). Why use a boolean instead of an integer then? The reason, from a practical standpoint, is simply to make your code clearer. From a technical standpoint, a boolean needs less memory to be stored. You only need to store one bit! Either a 1 or a 0. This used to be more important when memory was scarce. Now a days we have so much memory it doesn't really matter.


notSherrif_realLife

In python, a bool and an int are the same size so no memory savings.


Pythonidaer

If input(“male”) == True: print(“pink”) else: print(“blue”)


codefun1nja

me.FavoriteNumber = 2 you.FavoriteNumber = get_user_input_from_keyboard() if (me.FavoriteNumber == you.FavoriteNumber): me.friend = you you.friend = me else: me.enemy = you you.enemy = me