Hi, everybody.
All right, so now that we are past theri level basics regarding methods and we have seen in the previous
video how to create simple methods and also how to create methods that receive variables from the execution
level, I think it's time to get over to the mid level theory and see some more cool tricks that methods
can apply on.
Also, you can see here behaviors, some of them, those are especially only in Python as Python is
a very flexible language.
And it can see really, really cool tricks here that other programming languages cannot do.
All right.
So let's start off with the first one and let's just go over it.
All right.
So we have seen that we can pass variables into methods, but what if we're not sure that each time
that a certain method would be called out would be executed?
We're not sure that each time we will have a variable to pass through it.
OK, so in this case, we'll have simply an error because like, if a certain method should receive
a variable and we can see it in the declaration level with the line that contains the def keyword and
it should get a variable BOBERT from the module level, we did not pass a variable.
So this will cause all program to to fail.
But for that we have a solution in Python.
All right.
So in this case that we are not sure if certain variable variable will be passed to a method.
You can see the example right here.
We can do the following thing.
We can set a default value to the method, meaning right here we can see that we have created my function.
All right.
This is the method.
And inside the round brackets, we have set a value country equals Canada.
All right.
So this is the declaration.
And from here, from the middle part here, we're going to execute the execute the the method and let's
see what happens.
So if we take the method and execute it for for the first one right here, we're seeing my function.
And then inside the brackets we see Eataly.
So the printout inside the method would be, I'm from Italy.
That's easy.
But from the green one right here, we can see that we haven't passed any variable to this method at
all.
All right.
So it's empty.
We have some kind of problem here because the print here is waiting for a variable.
And we did not pass any variable to this method.
But we have overcame this of failure with the following solution.
We have set a default value for this method right here in the in the declaration line.
So you can see contre equals Canada, meaning that once we will not have any value set up here in the
execution level, the different value, the default value that would be printed out here would be Canada.
So this is how we can now overcome an exception or an error in the side, our code execution by setting
up a default value.
All right, let's go straight ahead to the next topic that we're going to learn regarding methods,
and it goes as follows.
OK, so we already familiar that methods can receive variables, all collections into them in the execution
line.
But a method can also return a value back to the module level, meaning that if we would like to pass
some variable out of the method into the module level, we can achieve it.
OK, so here we have again, we have a declaration of a module.
OK, my function and you can see here the new keyword that we're not familiar with up until this point
return can see it marked in pink.
I saw the return key word basically returns, whatever comes after it into the execution level that
we can see right here with a pink and recombined.
OK, so first of all, we execute the we execute the method by stating its name right here, my function,
and we're passing the variable three into it.
OK, so from here it goes up to the top and X equals three, meaning it goes inside and inside we can
see a return five equals X, right.
And in this case, X is equals three.
So basically a return will be would be returned the value of 15 in our case.
So what am I doing here is once I executed the method right here, I put an equal sign to the left and
to the left some more.
I've created a new variable called number, and as this method right here returns a value.
OK, so this number variable would get this value that was extracted out of the method to the module
level right here outside of the method.
This is the module level and here with the intention is the method level.
So right here in the module level, we would get the value that was returned from inside the method
to out here and then we can do whatever we want with it.
In our case, the value that was set inside the method is three three three multiplied by five is 15.
So once we print this number variable, we would get the output 15, correct.
I would like to do the following thing, which is going to copy all of this code.
OK.
And show it to you live on by charm, I think is the best way to understand things like that.
All right.
OK, so here's the method and here's the return.
So basically from here, the three goes up in here to the X, OK?
And then this X goes inside the method right here.
And we have the return, as you can see.
Also, the return is is with orange color, which is a special keyword.
And then after the return, we have five multiplied by three in our case.
OK, and then the number of variable right here gets whatever comes out of this method.
This is like we have a return from here.
So everything that is returned from here goes straight to this number.
So as you can see, when we run it, we get the output 15 in once we delete it.
For example, let's put it in.
The comments on this line does not exist and let's put base here so we won't have any error.
And now once we will run it, we will get nothing.
All right.
We have none because number was never set up with anything, because here we didn't have any return
action made out from inside of the method.
So we have none here.
But when we have this value, the value that comes after the return is being returned to the execution
level and we can straight set up a variable and print it out.
And we also can do the following thing.
We can just take this thing, which equals 15 and also put this one inside here inside the print, because
this whole term equals to 15 once we print it.
OK, so there you go.
So we don't even need the number available.
Just easier to understand with.
No.
So, OK, in this case, the number gets the value for whatever comes from here.
All right.
And for the first example right here, I also want to show it.
Let me just copy it to them, to the picture and let's look at it together.
OK, so right here we have this thing.
I'm just going to delete the output, OK?
And just going to leave the EU one with a variable passed to the method and one without with an empty,
empty one.
All right.
And once we execute it, so you see it runs perfectly because in the second case, once we did not pass
that variable to the method.
OK, so the default value was executed and this country equals Canada was set up in here and we have
it inside the printout.
What if we will remove this one?
All right.
Just just leave country here.
You see that right here.
If we hover above above it, parameter country on field.
OK, so we have a certain problem here.
And you see we have an exception.
It's a fail, by the way, just reminding that we are going to learn all of these type errors and exceptions
in a later stage.
But for now, we're just seeing that we have a problem here regarding this execution right here, because
at certain value is expected of the method to be inserted in here.
But nothing comes in.
And then we have a problem because the the code does not know what to run here.
So if we have the default value, we do not have any issues with this thing.
All right.
That's about it.
Regarding Python mid-level lecture, regarding methods, I think we're done with this one.
I'll see you in the next lecture where where we discuss some.
Furthermore, regarding methods, this will be the advanced lecture.
Thank you and bye bye.