Institute of Computer Science
  1. Courses
  2. 2024/25 fall
  3. Introduction to Programming (MTAT.03.236)
ET
Log in

Introduction to Programming 2024/25 fall

  • Home Page
  • Introduction
  • Expressions
  • Conditional Execution
  • Functions
  • Iterations
  • Strings
  • Files
  • Lists
  • Graphics?
< previousTable of Contentsnext >

3.4 THE PASS STATEMENT

The pass is a statement that does nothing. This can be useful in various situations, especially in places where your code will eventually go but has not been written yet (a placeholder) or when you're required to have a statement by Python's syntax but you don't want to execute any code.

PASS STATEMENT AS A PLACEHOLDER

The most common use of pass is as a placeholder for future code. It's common to outline your code's structure during the development process without implementing the functionality immediately. Using pass allows you to run your code without causing any errors.


has_ID_card = True
if has_ID_card:
    pass  # Placeholder for implementing further logic later. 
else:
    print("You need an ID card to proceed!")

Python syntax requires at least one statement in blocks like if, elif, else, for, while, and except. If you have a block with no content, you can use pass to avoid an error.

If we do not include a pass statement but insert a comment instead in an if statement, for example, the result will be an IndentationError. Here's a Thonny example to illustrate.

Furthermore, for exception handling, you may want to catch exceptions but intentionally ignore them without performing any error handling, so you can use the pass statement in the except block. More on exception handling will be discussed at the end of this chapter.

KEY THINGS TO REMEMBER

  • The pass statement is a null operation that does nothing when executed. It's effectively a placeholder in the code.
  • It helps prevent errors by fulfilling Python's requirement for non-empty code blocks.
< previousTable of Contentsnext >

  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment