This video explains how to construct "if not" logic in python and explores what happens behind the scenes
The article explaining the concepts covered in this video can be found here
https://embeddedinventor.com/python-if-not-explained-with-examples/
Show More Show Less View Video Transcript
0:00
Hello everyone, in this video let's learn how to implement the if not logic in Python
0:06
Consider a situation. Let's say if a condition is not met, then you wish to execute some code
0:15
You need to remember that all conditions or expressions will evaluate to either true or
0:22
false let's take an example let me head over to the spider ID let's take a
0:32
simple boolean variable here and initialize that to false okay and let's
0:43
invoke the if statement using that boolean variable and see if the code inside the if statement will execute or not so inside
1:10
the if statement I have something like this just the print statement so what do
1:17
you think will happen if I press enter yep the line in inside the if statement
1:25
got executed and we got this line printed on the screen so let's get back
1:32
to the slide so the syntax for this particular scenario will look like this
1:38
if condition is equal to false then you do your cool stuff here inside the if
1:46
statement. Okay next let's look at another common scenario. Say you want to
1:51
compare two objects for inequality The syntax to do that will be as shown here if object one exclamation equals object to this operator stands for not equal to then whatever if this
2:08
condition is true then whatever you write here instead the if block will get
2:12
executed again head over to the spider ID and let's see some examples let's say
2:20
equals 5 and y equals 10 then if x is not equal to y we print say the x is not
2:41
equal to y if you run this code of course we get this so the important thing to
2:52
note here is this operator this not equal to operator works as expected with
3:01
two integer objects here X and Y or integer objects as they store integer
3:06
values now let's take two string values let's say a equals apple and b equals
3:19
ball then what happens if is not equal to B
3:32
let's print so yes the Python interpreter used the same operator this time to compare two strings instead of two integers in our previous example so how did the Python interpreter use the exact same
4:07
operator to compare different types of data let's take a third example this
4:14
time let's take a list maybe so let's say list one is one two one doesn't matter
4:28
this is just an example list 2 is say A B and let's have a list 3 just for fun and
4:46
let's keep that also one two one so if I check inequality for list one and list
5:00
two we get true as expected they are not equal let's say I do list one and list
5:10
three this time and you get false just because they are equal so how did Python
5:20
interpreter know how to use this sign the same way as we would expect it to for
5:29
integers for strings and for lists the answer to that question is all the
5:38
inbuilt objects in Python come with some special functions or methods the method
5:48
that responsible for this particular not equal to operator is this one double underscore in E double underscore so we can use this method to
6:07
get the same result as we would use this operator so basically what Python does
6:19
whenever we use this operator python will call this particular method and pass these values to
6:28
this method and execute the code and the implementation of this particular any method
6:38
stands for not equal to this will be different for different types of object so the implementation
6:46
for an integer object will not be the same for implementation of the string
6:51
object and it will not be this same for in implementation of this any method for
6:57
this least list object so I hope that clears the point of how Python takes
7:08
these operators and does its magic on different kinds of data. Alright let's
7:18
get back to that presentation. I want to go and check out this article in the
7:23
description where I have added some more explanations on how the inbuilt objects
7:28
obey this not equal to operator as we intuitively expect them to. Also I've
7:34
explained how we can make sure that the classes that we define ourselves in our
7:39
projects can be made to obey this not equal to operator okay I hope you enjoyed
7:47
the video if you like the video give it a like if you haven't subscribed already
7:51
consider subscribing and I'll see you in the next one bye
#Programming
#Software
#Scripting Languages
