Python

Pass Statement in Python with Example

Description:

hey everyone in this article  I’m going to be teaching you about a statement in Python that literally does nothing and that statement is “pass” anytime we use the keyword pass we are saying do nothing so it’s really weird at first but I’ll explain why it’s useful.



so as an example

pass statement in python

Now I am using the pass statement and see what will be the out of our program


pass statement in python

we can just put the keyword pass anywhere in our program we’ll put it inside the if statement and I’ll even put one after the if statement as well and heck why not put one at the end however when we run this program it works exactly the same as you can see we can search for something and it’ll tell us when it found it so why in the word what I want to use the pass statement well first you should know about it because you’re probably going to see it and I don’t want you guys to be confused on what that word means but the second thing is you can use it as a placeholder until you have a better solution for the problem so it’s the equivalent of saying hey this does not do anything right now but we’re going to implement this in the future it’s a little bit more clear to developers when you explicitly say hey I’m gonna pass on filling out this part right here but I’m gonna get back to that rather than just leaving it blank and on top of that sometimes if you need to leave a blank you have to use the pass statement in python.


so let me show you an example

first, let me just get rid of all of these pass statement from the program and we have the if statement let’s just go ahead and say hmm I’m not sure what to put there let’s just put pass statement and when we run this our program works and we can run it

pass statement in python

it’s happy it doesn’t really do anything because we didn’t implement the if statement however it runs as you can see in the above picture.



now see this if I remove the of pass statement and running now look I getting an issue

pass statement in python

because it’s expecting an indent it’s expecting a line there and even if we put that indent there we run this is still not going to work so any time it’s expecting an indent or expecting some line of code but you don’t have anything to put there you just use the pass statement in python.

another common example of this is with classes and functions which we haven’t even talked about this yet but you can say something like

if you’re not ready to make this you can just say pass or you might have class data and you’re not really sure what you want to do to implement that so you just used pass statement inside the if statement will say pass and all of the code should be fine you should be able to use it and it’s not gonna do anything but it allows you to run the code

pass statement in python


so that’s all I got for you guys on paths pretty simple yeah I don’t really know what else to say so I’m just going to move on to the next one well before we go I’m gonna put this back I was and say fruit was found all right there we go we have a search algorithm and there’s no pass statement involved

pass statement in python

Break vs continue vs pass statement in python:

In this section, I will show you the difference between break continued and pass statement in python.


Break statement:

look at  the code below

pass statement in python


Code explanation:

So the first thing we’re going to do is print I then the next line we look at is if I is greater than 5 we will break so what this breaks essentially does it breaks out of the outer loop the nearest outer loop so say we had wild true for I in range of 100 it would break out of the overlying range of hundred and not the while true loop but if this condition isn’t satisfied we will print programming digest and then we’ll go to the next line print community so it’s important to realize is that if the break condition is not satisfied we will hit this else statement and then we’ll continue on and hit the print come here.

Continue statement:

so now I’m going to show you what happens with continue

Code explanation:

so essentially continue doesn’t break out of the loop continues just sort of a misnomer it doesn’t continue the loop what it does is it goes back to the beginning of the loop what you’re going to see is something specific to the continued is that instead of continuing the loop and printing out the print statement what it does is it actually goes back to the beginning of the loop so if  the if statement gets hit it does not continue going down it instead just continues to the beginning in the loop so that’s a key this is a key distinction that you should be aware of when using the continue loop so I’m going to run this now and we’re going to look at the output

pass statement in python

okay so let’s go to the beginning and we’re going to see exactly what happened okay so we learn the for in range the first thing that’s going we’re going to do is print the i so which is zero then if I is greater than five will continue else will print programming digest so that makes sense we print programming digest and then we break out of the if else loop and we will print come here  lonely so this will continue until I is greater than five so let’s see what happens with I is greater than 5 which is 6 so what happens the I is 6 when it continues the continue will bring this back to the beginning of the loop so we don’t actually get to print out the statement which is not part of the  if-else statement so we actually skip print com here part and just go back to the beginning of the for loop so as you can see we do all the way until the  for loop is completed so anytime the if statement is activated we go back to the beginning of loop so the continue will be activated again we go back to the beginning loop we do this all the way until 99 which is the ranger 100


 pass statement in python:

now I’m gonna show you what pass statement in python do we’re going to run the below code and we’re going to see exactly what happens with pass statement

pass statement in python


code explanation:

so for I in range hundred print I if I is greater than 5 if the if statement gets activated we pass, passes essentially do nothing so it’s just going to move forward so if we say pass the else statement doesn’t get activated because the if statement got activated and we will hit the print statement come here so essentially once 5 is activated the if statement greater than 5 is activated. Pass statement in python is just sort of do nothing it’s a filler command that is just saying do nothing.

so the difference between pass and continues that continue brings us back to forces back to the beginning of the loop while pass statement just is a filler and doesn’t really do anything so we just continue on.

 

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button