Hello and welcome to this lecture and this lecture you are going to learn about the BPP style guide
for Python.
So PPA is actually a style guide which stands for Python and Lanzmann proposal number eight and it's
actually a style guide on how to format your Python code.
So there are actually a few rules which we need to follow in this particular style guide so we will
go through each one of those rules one by one.
So without wasting any time.
Let's go ahead and learn about Rule number one.
So the rule number one states that use spaces instead of tabs for indentation.
So whenever you indent your code.
So let's basically go ahead and write in some code where we need to perform indentation.
So let's say you want to write a for loop so you type for x in range and then what you do is that you
simply specify some number were here and brackets and then when you hit enter as you could see you get
indentation here.
So if you go back and count the number of spaces over here you will see one two three and four.
So as you could see on the next line for indenting your code you use force basis over here and that's
the thing which you need to remember that you always need to use spaces instead of tabs.
So if you hit tabbouleh here as you could see still you have four spaces.
But the main issue with this is that your python interpreter actually interprets the spaces and tabs
in different fashion.
So always make sure that instead of using tabs you use one two three and four spaces.
And from this line onwards you could actually go ahead and print out whatever things you actually need.
So this is one of the rules which you need to follow while indenting your code.
That is you must always use spaces or you must always prefer for spaces instead of tabs.
Now if you use tabs that go to syntactically correct but that's not the efficient way to write Python
code.
Now the next rule in this P.B. style guide is that use force basis for each level of indentation.
So as you could see here we have performed a single level indentation.
Now let's say you have to perform multilevel indentation.
So what you could do is that you could go ahead and perform indentation again so you could type for
y in range and you could specify the range as 5.
And as you could see now we have the next level of indentation here.
And for each level of indentation you actually use force pieces as you could see this is level 1 we
have use for spaces.
This is Level 2 inundations right here.
And henceforth we use four more spaces.
So right now we have it's paces away here.
So this is another rule which you need to follow while indenting your Python code so often limitation.
Let's move on to the next rule that is every line of Python code should be 79 characters or less.
Now let's say you have some line of code over here so let me just go ahead and type and print and let's
say you have some line of code like let's say there is a simple line of code.
So if you actually notice or hear if you go down over here as you could see it says we actually have
the line and column number here.
So as you could see we have 39 characters in our line of code here.
And that's actually fine.
But in case if your line of code is pretty long that is let's see if it extends 79 characters then that
thing actually makes your code inefficient.
And it's not going to look clean.
So whenever you line of code actually extends 79 characters you should always make sure that you either
split your single line of code and do multiple lines or you actually make it a point to make it less
than so in the main character.
So moving on to the next rule and the fourth rule is the rule for functions and classes and the rule
for functions and classes actually states that you always need to have two blank lines after each function
or after each class.
So let's go ahead and try to define a function so let's typedef let's say display and let's see it says
something like.
Message.
And let's see we print the message on here.
So as you could see we have one one function over here and if you could notice this thing actually shows
you an error.
And if you would actually see is it expected to blank lines found.
So whenever you write a function you always need to make it a point that are both the function and below
the function.
You do need to leave two blank lines so as you could see right now you have only one blank line and
that's why we are getting a warning kind of thing over here because it does not follow the BP rule which
is leaving two blank lines before a function.
So if you again hit enter as you could see now we have two blank lines and the error actually disappeared.
So if you have to write the next set of function you always need to make sure that instead of leaving
a single line you do need to leave two lines away here and then type in the next function.
Let's say that's going to be right and then you could pass in whatever you want to pass over here.
So you could type in message and you could print the message as well.
So if you actually go ahead and reduce the number of lines to one as you could see it's again going
to pop out the same thing that is Abebe is expected to blank lines are required.
And I actually found one.
So make sure to follow this convention with functions and classes as well.
So if you're actually writing classes away here always make sure that those classes are separated by
two blank lines.
So let's move on to the next rule which is that the method should be separated by one blank line.
So this was the case for functions that is you always need two separate functions with two lines but
in case of methods inside a class the method should be separated by a single line.
So let's create a class first let's call it as pointer and let's create a simple method here.
So that's going to be deaf.
Let's see.
Display and in here let's print out something like let's see high and let's also go ahead and let's
define another method here which is to.
And let's go ahead and print something out as well.
So that's going to be let's say hello.
So as you could see this thing actually gives us an error because if you have oh it says BP it expected
one blank line.
So you should always leave one blank line between two methods so if you had to as you could see the
error disappear.
And this is the thing which you do need to keep in mind that whenever you actually go ahead and define
a method inside a particular class you should always have a single blank line and between those two
methods.
So now let's move on to the next rule which is to only include one space before and after variable assignment.
So let's say you want to assign a particular variable.
So let me just go ahead and delete this quickly.
So let's say you have a particular variable x and you do need to assign a value of let's say 10.
So this is actually a correct way to do so.
That is you type in X leave a single space type equals a single space and type the number which you
want to assign.
Now sometimes what people do is that they directly read X with 10 or we're here or let's say another
variable like y equals 12.
And this is actually not the correct way to do so.
Or do you know if you actually leave two spaces away here still that does not fit into the P.B. format.
So you always need to make sure that you leave only one space before and after the variable assignment.
Now let's move on to the next rule or the last rule for this lecture.
That is you should not leave spaces around keyword arguments.
So what mistake many people commit is that they actually keep this thing in mind that they have to leave
spaces when you basically go ahead and assign a value and they actually go ahead and apply the same
rule while calling the function as well.
While defining the function as well.
So let's say if you have a function like.
Let's see.
Right.
And here you could type in message.
Equals.
And what they actually do is that they actually leave the space here as well and they and then they
will type in Hello.
So this is actually not the correct way or not the proper way to write clean code because a few know
it as you could see it says unexpected spaces around keyword slash parameters.
So in order to resolve that you always need to remember like when you write such kind of assignment
and that when you actually go ahead and pass around keyword arguments to functions you always need to
make sure that you don't pass any kind of spaces on here.
So that's it for this lecture and I hope you guys were able to understand the rules discussed in this
lecture.
So if you have trouble understanding any rule then feel free to ask me in the queue in N.S. and I will
be there to help you out.
So in the next lecture we will be learning a few more rules about how to write clean and effective Python
code.
So thank you very much for watching and I'll see you guys next time.
Thank you.