Python Statement, Indentation and Comment

In this article, I will discuss about Basics of Python Programming Language explaining what’s meaning of Statement, Indentation and Comment in Python.
Let’s straightforward dive into meaning of each of these in Python.

Statements in Python

Statement in Python is just a line of code for doing some specific task. For example a = 10 is statement which performs task of assigning value of 10 to variable named a. Similar to this there does exist different types of statements in Python.

Different types of Statements in Python Programming Language

  • Assignment Statement
  • Conditional Statement
  • Looping Statement
  • Decleration Statement

Examples of Different types of Statements in Python

Let’s see some examples of different types of Statements in Python so as to better understand these.

  • Assignment Statement in Python Examples
    • a = 10
    • b = “Hello”
    • c = “Computer Science Hub”
  • Conditional Statement in Python Examples
    • if ( 10 > 4 )
    • switch (some_variable)
  • Loop Statement in Python Examples
    • for (int i = 0; i <= 8; i++)
    • while ( i > 10)
  • Decleration Statement in Python Examples
    • def function():

Indentation in Python

Indentation in Python is just a way for grouping together different statements. Indentation can be implemented either using tabs or whitespaces. If two consecutive statements in python code, are at same distance from left hand side of code editor then these two statements are in same code block.
This Indentation concept in Python is similar to braces {} which are used in other low level programming languages like C, C++ and Java.

def f():
	if (a = 10):
		print("This is ComputerScienceHub.io")
	else:
		print("I do not know which website it is")

In the code above, you can clearly see that different code statements are indented in a hierarchical way making the code look beautiful and simpler.
Let’s now see what are some rules in Python for properly indenting the code.

Python Indentation Rules

  • Indentation should be on one line only, meaning in Python code statements on different lines can be indented. But a single code statement which is larger enough to cover two lines should not be indented.
  • Usually Declaration Statements should not be indented, for example – def function(): code statement must not be indented other python would raise IndentationError.
  • Have 4 whitespaces for first indentation and for second add 8 whitespaces and increase as you further indent your code.

Some benefits of Python Indentation

  • Keeps the code cleaner and easy to understand
  • Helps Python Interpreter to better understand relationship between different code statements

Disadvantages of Python Indentation

  • If in python code some error occurs and the code is already not properly indented then it would be very difficult task to debug that code.

Comments in Python

Comments in any programming language are sort of a helping hand for explaining your code to others. Usually in the larger companies, comments are really crucial part of their code base. Because whenever someone leaved a job and another person comes in to do the job then comments can be really helpful for new employee to understand exactly the code base which have already been developed.
Anyway, let’s get back to meaning of Comments in Python Programming Langauge. Comments in Python can be either single liner or multi liner.

Different types of Comments in Python

  • Hashtag(#) – Putting hashtag symbol at the start of a line in Python Code makes the line a comment. These one liner comments can be useful for explaining little pieces of code, for example – writing some information about a variable or function declaration.
  • Multi-Line(“””) – Putting some text inside delimiters “”” some comment “”” makes a multi-liner comment. These comments can be useful for explaining the functionality of a function or some other group of code statements.

Examples of Comments in Python

Singler Line Comment

# Defining a variable a and assigning it value 10
a = 10

Multi Line Comment

"""
Function computer() takes in details of Memory, RAM and Hard disk
and return a computer object
"""
def computer():
	Memory = input("Enter Memory size")
	RAM = input("How much RAM, you want")
	Hard_Disk = input("Enter Hard Disk size")

Final Thoughts

I hope that this article have helped you to understand what’s meaning of Indentation, Statements and Comments in Python Programming Language. If in case you have some question then let me know in the comments below and I’ll try to reply as soon as possible for me.

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

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

Recent Posts