2.4 - Comments, Indentation and Statements - mp4

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Now let's learn about comments in Python.

As some of you might be already aware of,


Python comments in any programming language are basically statements like this, right?
Which are not interpreted by a compiler or your interpreters, so they're not executed, if I
have to put it that simply. And they're typically used to write exploratory pieces of your
code to, to make your code more readable by humans. For example here, you could say this
is print hello world to console. So it's saying what this line of code below that is doing. And
one of the simplest ways to write comments in Python is to start the line with a hash with
this symbol, which is right above your three key on your keyboard, right? So that's the
simplest way to write a single line comment. Of course, if you want to write multiline
comments, here is how you can write it. Of course you can write a multiline comment like
this with hash starting on each line. Or a better way to write it is to actually start your
comments with your triple quotes. We saw the triple quotes are used typically to represent
your strings, right? Multiline strings. So you can actually start with triple quotes, and this
will be regarded as a multiline comment. Of course you can start either single, you can use
either single quotes or double quotes. Both of them will work equally well. Now, unlike
many programming, for example in C, if you recall, how do you put a block of code? How do
you write a block of code? For example, if I'm writing a for loop in C, I typically might write
int I equals to ten, I less than ten, sorry, I less than 100, I plus plus. And I typically put all of
this code in your curly braces, right? So this is a block of code. This is how I define that all of
this piece of code, whatever I write here, will be executed whenever this for will be looped
through using your for condition, using your for loop, right? So what happens is, unlike your
C, C plus plus or java, Python actually has some integration, which is slightly unique. What it
does is. So if this is my for loop. So this is a simple for loop, what it says is for I in range ten.
So we'll look at loops in much more detail later. But what this does is basically, it goes from,
basically this is equal to saying for I equals to zero, I less than ten. This is what it literally
means, because when you print it, you'll see that, right? But you don't see your curly braces.
You don't notice your C program or Java's curly braces here, because what Python uses is an
indentation of this sort where if this is my code flow, if this is the line on which my code is
flowing, if I have some gap here, it assumes that till the time the gap is there, it is all part of
my for loop. So it uses, typically it's a good habit to use four spaces for indentation, right?
Some people also use a tab, but it's recommended to use four spaces because tabs could be
interpreted slightly differently in various text editors. Right? So for example, if you look at a
code like this, okay, for example, for example, if I write a line like this, let me just edit this
code so as to make it interesting, right? So I just said, okay, print I in one and one to range.
Now, if I say, if I just go back here, and if I say print 100, right? So this print 100 here is not
part of my for loop because it's at a different, it says indentation of for. So everything here.
For example, if I change this to print, I multiplied by two, right? So I get my, I get my print I
and print I equal print. So both these lines. So. 001-2436. So this whole piece of code that is
highlighted here is part of my for loop because it is indented at the same level inside the for
loop. But my print hundred is not in my for loop. That's important to notice. What if instead
of four spaces, I have one space? Right, so, sorry, I think I mistakenly used a tab here. It's
always good to use 1234, right? So I could say 1234. Right? Then the question is, what if I
put a single space? So this line now is neither at the for loop nor at the print statements,
right? Then what happens? It simply raises an error. It says you have not indented it
properly. It's called an indentation error. And your ipython is smart enough to realize that
there is an indentation problem and it highlights it in red as you type, right? But you can get
rid of this. You can get rid of this problem if you just make this print 100 line start with your
for or if you just make it go inside your for loop, right? So this is a very, very useful way. Let
me explain you why. Because this makes your code readable. It makes your code look
beautiful and interesting to read. Let me show you an example. For example, this is how
your if conditional statement looks like in Python. I promise you that we'll look at the if else
conditions in more detail. But there are two ways of writing this. If statement. I can say if
true, print this. Of course, there are four spaces here, which means both of them will be
executed, if true, of course, and hence these two will be executed. I could have written the
same thing in just one line like this. And these two statement. These two statements now
are delimited using a column. When you have a column like this, what it says is that these
two statements need to be executed one after other, and it's there on the same line. But this
is not readable. If you look at readability or the cleanliness of code, or how easy is it to just
flow through your code, this is much less readable. Of course, they both are doing exactly
the same thing. But remember, code readability is as important as code correctness,
because when you write code, maybe 100 other people may read your code later, and your
code should be in a state that people can easily understand what's happening. The next
concept is statements. So statements are basically commands that your python interpreter
will execute. Okay, so for example, what does this do? It basically assigns a value of one to a
variable a. This is a simple statement. Let's look at slightly more variations of statements. So
this is called a multiline statement, because your whole statement, your one plus two plus
three plus four plus five plus six plus seven plus eight is split over multiple lines. So when
you're splitting your whole statement across multiple lines, it is mandatory that you use this
backslash symbol at the end of the line, telling the interpreter that, okay, what comes in the
next line is actually part of the same line, right. So when you execute it, you'll get print a,
you'll get 36, which is the sum of all the numbers from all the numbers from one to eight. It
just works like magic. The other way to use to do multiline, to do multiline statements,
instead of using a backslash, if you put everything in your parenthesis like this, the moment
you open a parenthesis like this, or a small braces, it understands that till time the braces
are closed, everything is part of the same sentence, same statement, sorry. And everything
will work like a charm. The third type of statements that we will see is you can actually have
multiple statements on the same line. In this case, we had a single statement spanning
across multiple lines. We could have multiple statements in a single line, probably for
brevity or for shortness, if you want to write it. If you want to write short, quick snippet of
code, this is how you can write it. Of course. Here, ten is assigned to A, 20 is assigned to B,
and 30 is assigned to C. The most important thing is the semicolons if you notice, we haven't
put semicolons anywhere else. But if you don't put a semicolon here, it'll raise an error. It'll
raise an exception. Okay? As long as you put your semicolon, it knows that. Okay, this is one
statement, this is another statement, and this is another statement. So the delimiter
between multiple statements in a single line is nothing but your semicolon.

You might also like