Python Comment | Python Indentation | Python Statement

post

Learn Python basics with Debugshala – Comments, Indentation & Statements explained with syntax, types & examples in a simple, easy way!

Python Statement

In Python, everything revolves around statements. For example:

>>> a *= 3 

1. Multiline Python Statement

Typically, each Python statement ends with a newline. But you can extend it to multiple lines using the backslash \ character.

>>> a =\    10\    + 20 >>> a 30 

Another example:

>>> "Hello\ hi" 'Hellohi' 

You can also wrap expressions in parentheses to achieve multiline statements:

>>> a = (    10 +    20) >>> a 30 >>> type(a) <class 'int'>

2. Multiple Statements in One Line

Python allows placing multiple statements on the same line using a semicolon ;.

>>> a = 7; print(a) 7 

You can even write an if condition in the same way:

>>> if 2 > 1: print("2") 2 

3. String Statements in Python

You can declare strings using single or double quotes:

>>> "Hello 'user'" "Hello 'user'" 

If you use double quotes for the outer string, you can use single quotes inside, and vice versa.

4. Blank Lines in Python

Blank lines are ignored by the Python interpreter.

Python Indentation

Unlike other languages like C++ or Java that use curly braces {} for blocks, Python uses indentation to define code blocks.

>>> if 2 > 1:        print("2") 2 

There’s no strict rule for how many spaces you should use, but the indentation must be consistent within a block. Typically, four spaces are recommended. Using tabs is discouraged.

Here’s an example of inconsistent indentation that results in an error:

>>> if 2 > 1:        print("1")         print("2") # SyntaxError: unexpected indent 

Python Comment

Comments are notes written by the programmer to make the code easier to understand. These are ignored by the interpreter.

You might need to revisit your code after weeks or someone else might work on it — good comments help clarify what the code does.

Python uses the hash symbol # for single-line comments:

>>> # This is a comment 

1. Multiline Comments in Python

To write multiline comments, use # at the beginning of each line:

>>> # Line 1 of comment >>> # Line 2 of comment >>> # Line 3 of comment 

You can also use triple quotes (''' ''' or """ """) for writing comments across multiple lines:

>>> """This comment is spread across multiple lines""" "This comment\nis spread across\nmultiple lines" 

These work in the interactive shell, where they return a string. But in script files, they act as comments and do not produce output.

Triple-quoted strings are also great for preserving formatting:

>>> print("""Hello Hi""") Hello Hi

2. Docstrings in Python

A docstring is a special kind of comment used for documentation. It is placed as the first statement in a module, function, class, or method.

>>> def sayhi():    """    This function prints Hi    """    print("Hi") >>> sayhi() Hi

You can access the docstring using the __doc__ attribute:

>>> sayhi.__doc__ '\n    This function prints Hi\n    ' 

Note: The docstring must be the first statement in the function or it won’t be recognized.

>>> def sayhi():    print("Hi")    """    This function prints Hi    """ >>> sayhi.__doc__ None 

Python Interview Questions (Topic: Indentation, Comments, and Statements)

What are the different types of Python Statements?

How do you write comments in Python?

What is the use of the hash symbol # in Python?

Why is indentation important in Python?

Write a code snippet that uses a docstring in a function.

Conclusion

That’s it! We’ve covered the basics of Python indentation, comments, and statements. We hope this Debugshala guide helped you understand these concepts more clearly.


Share This Job:

Write A Comment

    No Comments