Skip to main content

Command Palette

Search for a command to run...

Python For Beginner's Day - 2

Published
2 min readView as Markdown

Python Indentation:

  • Indentation is very important in Python.

  • It refers to the spaces at the beginning of the code line.

  • used to indicate block of code.

    • From above, the colon (:) at the end of the if line indicates that new code block is starting.

    • Subsequent lines which are indented are part of that code block.

    • Python gives you an error if an indentation is missed.

    • The Number of spaces for indentation is up to the programmer, the most common use is 4, but it should have at least 1.

    • Observe the below code block, the first print function is after 4 spaces and subsequent should also follow the same, but it has extra spaces. Try to execute and check the below code.

      • Output will be

        • The same code block, can be written as

          • Output will be

            NOTE: If you have coded before for other programming languages, they use {curly braces} to mark beginning and ending of the code blocks. But Python uses Indentation which are nothing but whitespaces.

Python Comments:

  • In Python, Comments start with '#'. That line will be ignored while execution.

  • Comments can be used to make the code more readable.

  • Whatever code block is written below, that can be explained inside the comments and whenever some other person tries to read that, it is easy for them to understand what has been implemented.

Creating a Comment:

  • From above, first line is commenting, output for above is:

    • Comments can also be placed at the end of the line, python just ignores that rest of the line.

    • A Comment does not have to be text that explains the code, it can also be used to prevent Python from executing code.

Multiline Comments:

  • To add a multiline comment, just insert a # for each line.

  • Observe below code for Multiline Comments,

    • Python ignores string literals that are not assigned to a variable, so just add a multiline string (triple quotes) in your code, and place your comment inside it, like below

More from this blog

Anusha's blog

15 posts