Indentation and Comments in Python (Part 2)

1.Indentation in Python

  • In Python, indentation refers to the spaces at the beginning of a code line.
  • Unlike many other programming languages, indentation in Python is crucial and not just for readability. It is used to define code blocks.
  • Python uses indentation to indicate which statements belong to a particular block of code. All the statements indented at the same level are considered part of the same block.
  • Indentation level can be increased to create nested blocks.
  • The number of spaces used for indentation is up to the programmer, as long as it’s consistent within the same block. Using at least one space is mandatory.
  • It is recommended to use the same number of spaces for all lines in the same block to improve code readability.

highlighted is indentation

2.Comments in Python

  • Single line comments: These comments start with the hash symbol (#) and extend to the end of the line. They are used for short explanations about the code.
# This is a single line comment

  • Multiline comments: Python doesn’t have a dedicated way to write multiline comments, but there are two workarounds:
    • Using multiple lines that each begin with a hash symbol (#).

# This is a multiline comment
# created using multiple hash symbols

* Using triple quoted strings (either three single quotes or three double quotes).  These are not technically comments, but the Python interpreter will ignore them.

"""This is a multiline comment
created using triple quoted strings"""

  • Docstrings: These are special multiline comments used to document functions, classes, and modules. Docstrings are typically placed within triple quotes directly below the function or class definition. They provide information about the function or class’s purpose, usage, and parameters.

def greet(name):
  """
  This function greets the user by name.

  Args:
      name: The name of the person to greet.

  Returns:
      A string containing the greeting message.
  """
  return f"Hello, {name}!"

print(greet("Alice"))

Related Posts

Package your Python code as Wheel File (Part-20)

Package your Python code as Wheel File If you want to share code with others or create libraries for your code so other can use your code….

Read More

map() function in Python (Part-19)

Using map() Function in Python The map() function in Python is used to apply a function to all the items in an input list. Here are some…

Read More

User Input | input() function |File Handling| open() function in Python (Part-18)

Using input() in Python 3.6 and later When you run this code, it will prompt you to enter a username and then print the entered username. Using…

Read More

Try…Except in Python | Exception handling in Python (part-17)

Exception Handling When an error occurs, or exception as we call it, Python will normally stop and generate an error message. These exceptions can be handled using…

Read More

PIP in Python (Part-16)

What is PIP? PIP is a package manager for Python. It allows you to install and manage additional libraries and dependencies that are not included in the…

Read More

JSON in Python (Part-15)

JSON in Python JSON (JavaScript Object Notation) is a lightweight data interchange format that’s easy for humans to read and write, and easy for machines to parse…

Read More
guest
0 Comments
Oldest
Newest Most Voted
0
Would love your thoughts, please comment.x
()
x