What is indentation and how to comment in python?

here we are continuing our python article series at first we are learned how to download and install python, then we learn what is the general syntax and types of python programming, then we are here learn about python indentation which is the most required thing in python, without indentation you can’t do anything in python, and then we learn how to comment a single statement or block of code in python.

Let’s Start…

what is an indentation in python

here we learn what is indentation and how we do indentation in python, indentation means in an easy word, some blank space before the statement python use indentation to indicate a block of code same indentation statement are counted as one block of code.

in python when we use class, method, loops, condition statement each and everywhere in c and java we use curly braces like {} this but in python, we simply write Class name or method name and do colon (:) like below.

here we have to use this curly braces ( {} ) to indicate the starting and ending of particular blocks.

if (20 > 18) 
{
  System.out.println("20 is greater than 18");
}

Example Of Indentation

But in Python no need to do this simply use colon ( : ) like below.

if (20 > 18) :
  print("20 is greater than 18")

that’s called indentation means the print statement is a part of for loop so here we use 1 tab suppose we have multiple statements like below then we use the same space for all statements.

if (20 > 18) :
  print("20 is greater than 18")
  print("18 is lesser than 20")
  print("Your if is complete here")
else:
  print("18 is greater than 20")
  print("20 is lesser than 18")

In the above code, you exactly got what is indentation. Now if we have 3 statement which has the same indentation and also else have 2 statement they both have the same indentation so that’s mean here two blocks of code is here, when you have nested loop or nested conditional statement then at first we give 1 tab indentation and in nested part, we give two tab indentation like below.

if (a>20 and a<30):
  print("a is grater then 20 but less then 30)
  if (a>25 and a<30):
     print("a is grater then 25 but less then 30)

How to comment in Python :-

Comment is a useful part of the program. Comment tells what is going around in the program. If you use the comment in one of your codes and you want to give that code to any person, now that person checks that code after a long time then at that time that person can easily understand that code. Means, suppose I write code for some of two numbers then I comment sum of two numbers, when I reopen that code then I simply read the comment and then I know ya, that’s a code for some of two number.

There are two type of comments in python

  • Single-line comment
  • multi-line comment

Single-line comment

Whenever you want to give one single line as a comment that time you can use this single-line comment. You can use (#) to give a single line comment in your program. the first line of the program is as a comment I declared.

#sum of two number
a=10
b=20
c=a+b
print(c)

multi-line comment

whenever you want to give a more than one line comment then that time you can use triple-single cots ( ”’ ”’ ) or triple-double cots ( “”” “”” ) like below.

Using Single cots :-

''' hello you are reading a article
of python on smgplaza 
Thank You'''

Using Double cots :-

""" hello you are reading a article
of python on smgplaza 
Thank You """

Also visit this Articles :-

Also Read :- How to learn Python programming?

If you have any doubts any suggestion or any query DM us one below Social media handles.

Our Social Media Handles:-

Instagram Handle :- https://www.instagram.com/smgplaza/

Twitter Handle :- https://twitter.com/smgplaza

Thank you so much for your support you all are very valuable for us. You have to follow us using your mail id and also follow us on social media for more updates. We are one by one covering all topics of python and android both, if you have any query then you also comment below comment box.