Statement, Indentation and Comment in Python

Statement, Indentation and Comment in Python


Statement, Indentation and Comment in Python

python,django,pycharm,anaconda python,opencv,matplotlib,python online,python programming,python list,learn python,python requests




Statement :

The written instructions in the source code by which the interpreter can read and execute. There are different types of statements are available for example, ‘i=2’ is an assign statement, ‘for in range’ is a looping statement, ‘while’ is an conditional statement and more.

Multi-line statement :  

Statements in Python can be extended to one or more lines using parentheses (), braces {}, square brackets [], semi-colon (;), continuation character slash (\). When the programmer needs to do long calculations and cannot fit his statements into one line, one can make use of these characters so the calculations can be continued.


Example :


Declared using Continuation Character (\):
s = 1 + 2 + 4 + \
    4 + 5 + 8 + \
    7 + 8 + 5

Declared using parentheses () :
n = (1 * 2 * 3 + 6 + 8 + 9)

Declared using square brackets [] :
footballer = ['MESSI',
                    'RONALDO',
                     'SUAREZ']

Declared using braces {} :
x = {3 + 2 + 3 + 9 + 5 + 6 +
     7 + 8 + 4}

Declared using semicolons(;) :
flag = 1; ropes = 8; pole = 9;


Indentation :


A block is a combination of all these statements. Block can be regarded as the grouping of statements for a specific purpose. In most of the programming languages like C, C++, Java use braces { } to define a block of code. One of the distinctive features of Python is its use of indentation to highlight the blocks of code. Whitespace is used for indentation in Python. All statements with the same distance to the right belong to the same block of code. If a block has to be more deeply nested, it is simply indented further to the right. You can understand it better by looking at the following lines of code as an example:


# python program showing indentation
  
site = 'cd'
 
if site == 'cd':
    print('Logging on to codedudle...')
else:
    print('retype the URL.')
print('All set !')

Output :

Logging on to codedudle...
All set !


The lines print(‘Logging on to codedudle…’) and print(‘retype the URL.’) are two separate code blocks. The two blocks of code in our example if-statement are both indented four spaces, in code. The final print(‘All set !’) is not indented, and so it does not belong to the else-block.


j = 1
while(j<= 6):
     print(j)
     j = j + 1



Output:

1
2
3
4
5
6

To indicate a block of code in Python, you must indent each line of the block by the same whitespace then it will be considered. The two lines of code in the while loop are both indented four spaces. It is required for indicating what block of code a statement belongs to. For example, j=1 and while(j<=6): is not indented, and so it is not within while block. So, Python code structures by indentation.


Comments :


This is the simple thing clearing by its name, we use comments to clearify the confusing to the developer. We use (#) to give a comment.

Example :

Code 1: 


# This is a comment

#printing the “codedudle”

Print(“codedudle”)



Code 2:

a, b = 1, 3 # Declaring two integers
sum = a + b # adding two integers
print(sum) # displaying the output



Multi-line string as comment : 

Python multi-line comment is a piece of text enclosed in a delimiter (""") on each end of the comment by the help of this you can write comment as many as you want, Again there should be no white space between delimiter ("""). They are useful when the comment text does not fit into one line; therefore needs to span across lines. Multi-line comments or paragraphs serve as documentation for others reading your code. See the following code snippet demonstrating multi-line comment:


Code 1:

"""
This would be a multiline comment in Python that
spans several lines and describes codedudle.
A Computer Science portal for coders. It contains 
well written, well thought 
and well-explained computer science 
and programming articles, 
quizzes and more. 
"""
print("codedudle ")

Code 2:


'''This article on codedudle gives you a 
perfect example of
multi-line comments'''
  
print("codedudle")






In this article i have briefly discussed about statement, indentation and comments in python.

Next article : Decision Making



#python
#django
#pycharm
#anaconda python
#opencv
#matplotlib
#python online
#python programming
#python list
#learn python
#python requests
#introduction to data science in python
#free python tutorial
#learn python basics
#python study
#python and ai
#python computer science
#learn python step by step
#best course to learn python
#learn python with projects
#python artificial intelligence tutorial
 #micropython
 #python for data science
 #data science from scratch
 #micropython esp32
 #machine learning using python
 #matplotlib
 #python 3
 #scikit learn
 #learn python for data analysis free

















Post a Comment

0 Comments