TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to PYTHON

Python pass Statement

Use the pass statement to indicate empty functions, classes and loops.
Pass. Time passes. And often nothing happens. In Python we use the "pass" keyword (a statement) to indicate that nothing happens—the function, class or loop is empty.Built-ins
With pass, we indicate a "null" block. Pass can be placed on the same line, or on a separate line. Pass can be used to quickly add things that are unimplemented.
Method example. Here we use the "pass" statement on two methods. These methods do nothing. But they can be called in Python code.

Result: Nothing happens when the pass methods are called. Pass can be used in other places in Python programs.

Python program that uses pass # This function does nothing. # ... It is empty. def test(n): pass # Pass can be on a separate line. def test_two(n): pass # Call our empty functions. test(10) test(200) test_two(10) test_two(200) # We are done. print("DONE") Output DONE
Class example. Sometimes we use the "pass" statement in a class. When we first add a class, we might not be ready to add its entire contents. Pass is helpful.
Python program that uses class with pass # An empty class. class Box: pass # A class with an empty method. class Bird: def chirp(self): print("Bird chirped") def fly(self): pass # Create empty class. x = Box() # Create class and call empty method. b = Bird() b.fly() # Program is done. print("Z") Output Z
If example. Here we use pass in an if-statement. If a method has a side effect, we sometimes might want to call it even if we do not do anything afterwards.

And: With pass we can add "elif" clauses afterwards. This sometimes makes code clearer to read.

Python program that uses if, pass # This method has a side effect. # ... It prints a word. def equals(name, value): print("equals") return name == value name = "snake" # Use if-statement with a method that has a side effect. # ... Use pass to ignore contents of block. if equals(name, "snake"): pass Output equals
While example. With a while-loop, we can use a "pass" statement. Sometimes we want to keep calling a method while a condition is true. With pass we can use an empty loop body.
Python program that uses while, pass import random def x(): # Print and return a random int. result = random.randint(0, 5) print(result) return result # Continue calling x while its value is at least 1. # ... Use no loop body. while x() >= 1: pass print("DONE") Output 2 3 1 3 1 4 3 4 0 DONE
Some research. With pass, we have a null operation. It does nothing—it passes on taking an action. In some languages we might use a semicolon to indicate nothing.

Quote: Pass is a null operation—when it is executed, nothing happens. It is useful as a placeholder when a statement is required syntactically, but no code needs to be executed.

Simple statements: python.org
A summary. Python has a full-featured group of keywords. But it has no semicolons. So we must indicate an empty line with a special keyword. That is "pass."
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf