Statement, Indentation, and Comments in Python.

Statements: statements are start with a keyword and end with semicolon (;). statements are executed by Python interpreter, for example: a=1; is an assignment operator.
There are other kind of statements if, for, while etc.

Multi-line statement: statements can be extend over multiple lines with

  • line continuation character (\)
  • example:
    x = 1+2+3+4+\
    5+6+7+8+\
    9+10;

  • line continuation can be implied inside parentheses(), brackets[], and braces{ }.
  • example:
    y = (1+2+3+4+
    5+6+7+8);

    z = [1+2+3+4];

    i = [‘Red’,
    ‘Yellow’,
    ‘Green’];

  • using semicolon(;), we can write multiple statements in a single line.
  • example:
    a = 1; b = 2; c = 3;

Indentation: Python use indentation to define a block of code like other languages c, C++ use braces{ }.
code block like a body of function, loop etc. starts with indentation and ends with the first unindented line. you can use as many indentations you want but they all should be consistent throughout the code block. generally four whitespaces are used for indentation and is preferred over tabs.

example:
for i in range(1,11):
print(i)
if i == 5:
break

Note: Use of indentation in python make the code look neat and clean. indentation can be ignored in line continuation but it’s good if you use indentation.
Incorrect indentation will result into IndentationError.

Comments: comments are important part of the program. it used to describe the program so that a person looking into the source code does not have hard time to figuring it out. comments also help you to understand your code also if you are looking for a key details of the program you just wrote in a month’s time.

  • Single-line comment: Use has (#) symbol at the starting of the statement to make it comment.
  • example:
    #This is a comment
    #print out Hello
    print(‘Hello’)

  • Multiple-line comment: Use triple quotes, either (‘ ‘ ‘ or ” ” “).
  • example:
    “””This is also a
    perfect example of
    multi-line comments”””

You may also like...

1 Response

  1. Dating Guide says:

    I spent four years trying EVERYTHING in Online Dating, and through a huge amount of trial and error, I produced a system that I will share for you. This book will take you, step by step, through everything you need to know to double, triple or even quadruple the number of women you meet online.

Leave a Reply

Your email address will not be published.