Statement, Indentation and Comment in Python

Statements for data science using python:

Statements are the instructions which are written for the execution of a program in the source code. It can be of many different types in the Python language which we’ll be learning. Some examples are Assignment statement, looping statement, Conditional statement, and so on. These statements help us to get the desired output.python for data science

Multi-Line Statements:

While doing lengthy calculations, one cannot fit the statements in one line; these come in handy in such cases. Parenthesis (), semi-colon ;,square bracket [], braces {} and continuation character slash (\) are all use to generate multi-line statements.

Example

Declare it using parentheses () :

n = (4 * 5 * 6 + 7 + 8 + 9)

Declare it using semicolons(;) :

kings = 2; pawns = 16; horses = 4

Declare it using square brackets [] :

cricketer = [‘DHONI’,

‘YUVRAJ’,

‘SACHIN’]

Declare it using braces {} :

x = {1 + 2 + 3 + 4 + 5 + 6 +

7 + 8 + 9}

Continuation Character (\):

s = 1 + 2 + 3 + \

4 + 5 + 6 + \

7 + 8 + 9

Indentation:

A combination of all statements is known as a block. Grouping of purpose in order to fulfill a specific purpose is seen as a block. So, In C, C++ and Java braces{} are use to define a block of code. Python is unique in nature, and one of those unique characteristics is the use of indentation for the purpose of highlighting the blocks of code. Whitespace does the job of indentation in Python. Statements with the same distance to the right are consider to belong to the same block of code. So, if we need to nest the block more deeply, we simply need to indent it more to the right. The examples below will make the concepts clear.

# Python program for indentation

site = ‘PST’

if site == ‘PST’:

print(‘Logging on to pstanalytics’)

else:

print(‘retype the URL.’)

print(‘All set !’)

Output:

Logging on to pstanalytics

All set !

The output above point out that the else statement print(‘retype the URL.’) was of four indents to the right which made it a separate block but in case of print(‘All set !’) no such thing was done which did not change its block.

j = 1

while(j<= 5):

print(j)

j = j + 1

Output:

1

2

3

4

5

Indentation by the same whitespace is there to indicate a block of code in Python. The two lines in the while loop are indented which is there to indicate the block of code it belongs to. Python code structures with the help of indentation.

Comments:

These do not take part in the output of the program but helps the users understand the programs written in a better way. Without comments, programming can be very confusing. Comments are use by developers to provide information on the source code use by them. There are two types of comments data science using python.

Single line comments: These start with the first character being a hashtag, and it ends in the same line. If we need to continue to the next line then before every line a hashtag is required. It is useful to provide short information explaining variables, expressions, and function declarations. Below is an example that will make the whole concept clear.

# This is a comment

# Print “PST Analytics”

print(“PST Analytics “)

a, b = 1, 6 # Declaring two integers

sum = a + b # adding two integers

print(sum) # displaying the output

Multi-line string as comment: When a delimiter (“””) is present on each side of a comment it is known as the multi-line comment. So, no white space is allowed between delimiters. Useful when the comment does not fit into a single line. Example of a multi-line comment is given below.

“””

A multi-line comment spans across

more than one line and denoted

by delimiters. It helps the reader to

understand the source code well.

“””

print(“Multi-line comment”)

”’This article on PST Analytics gives you a

perfect example of

multi-line comments”’

print(“PST Analytics”)

To learn more, go to this.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.