Practical use of booleans | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson

Practical use of booleans

What is the practical use for these types?

30th Aug 2019, 1:49 AM
Andrew Johnson
Andrew Johnson - avatar
3 Answers
+ 5
A flag is nothing official in a programming language. I found this short definition in In computer science, a flag is a value that acts as a signal for a function or process. The value of the flag is used to determine the next step of a program. Flags are often binary flags, which contain a boolean value (true or false). ... A common example of a flag in computer programming is a variable in a while loop. A flag can be stored in a variable so it holds just one information. But flags can also be stored in data structures like lists or dictionaries. An example for this can be used if you have to check a user input against some conditions the input should have. So if you have to check if a password meets a length of min 5 and max 10 characters, and it also needs to have at least on digit you can store each result of all this things as a flag. Let's have a dict like this: results = { "length" : True, "digit" : True, ... } So you can do all necessary checks and store the result in the appropriate place. If you have done all these checks, you can see with all() or any() if your dict meets all conditions of user input (pass or fail).
30th Aug 2019, 9:08 AM
Lothar - avatar
+ 4
Thank you for your time, i just learned if and elif statements so i will learn about it soon because i don't understand what you are talking about now lol.
30th Aug 2019, 2:49 AM
Andrew Johnson
Andrew Johnson - avatar
+ 3
I guess I have not come across flags yet. What exactly are they?
30th Aug 2019, 2:39 AM
Andrew Johnson
Andrew Johnson - avatar