Statement, indentation, and comment in Python

Multi-line statements: statements in Python can be expanded to one or more lines using brackets (), curly braces {}, square brackets [], semicolon (;), forward slash (/). When a programmer needs to perform long calculations and cannot fit his statements on one line, these characters can be used. 

 Declared using Continuation Character (): s = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 Declared using parentheses ( ): n = (1 * 2 * 3 + 7 + 8 + 9) Declared using square brackets []: footballer = [`MESSI`,` NEYMAR`, `SUAREZ`] Declared using braces {}: x = {1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9} Declared using semicolons (;): flag = 2; ropes = 3; pole = 4 


Block — it is a combination of all these operators. A block can be thought of as a grouping of statements for a specific purpose. Most programming languages ​​such as C, C ++, Java use curly braces {} to define a block of code. One of the great things about Python is the use of indentation to highlight blocks of code. Space is used for indentation in Python. All operators with the same distance to the right belong to the same block of code. If the block needs to be more deeply nested, it just backs off to the right. You can understand this better by looking at the following lines of code:

# Show Python program
# indent


site = `gfg`


if site = = `gfg` :

  print ( `Logging on to pythonengineering ...` )

else :

pr int ( `retype the URL.` )

print ( `All set!` )

Exit :

 Logging on to pythonengineering ... All set! 

The lines print ("Logging into pythonengineering ...") and print ("retype the URL.") — these are two separate blocks of code. The two blocks of code in our example if statement are indented four times. The final output (& # 39; All set! & # 39;) is not indented and therefore does not belong to the else block.

Exit :

 1 2 3 4 5 

To specify a block of code in Python , you must indent each line of the block with the same space. Both lines of code in the while loop are indented four times. This is necessary to indicate which block of code the statement belongs to. For example, j = 1 and while (j & lt; = 5): are not indented and therefore not in the while block. So, the structure of Python code is indented.


Python developers often use the comment system, as without it, things can get very confusing, very fast. Comments — it is useful information that developers provide the reader with in order to understand the source code. This explains the logic or part of it used in the code. Comments are usually useful for those who maintain or improve your code when you are no longer around to answer questions about it. They are often referred to as a useful programming convention that does not contribute to the output of a program, but improves the readability of the entire program. There are two types of comments in Python:
Single-line comments: A single-line comment Python starts with a hashtag character without spaces (#) and continues to the end of the line. If the comment is longer than one line, put the hashtag on the next line and continue with the comment. Python one-line comments have proven useful for providing concise explanations of variables, function declarations, and expressions. See the following code snippet demonstrating a one-line comment:

Code 1:

j = 1

while (j & lt; = 5 ):

print (j)

j = j + 1

# This is a comment
# Output "GeeksforGeeks!" To the console

print ( "GeeksforGeeks" )

Code 2:

a, b = 1 , 3 # Declaring two integers

sum = a + b # add two integers

print ( sum ) # display output

Multi-line line as comment: multi-line comment Python — it is a piece of text, enclosed in the () delimiter at each end of the comment. Again, there should be no spaces between the () separator. They are useful when the comment text does not fit on one line; therefore it is necessary to cover the lines. Multi-line comments or paragraphs serve as documentation for others reading your code. See the following code snippet that demonstrates a multiline comment:

Code 1:

"" "
This will be a multi-line comment in Python that
spans multiple lines and describes pythonengineering.
Portal of informatics for geeks. It contains
well written, well thought out
and well explained computer science
and programming articles,
quizzes and more.
"" "

print ( "GeeksForGeeks" )

Code 2:

& # 39; & # 39; & # 39; This pythonengineering article gives you
a great example
multi-line comments & # 39; & # 39; & # 39;


print ( "GeeksForGeeks" )

Get Solution for free from DataCamp guru