You learned about code comments in the previous lesson. Now you'll learn about your first Python keyword! Python has a number of keywords that are part of the language and have special meanings.
You'll start with a fun one, pass, which doesn't do anything. Lazy, right? :)
Photo by Kiy Turk https://unsplash.com/@kiy_turk
What Does the Pass Keyword Do
The pass keyword is generally used as a placeholder while building out your code functionality. You might see that it sounds a little similar to a code comment. However, it's actual code that Python executes. It just doesn't do anything. So why would you need it?
Python doesn't do anything when it encounters the pass keyword, but execution can continue. Sometimes that means that Python won't throw a syntax-related error that would come up if you hadn't used pass.
Note: Here comes some more code that won't mean anything to you yet. Keep your open Zen mindset and let it flow over you in peace and stillness. Just focus on the concept of the pass keyword; all the rest will come with time.
Pass Keyword Example
If you are planning to build a for loop but don't quite have the functionality figured out yet. You could use a mix of code and pseudocode and write something like this:
user_input = input("whisper something: ")
for char in user_input:
# Make each character uppercase
# So that the program SHOUTS back at the user
print("ssssshhhh!")
You might understand the intended functionality of this code snippet, or maybe not. It doesn't matter for the purpose of thinking about the concept of the pass keyword.
Test Functionality
In any case, this code isn't quite finished yet. There is already some functionality coded up, while other parts are just described as a code comment. You might want to test the existing functionality before filling in the gaps. For example, does the user input work as expected?
However, if you would run this code as-is, you'd get an error:
# OUTPUT
File "your-file-name.py", line 4
print("ssssshhhh!")
^
IndentationError: expected an indented block
You'd receive this error because you did something that goes against a rule in the Python language. Don't worry; there are no repercussions! No fines, no jail time, just some feedback from your buddy Python telling you that you did something that was confusing.
Info: In case you're wondering, the specific reason you're receiving an error is that you opened up a loop body for a loop, but then you didn't write any actual code there. Python expects at least some code inside the loop body, otherwise... why would you make it? Poor Python just doesn't get it...
To avoid confusing your friend Python, you can put the pass keyword into the body of the loop:
user_input = input("whisper something: ")
for char in user_input:
pass
# Make each character uppercase
# So that the program SHOUTS back at the user
print("ssssshhhh!")
How Does the Pass Keyword Work
Because pass is actual Python code, and not just a code comment that Python automatically ignores, this construct sticks to the rules and keeps Python happy and relaxed.
Python sees the pass keyword and understands that this is a valid for loop construct. It also understands that it just shouldn't do anything here right now.
Using pass in this scenario makes it possible for you to double-check the code you already wrote and then later move on to building out the functionality that's still missing.
Summary: Python Pass Keyword
- The
passkeyword doesn't do anything, but it is code, not just a code comment - Python reads
passit and understands that it shouldn't execute anything - You can use
passas a structural placeholder while planning, testing and building out your code functionality