Keyword , Identifier, Indentation, Comments & Documentation

Keyword :

In Python, a keyword is a special word that has a specific meaning and purpose in the language. These words are reserved, which means you can’t use them for anything other than their intended purpose. For example, you can’t use a keyword as a variable name.

Here are a few examples of Python keywords and what they do:

  • if: Used to make a decision. For example, if x > 0:.
  • for: Used to create a loop. For example, for i in range(10):.
  • def: Used to define a function. For example, def my_function():.
  • True and False: Represent boolean values.


 Identifier :

In Python, an identifier is simply a name you give to things like variables, functions, classes, or modules. It’s like a label that helps you refer to these elements in your code.

Here are some key points about identifiers:

  • Must start with a letter or an underscore (_): For example, name_value.
  • Cannot start with a number: For example, 1name is not allowed.
  • Can contain letters, numbers, and underscores: For example, my_variable1.
  • Case-sensitive: This means myVariable and myvariable are considered different identifiers.
  • Cannot be a Python keyword: For example, you can’t use iffor, or while as identifiers.

Example :
name = "Python"  # 'name' is an identifier 
def greet():     # 'greet' is an identifier for the function
    print("Hello, " + name)

greet()  # This will print: Hello, Python 

Indentation :

In Python, indentation refers to the spaces at the beginning of a code line. It’s very important because it indicates a block of code. Unlike many other programming languages that use braces {} to define blocks of code, Python uses indentation to show where a block starts and ends.


Example :

if 5 > 2:
    print("Five is greater than two!") # white spaces is indentation
    print("This is part of the if block.")
print("This is outside the if block.")


Comments in Python

Comments are notes you add to your code to explain what it does. They are ignored by the Python interpreter, so they don’t affect how your code runs. There are two main types of comments:

  1. Single-line comments: Start with a # symbol. Everything after # on that line is a comment.

     Example :
           # This is a single-line comment
           x = 5 # This is an inline comment

2. Multi-line comments: Use triple quotes (''' or """). These are often used for longer explanations or to temporarily disable code.
       Example :
        """
This is a multi-line comment.
It can span multiple lines.
"""

Documentation in Python

Documentation helps others understand how to use your code. The most common way to document Python code is using docstrings. Docstrings are special comments that describe what a module, function, class, or method does. They are written inside triple quotes and placed right after the definition.

  1. Function docstring

  2. Class docstring:

Best Practices

  • Be clear and concise: Write comments and docstrings that are easy to understand.
  • Keep comments up-to-date: Update comments if you change the code.
  • Avoid obvious comments: Don’t state the obvious. Focus on explaining why something is done, not what is done.

Comments

Popular posts from this blog

DSA Lab 8 program

DSA Lab 7 Program