6. LINES AND INDENTATION, MULTIPLE LINE STATEMENTS, QUOTATION AND COMMENTS IN PYTHON

1. Lines and Indentation:

  • Python doesn't use braces({}) to indicate blocks of code for class and function definitions or flow control. Blocks of code are denoted by line indentation, which is rigidly enforced.
  • The number of spaces in the indentation is variable, but all statements within the block must be indented the same amount. 

For example −
if True:
     print ("True")
else:
    print ("False")

However, the following block generates an error −
if True:
      print ("Answer")
      print ("True")
else:
     print "(Answer")
 print ("False")

Thus, in Python all the continuous lines indented with same
number of spaces would form a block.

2. Multi Line Statements:


Statements in Python typically end with a new line. Python

does, however, allow the use of the line continuation character

(\) to denote that the line should continue. 


For example −
total = item_one + \
           item_two + \
           item_three
Statements contained within the [], {}, or () brackets do not need
to use the line continuation character. 

For example −
days = ['Monday', 'Tuesday', 'Wednesday',
             'Thursday', 'Friday']

Multiple Statements on a Single Line:


The semicolon ( ; ) allows multiple statements on the single line 
given that neither statement starts a new code block. 

Here is a sample snip using the semicolon −
import sys; x = 'foo'; sys.stdout.write(x + '\n')

Multiple Statement Groups as Suites:


A group of individual statements, which make a single code block are called suites in Python. Compound or complex statements, such as if, while, def, and class require a header line and a suite.

Header lines begin the statement (with the keyword) and terminate with a colon ( : ) and are followed by one or more lines which make up the suite.

For example −
if expression :
   suite
elif expression :
   suite
else :
   suite

3. Quotation in Python:

Python accepts single ('), double (") and triple (''' or """) 

quotes to denote string literals, as long as the same type of 

quote starts and ends the string.

The triple quotes are used to span the string across multiple lines. 


For example, all the following are legal −
1. word = 'word'
2. sentence = "This is a sentence."
3. paragraph = """This is a paragraph. It is
    made up of multiple lines and sentences."""

4. Comments in Python:

A hash sign (#) that is not inside a string literal begins a 

comment. All characters after the # and up to the end of the 

physical line are part of the comment and the Python interpreter 

ignores them.


# First comment
print ("Hello, Python!")  # second comment
This produces the following result −
Hello, Python!

You can type a comment on the same line after a statement or
expression −
name = “Hello World" # This is again comment

Python doesn't have multiple-line commenting feature. You 
should comment each line individually as follows −
# This is a comment.
# This is a comment, too.
# This is a comment, too.
# I said that already.

No comments:

Post a Comment