[no audio]
Hi friends, here
we are going to discuss about indentation in Python.
Right. Let me start that.
First of all, what is an indent?
See indent is nothing but a space.
So guys, if you know some other languages like C, C++, Java,
or some shell scripts, right,
let's say suppose C language,
so in C language to represent some block of code
we are using curly braces. Now, the lines which are there between
these two curly braces are a block. The lines which are there
between these two curly braces,
this one and this one, is one block. Now to represent some set
of lines as a separate code, as a separate block of code
we are using curly braces there.
But in your Python you don't have something like curly braces
to represent a block of code.
Now instead of that
we are using here indentations.
We are using some space to represent a block of code, right.
So indentations are used to represent a block of code.
[no audio]
Right. Let me give you simple example.
So as of now, I don't know about Python, but just observe
the some code.
Let me write simple code.
[no audio]
So here I'm opening a program, a script called
'indentations_usage.py', right. Very first line
I am writing simply 'print("This is about indentations")'. Right,
simply you are printing some string.
Now what I am doing is 'if 3 > 1 then',
I want to execute some two lines - 'print(
"using if condition"), 'print("we are checking").
[no audio]
"we are comparing
[no audio]
3 and 1")', that's it.
Now guys see that
I am giving some space for these two lines.
Now, these two lines are a block of code.
These two lines are a block of code, and this block of code
is under this line.
So after this line I am giving some space, equal space for
all lines. Then these lines, even you can write 100 lines
also with equal space. Now all 100 lines are there under this line.
Because after this line only we are giving spaces,
that's why all these lines are there under this line, whatever
it may be. As of now
we don't know about your Python, right. Just observe that.
[no audio]
Now these two lines are called block of code,
and this line is there under this line.
Now, let me save it and run this.
[no audio]
Oh, sorry.
[no audio]
Right. Now I am running this.
Okay. Simply 'python',
your script name.
Right. You're getting some output, right.
Just see once again your code.
[no audio]
Now, this is called indentation, right. Now,
the amount of indentation is up to you,
but it must be consistent throughout that block. See that.
I have given same space for first line and second line. In
case by mistake if I give some extra space or some less
space, right, and try to run your script, and see the output
what you are getting.
[no audio]
'IndentationError: unexpected indent'. Unnecessarily you're
providing some space, right?
So guys whenever if you want to provide a space, right,
so before that line you should have some condition, or some
function name, something like that.
As of now we don't know that's why just assume that for each and
every line you should have same space. Unnecessarily don't provide space.
right, not only here. Now,
let me recheck it. First,
[no audio]
I'm running. It's working.
Now, what I am doing is for very first line
I am going to provide a space. Very first line is some 'print'
statement. For that
I'm giving some space.
If I give some space, and if I run, observe that
you are getting an error like again, 'IndentationError: unexpected indent'
Unnecessarily you're providing space for line #1.
[no audio]
Right. So to provide some space before starting your line, right,
before that line
you should have some logic. That logic like, with 'if' condition,
or some function name, or some class name, something like that.
[no audio]
Right. Let me open your script.
Very first line I am
starting. If it is very first line, first of all you should
not provide any space.
Let me delete that space. Even
[no audio]
let me do one thing.
Now forget about your block of code. Now, I'm writing something
here, 'print'.
[no audio]
"we are okay with indentations".
[no audio]
Now for this 'print' statement, right, if I write some space
then your Python won't accept. In case if this line is under this line,
I mean under this block of code, then for this line
you have to provide same length of space, but we are providing
only one space.
In case if this line is also under this block, then you have
to provide same space here.
But my assumption is there is no relation between this block
of code and this line, and this is I am assuming this is a separate line.
If this is a separate line, you should not provide a space first.
If you want to provide a space, before this line you
have to write again something, like some condition name, some
condition, or some function name, or some class name.
[no audio]
Right. So as of now, we don't have anything like condition, function,
and classes. Don't provide a space. That's it.
So simply guys as a learner don't provide any space before
starting your line.
Okay. That is nothing but simply about your indentations
of your Python.
[no audio]
Right. That's it. Now it's working perfectly.
[no audio]
So guys, final point just remember that this is very important,
don't include an indent unnecessarily.
Right. Okay.
Okay. Thank you for watching this video.
[no audio]