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)

8th Apr 2018, 9:49 PM
Ayanna
Ayanna - avatar
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.
8th Apr 2018, 11:33 PM
Bartosz Pieszko
Bartosz Pieszko - avatar
+ 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 '!=' ...
8th Apr 2018, 10:42 PM
Bartosz Pieszko
Bartosz Pieszko - avatar
+ 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.
9th Apr 2018, 12:58 AM
James
James - avatar
+ 1
I'm sorry I don't understand, will guys explain in more detail please?
8th Apr 2018, 10:12 PM
Ayanna
Ayanna - avatar
+ 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
8th Apr 2018, 10:18 PM
Markus Kaleton
Markus Kaleton - avatar
+ 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)
8th Apr 2018, 10:36 PM
Bartosz Pieszko
Bartosz Pieszko - avatar
+ 1
Okay I think I got it now thanks!👍
8th Apr 2018, 11:41 PM
Ayanna
Ayanna - avatar
+ 1
!x is not python. not is used in place of exclamation point.
9th Apr 2018, 12:52 AM
James
James - avatar
0
It is preferable to use in each boolean logic. For example: while (!queue.empty()) queue.pop();
8th Apr 2018, 10:00 PM
Bartosz Pieszko
Bartosz Pieszko - avatar
0
I haven't learnt while statements yet...Anyone have a link to explain that too?😫
8th Apr 2018, 10:50 PM
Ayanna
Ayanna - avatar
0
Thanks for all the help guys
8th Apr 2018, 10:51 PM
Ayanna
Ayanna - avatar
0
James It is described in most of the curses on SoloLearn 😉
8th Apr 2018, 10:53 PM
Bartosz Pieszko
Bartosz Pieszko - avatar
0
Okay, Okay, Hold on. I still don't understand! Why would someone use not in layman's terms for coding?!
8th Apr 2018, 11:04 PM
Ayanna
Ayanna - avatar
0
Why not just use if or elif😫
8th Apr 2018, 11:06 PM
Ayanna
Ayanna - avatar
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"
8th Apr 2018, 11:11 PM
Bartosz Pieszko
Bartosz Pieszko - avatar
0
why the exclamation?
8th Apr 2018, 11:17 PM
Ayanna
Ayanna - avatar
0
The exclamation is Boolean NOT. Because we should avoid writing false/true in if statements.
8th Apr 2018, 11:19 PM
Bartosz Pieszko
Bartosz Pieszko - avatar
0
Okay, I think I understand now. So we use NOT instead because it prevents bugs in is better in statements/code?
8th Apr 2018, 11:26 PM
Ayanna
Ayanna - avatar
0
SlickBack I thought that too!
9th Apr 2018, 12:53 AM
Ayanna
Ayanna - avatar