Hello and welcome to this lecture and this lecture you are again going to learn two more important rules
for writing clean and efficient Python code.
So the first rule is going to be the rule about import statements.
So whenever you actually you want to import certain Amandi words from certain packages you always need
to remember these rules.
So there are actually two important rules.
The first one is quite simple and the rules are just that you always need to write your import statements
at the top of the fight.
Now we always follow these rules most of the times.
But in some cases the import statements might be included in between some of the code and that's actually
not a good practice to import any code.
So let's say if you want to import certain packets you always need to import it at the top of your code
and the logical reason behind importing things at the top of your file is because of an other person
or when your coworker actually refers your code would be able to understand which particular modules
or which particular packages or which particular libraries you have used in your code.
But for some reason if you import those packages all modules or libraries in between your code and then
it becomes absolutely impossible for a person or it becomes absolutely difficult for a person to determine
which particular libraries modules or packages you have used.
So that's the logical explanation for why you should always include imports on the top of your code.
And let's also discuss one more important thing about importing a module from a package.
So let's say you have a Python package and as you all know the package actually contains a particular
module.
Let's say you have a module and a package.
So I am simply going to pick an example of here this is going to be a hypothetical scenario.
So let's say the package name is in this case X and let's say the module name is WHY.
So what you essentially do is that while performing an import on this you actually import more of your
life from the package.
Ex but what a lot of people actually do is that they that actually bought the model twice so simply
the type been import.
Why.
And this is actually not a proper way to do so.
So in this case what you have to do is that instead of directly importing this thing you have to explain
that you actually import this particular Mondial from a particular package.
So first you need to type from then you have to define the package name which is in this case X and
then you have to define import Y.
And the logical reason behind this thing is that what happens sometimes is that when you import a particular
module there are certain packages which have the same model name.
So your interpreter in that case is going to get confused that from which particular package it has
to import the certain y module.
Because a lot of packages might contain the same module and that is going to be a cause of confusion.
So always make sure that you always specify the package name that is you import certain module from
or put the package name in your Python code.
So this is one of the things which you need to remember.
And now let's move on to the next rule which is how you should fall apart condition.
So for this pope as I'm actually going to take an example.
So let's say if you have to see if a particular name value is entered into the main variable.
So let's take an example let's say a name is going to be equal to John.
And what you could do here is that you could figure out and I guess we have left two spaces away here.
And in order to check if this thing actually has some content in it.
So let's assume that we don't have some kind of value is stored in the name variable.
So one way to check it is by checking the length of that particular variable.
So you could type Plen name and you could type this equal equal to zero.
And if the length is phone equal to zero then what you could do is that you could print out the value
you like let's say no name
font.
And actually if you have a look over here it seems redundant.
But anti-seize phone.
So this is also not a good practice to have it in parentheses.
And also you have to specify white spaces around the operator.
So here if you run this code now as you could see we're going to get the correct result because this
actually contains a name and henceforth the statement is not executor.
And if you remove this name from here and if you again run this code it will actually see that no name
phone.
So syntactically this code looks correct.
But what's wrong in this particular code is that you have unnecessarily used this double equal to zero
sign.
So in this condition what we are doing is that we are checking if the length of this name variable is
equal to zero.
And if this is equal to zero then we are actually going to execute the statement but this line of code
is unnecessary here because the if statement actually checks if the result of the statement is having
a value 0 or 1.
So that means if the if statements value is zero that means the if statement turns out to be false.
And this code is not going to be executed.
And if the if statement actually is positive that is if it matches the condition then the result of
this if statement is a non zero or one.
So one way to actually modified this code is that you could simply go ahead and remove the zero.
And what you could do is that you could also add in an IF statement or here so.
So this thing actually does is that the if statement is by default going to have two results that is
0 or 1.
So if this condition turns out to be true or if the result of this condition is zero that means the
condition is false.
So now if you actually go ahead and calculate the value for the length of the name it's actually going
to be zero.
So if you save this code and run it as it could see it actually exit you that this line of code.
But what we essentially want to do here is that if the length of this thing is zero that means we want
to make sure that we want to print this particular statement which is no name phone.
So what we could do here is that we could change this thing to name found and we could write a block
here.
To print out no name phone.
So Type A no name phone.
So by doing this what we have done here is that we have eliminated the use of writing a double equal
to sign away here.
So now if you run this code as you could see this thing has no name phone and if you actually specify
some name like John or we're here save the code and if you run this thing as you could see it says Name
Phone.
So always make sure that you never do certain kind of thing.
And you compare a result of the if statement with zero or one because by default the if is actually
doing the same job for you.
So you don't want to unnecessarily add one extra line of code over there.
So those are some rules about the import statement and the if statement.
So hopefully you guys were able to understand what I have tried to explain away here.
So thank you very much for watching and I'll see you guys next time.
Thank you.