DEV Community

Aravinthan
Aravinthan

Posted on

Python: Indentation, Comments

Hi All, Today i started to learn python.

INDENTATION

  1. Python uses indentation to indicate the block of code.
  2. Space is use to specify the indentation in the beginning of code.
  3. Most common indentation use four, and has to be use at least one space.(Don't use rest number of spaces it will generate syntax error)
if 10>1
    print("Ten is greater than one")
Enter fullscreen mode Exit fullscreen mode

Should be use same no. of spaces in the same block of code otherwise will generate error.

if 10>1
 print("Ten is greater than one")
    print("Ten is lower than one")
Enter fullscreen mode Exit fullscreen mode

The spaces use different no. of counts in two diff block of code.

if 5 > 2
 print("Five is greater than two!") 
if 5 > 2
    print("Five is greater than two!") 
Enter fullscreen mode Exit fullscreen mode

COMMENT

Single Line Comment

  1. Comment start with # in the code. where we indicate the # in the line of code the line should be ignored to read.
  2. If we use # in the end of line and python wouldn't read rest of the line in code.
#print("Hello Python")
print("Hello Python")
Enter fullscreen mode Exit fullscreen mode

Multi Line Comment
Since Python not have multi line comment. We will use # with each line which all we want ignore to read python.

#This is a comment
#written in
#just more than one line
print("Hello, World!")
Enter fullscreen mode Exit fullscreen mode

And as long as the string not assigned to a variable, python will read the line but then ignore it. We use triple quotes to use multi line comments.

"""
This is a comment
written in
just more than one line
"""
print("Hello, World!")
Enter fullscreen mode Exit fullscreen mode

Top comments (0)