Python From Beginner to Advanced

0% completed

Previous
Next
Python - pass Statement

The pass statement in Python is used when a statement is required syntactically, but no action is needed. It acts as a placeholder and prevents syntax errors in situations where a statement is expected but not yet implemented.

Syntax

if condition: pass # Placeholder statement
  • pass does nothing when executed.
  • It is often used in places where code will be added later or when a block is intentionally left empty.

Examples

Example 1: Using "pass" in an "if" Statement

Python3
Python3

. . . .

Explanation

  1. The if condition checks if age is 18 or more.
  2. Since the block is empty, pass prevents a syntax error.
  3. Nothing happens when age >= 18, but if age were less than 18, "You are not eligible to vote." would be printed.

Example 2: Using "pass" in a Loop

Python3
Python3

. . . .

Explanation

  1. The loop iterates five times.
  2. Since pass is used, nothing happens in each iteration.
  3. This is useful when a loop structure is needed but logic is not yet implemented.

Example 3: Using "pass" in a Function

Python3
Python3

. . . .

Explanation

  1. The function placeholder_function() is defined but has no logic.
  2. pass allows the function to exist without causing an error.
  3. This is useful when designing code but postponing implementation.

The pass statement is a placeholder that allows empty blocks of code without causing errors. It is useful for defining empty functions, loops, and conditional statements during development.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next