Course Content
Python Indentation, Comments and Variables
0/2
Object Oriented Programming in Python
0/1
Exception Handling in Python
0/1
Sending emails with Python
0/1
Unit test in python programming
0/1
Python programming (zero to advance!!!)
About Lesson

1 Python Indentation
Indentation in Python refers to the leading spaces at the beginning of a code line.

Unlike many other programming languages where indentation is purely for readability, Python treats indentation as a critical aspect of its syntax. It is used to delineate blocks of code such as loops, conditional statements, and function definitions.

Consider the following examples:

“`python
# Example 1
if x > 5:
     print(“x is greater than 5”)
“`

In this instance, the `print()` statement is indented by four spaces after the `if` statement. This indentation signifies that the `print()` statement belongs to the if-block.

Failure to adhere to proper indentation results in a syntax error, as demonstrated below:

“`python
# Example 2 – Syntax Error
if x > 5:
    print(“x is greater than 5”) # Incorrect indentation
“`

While the number of spaces used for indentation is a matter of preference, maintaining consistency within the same block of code is essential. The convention commonly employs four spaces for each level of indentation, though any consistent whitespace can suffice.

“`python
# Example 3
if x > 5:
     print(“x is greater than 5”) # Four spaces used for indentation

# Example 4
if x > 5:
     print(“x is greater than 5”) # Eight spaces for indentation within the same block
“`

It’s crucial to avoid mixing different indentation levels within the same block of code to prevent syntax errors:

“`python
# Example 5 – Syntax Error
if x > 5:
    print(“x is greater than 5”) # Four spaces used for indentation
    print(“x is greater than 5”) # Inconsistent indentation within the same block
“`

The indentation rules enforce clear and consistent coding styles, enhancing code readability and maintainability. Adhering to proper indentation practices is essential for writing clean and error-free Python code.

2 Python Comments

Comments play a crucial role in Python programming by enhancing code readability and providing explanatory notes. They also serve to prevent code execution during testing phases.

**Single-Line Comments in Python**

In Python, single-line comments start with the hash symbol `#`, and Python ignores everything following it on the same line.

Example:

“`python
# Print a message
print(“Hello Software Testers”)
“`

Comments can also appear at the end of a line, with Python disregarding the remainder of the line after the `#`.

Example:

“`python
print(“Hello Python Programmers”) # This is a message
“`

Comments need not only explain code but can also prevent Python from executing specific lines, as demonstrated below:

Example:

“`python
# print(“Hello Python World!”)
print(“Selenium Testers”)
“`

**Python Multi-Line Comments**

Python does not have a specific syntax for multi-line comments. To create a multi-line comment, you can insert a `#` for each line.

Example:

“`python
# Python supports the implicit declaration of Data Types
# User can check a variable Data type using the ‘type’ keyword
# Python is a case-sensitive language
x = 100
type(x) # int
“`

Alternatively, you can use a multiline string for writing multi-line comments. Python disregards string literals that aren’t assigned to a variable.

Example:

“`python
“””
This is a comment
written in
more than just one line
“””
print(“Hello Python World”)
“`

**Advantages of Using Comments:**

1. **Enhanced Understandability**: Comments make code more understandable, helping developers recall the purpose of specific code blocks.

2. **Readability**: Well-placed comments improve program readability, making it easier for others to comprehend your code.

3. **Testing and Pseudo-code**: Comments offer a simple way to ignore certain code segments during testing or to draft pseudo-code quickly, aiding in program development and debugging.

By leveraging comments effectively, Python developers can write cleaner, more maintainable code that is easier to understand and debug.

 

 

Join the conversation