Wednesday, 20 September 2017

3)Python Statement, Indentation ,Comments and DocString

Python Statement, Indentation ,Comments and DocString

Python Statements:
Instructions that a Python interpreter can execute are called statements.

Example:
a=10
if statement, for statement, while statement etc.
Two types of Python Statements:

  1. Single line statement
  2. Multi-line statement,

1.Single line statement:
Instructions are end with in a line

Example:
a=10
2.Multi-line statement:
Multi-line statement extend over multiple lines with the line continuation character (\).

Two types of continuations:
Explicit continuation ==> using \ character
Implicit continuation ==> using brackets [] or braces {} 

Example:



Note:
We can also put multiple statements in a single line using semicolons,

Example:
a=20; b=10; c=30
Python Indentation:

Most of the programming languages like C, C++, Java use braces { } to define a block of code. But Python uses indentation 

Note:
A code block (body of a function, loop etc.) starts with indentation and ends with the first unindented line.

Indetation ==> Whitespaces , prefered is tabs, we can use any amount of space, but should be consistent
Incorrect indentation will result into IndentationError


Python Comments:

Comments are very important while writing a program.

Note:

  • It describes what's going on inside a program so that a person looking at the source code does not have a hard time figuring it out
  • Basically, helpful to give the program logic for understandability


Hash (#) symbol to start writing a comment.

Two types of comments:

  1. Single line comments ==> using #
  2. Multi-line comments ==> using either ''' ''' or """ """

Docstring in Python:

Docstring is short for documentation string.

Note:
It is a string that occurs as the first statement in a module, function, class, or method definition. We must write what a function/class does in the docstring.

Syntax:

Within tuples quotes
""" """ 

Docstring is available to us as the attribute __doc__ of the function.



Previous page                                                                                                         Next page

3 comments:

Fundamentals of Python programming

Fundamentals of Python programming: Following below are the fundamental constructs of Python programming: Python Data types Python...