Statement, Indentation And Comment in Python - Programmer's Academy

Statement, Indentation And Comment in Python

Statements

In programming statements are set of instructions. They are often goal or task oriented. For example, x=10 is a statement. In python programming there are many types of statement such as assignment(x-5), conditional and loop statement such as for, while and do while. There can be multi-line statements in python programming. They can be declared as using backslash at the end of each line.

There are several other ways to write multi-line functions such as

  • Parentheses

  • Square bracket

  • Semicolons

Indentation

Indentation is a vital concept in python as compared to other languages. This is because in python language indentation of a block (i.e., set of statements) defines its scope. Other languages use braces to declare or define the scope of a block. So, in python language each line of block should start at the same level. Whitespace is used for indentation in python. To ident a block in python we must use same whitespaces throughout the whole block. If a nested block needs to be used, then indent that further with more whitespaces. Indentation also improves code readability and provides a clean look, which are essential part of good programming practices. It also improves traceability of error. Let us understand indentation through an example

With Indentation
Without Indentation
Try it yourself with add function that adds two numbers and prints them!

Comments

Comments are written by developers so that the function performed could be understood easily later. For example, you multiplied two numbers in your code, so you write that here I multiplied some numbers for some other function I want to perform just to remind yourself when you open your code again. Comments are optional but an important part and good programming practice. How to write comments in python. Any sentence followed by a “#” is comment. Comments do not affect program flow as they are ignored by compiler. Comments play an important part while maintenance and reuse of code. It also helps trace error in code. There are two types of comments.

Single line comments:

Single line comments are written followed by a # symbol without space. If it exceeds that line, insert # on the other line and so on

Single-line comment
Single-line comment
Multiple line comments

There can be some explanation of certain functions that exceeds more than 2,3 line. In python for multi-line comments just add “”” at start and end of comment. Let us check through example.

Multi-line Comment
References
  • https://docs.python.org/3/tutorial/index.html
  • https://www.amazon.in/dp/1449357016?tag=guru99-21&geniuslink=true
  • https://en.wikipedia.org/wiki/Python_(programming_language)#Indentation

Help Me Please: If you found any error in the content or example please let us know by comment also write your suggestion or if any doubt in the concept please feel free to write us in the comment section.

Practice your Code online here

(Visited 81 times, 1 visits today)

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
//poosoahe.com/4/4219743
You cannot copy content of this page
Scroll Up
0
Would love your thoughts, please comment.x
()
x

  • Name

  • Minimum length of 8 characters.