Basic Syntax & Program Structure in Python

Module 1 • Session 04 • Allocation: 1 Contact Hr

Indentation Statements Comments PEP 8

Python design emphasizes code readability and simplicity. Unlike languages such as C, C++, or Java that rely on curly braces {} and semicolons ; to mark statement blocks and terminations, Python utilizes **whitespace indentation** and **newlines** to define program structure.

1. Indentation & Block Structure

In Python, blocks of code (functions, loops, conditionals, classes) are defined by their indentation level. A block begins with a colon : at the end of a header line, and all subsequent statements in the block must be indented equally.

PEP 8 Standard: The official Python Style Guide specifies using 4 spaces per indentation level. Mixing spaces and tabs results in an IndentationError or unpredictable execution behavior.
def evaluate_score(score): # Block level 1 (4 spaces) if score >= 90: # Block level 2 (8 spaces) status = "Passed with Distinction" print(f"Result: {status}") else: # Block level 2 (8 spaces) status = "Passed Standard" print(f"Result: {status}") evaluate_score(95)

2. Statements: Single-line & Multi-line

By default, every statement ends with a new line. Semicolons can place multiple statements on one line, but this is strongly discouraged by PEP 8.

Implicit & Explicit Line Continuation

When expressions are lengthy, split them using implicit continuation inside parentheses (), brackets [], or braces {}, or explicitly using the backslash \ character.

# Preferred: Implicit continuation within parentheses total = ( first_value + second_value + third_value ) # Explicit continuation using backslash \ long_greeting = "Hello, welcome to Python programming. " \ "This line is continued explicitly."

3. Comments and Documentation Strings (Docstrings)

Comments clarify intent for human developers and are completely ignored by the Python interpreter during execution.

  • Single-line Comments: Initiated with the hash symbol #.
  • Multi-line Comments: Created using consecutive # lines or unassigned multi-line strings.
  • Docstrings: Triple-quoted strings """...""" placed immediately below module, class, or function definitions to serve as built-in documentation accessed via help().
def calculate_area(length: float, width: float) -> float: """Calculate the surface area of a rectangle. Parameters: length (float): The length dimension. width (float): The width dimension. Returns: float: The calculated area product. """ return length * width # Interrogate docstring programmatically: print(calculate_area.__doc__)

4. Main Execution Block (`if __name__ == '__main__':`)

When executing a script directly from the terminal, Python sets the special built-in variable __name__ to "__main__". If the file is imported into another script, __name__ holds the module's filename instead.

def main(): print("Executing core script logic...") if __name__ == "__main__": main()

🏋️ Try It Yourself: Practice Challenges

Challenge 1: Fix the Indentation Bug

Run the sandbox code below to fix an intentional IndentationError and structure a nested decision block correctly.

Challenge 2: Docstring Inspector

Define a utility function with a detailed multi-line docstring and print its metadata using help() and __doc__.

Challenge 3: Multi-Line Calculator

Construct a multi-line mathematical evaluation using implicit tuple continuation without throwing syntax errors.

⚡ Interactive Sandbox (Syntax & Structure Tester)
Console Output:
Click "Run Code" above to execute interactive input scripts...

📝 Knowledge Check Quiz

1. How does Python identify code blocks for functions, loops, and conditional branches?
2. What is the PEP 8 recommended standard for indentation spacing in Python?
3. What special attribute allows you to inspect a function's docstring programmatically?
Advertisement
Horizontal Ad Banner Slot (728x90 / Responsive)