0
In what situation would you use Boolean Not?
I recently learned what Boolean Not is and I was wondering what would be the point of using it instead of using Booleans or simple Boolean Logic? In what situation would this be needed or wanted?! Please explain as simple as possible (I'm a beginner)
19 odpowiedzi
+ 1
Ayanna
'If' statement looks like this:
if something is true then do something2
It is not necessary to do:
If (something is false) is true do something2
It is better to do:
If (not something) is true do something2.
It is described by a lot of big companies in best coding practices.
+ 3
James
Writing:
while x == True
is a bad practice of coding, instead of it you should write:
while x.
And Boolean Not ISN'T '!=' ...
+ 2
As for the stack example. In python
stack = []
for i in range(5):
stack.append(i)
while stack:
stack.pop()
is a basic example. Empty data or data assigned 0 initializes false. Using == or != is perfectly fine also.
+ 1
I'm sorry I don't understand, will guys explain in more detail please?
+ 1
belowhundred = True
numberis = 0
while belowhundred is not False:
if numberis >= 100:
print("Woop hit 100")
belowhundred = False
elif numberis < 100:
print(numberis, "is still below 100")
numberis += 10
You could use it if you need to set some setting variables for example that have exactly two different possibilities like on off switch
+ 1
Ayanna
If you have a functions which returns boolean then you have to you it to set the if/while statements in correct way.
For example:
You have an queue structure and function empty() returning true is this queue is empty or false if it is not empty. You want to check if it is not empty, then the correct way to do this is:
if (!queue.empty()) instead of if(queue.empty()==false)
+ 1
Okay I think I got it now thanks!👍
+ 1
!x is not python. not is used in place of exclamation point.
0
It is preferable to use in each boolean logic.
For example:
while (!queue.empty())
queue.pop();
0
I haven't learnt while statements yet...Anyone have a link to explain that too?😫
0
Thanks for all the help guys
0
James
It is described in most of the curses on SoloLearn 😉
0
Okay, Okay, Hold on. I still don't understand! Why would someone use not in layman's terms for coding?!
0
Why not just use if or elif😫
0
Let's try in another way:
You have an Boolean variable 'x' . You want to check it it is set to False, then:
if (!x)
print "It is false"
And you should not use:
if(x==false)
print "It is false"
0
why the exclamation?
0
The exclamation is Boolean NOT. Because we should avoid writing false/true in if statements.
0
Okay, I think I understand now. So we use NOT instead because it prevents bugs in is better in statements/code?
0
SlickBack I thought that too!