HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Python / Python Flow Control / Python pass Statement

Python pass Statement

The Python pass statement is used to execute an empty statement. We can use the pass statement when we do not want to execute any statement at a place in the code, but Python requires us to specify a statement to satisfy the Syntax rules.

Python pass statement is different than a Python comment. A comment is totally ignored by the Python interpreter, but pass sstatement is not ignored.

Python pass Statement Example

The for loop in Python requires at least one statement to execute. If we do not write any statement under for loop body, the program will give an error.

If for some strange reason, we want to loop through a sequence but do not want to execute any statement, we can use pass statement.

names = ["alex", "brian", "charles"]

for x in names:
  pass

print("Statement after the loop body")

Program output.

Statement after the loop body

The pass Block

Please note that pass statement does not work as the break statement. It does not pass the whole code block.

The pass statement only passes the single statement. If there are other statements in scope of pass block, they will be executed.

names = ["alex", "brian", "charles"]

for x in names:
  pass
  print(x)

print("Statement after the loop body")

Program output.

alex
brian
charles
Statement after the loop body

Happy Learning !!

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Python Tutorial

  • Python Introduction
  • Python Install in Sublime
  • Python Keywords
  • Python Comments
  • Python Variables
  • Python Data Types
  • Python Type Conversion
  • Python References
  • Python Examples

Python Flow Control

  • Python if…else
  • Python for Loop
  • Python while Loop
  • Python break
  • Python continue
  • Python pass

Python Datatypes

  • Python Integer
  • Python String
  • Python List
  • Python Tuple
  • Python Set
  • Python Dictionary
  • Python OrderedDict
  • Python MultiDict
  • Python Priority Queue

Python Advanced Topics

  • Python Lists vs Tuples
  • Python Comparing Tuples
  • Python Unpacking Tuples
  • Python CSV Files

Python Libraries

  • Python Httplib2

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)