[no audio]
Friends, here we are going to
discuss about Python functions concept, simply
introduction part with your functions. Before going to work
with your functions,
let us assume that we have some code which consists of
suppose some 150 lines.
So I have 150 lines of code.
And if you observe this entire code, somewhere I am repeating
same logic in different places.
Assume that, same logic I am repeating in 3 places.
Assume that each logic consists of suppose some 30 lines.
That means, the same 30 lines of logic you are repeating in 3 places.
Not only 3, just assume that some number of
times you are repeating same type of logic.
Right. See if you are repeating the same logic in different places,
in that case you don't need to write your logic in 3 places whenever
if you are repeating. What we will do is, whatever
the logic you are repeating no, that logic I will write
in one place in my code itself, and I will give some name
suppose for that 'mycode'.
[no audio]
And now what I will do is, I will remove these 30 lines of
code in 3 places.
Wherever you have
30 lines of code, right, there simply I will write 'mycode'.
I have given a name for 30 lines of code, 'mycode'.
Now here I am giving 'mycode'.
[no audio]
'mycode',
Right. Now guys, what I am doing is, see, actually you have
150 lines of code, in this 150 lines of code, 30 + 30 + 30, 90,
90 lines, plus remaining 60 lines, finally 150 lines of code.
But 30 lines of code we are repeating in 3 places.
That means, instead of these 90 lines,
what we are doing is 30 lines of code
I'm writing somewhere.
Wherever this 30 lines of code is required, there
I will write the name which I have given for this 30 lines of code.
So, what is the name I have given? 'mycode'.
So here I will write 'mycode'. Instead of 30 lines simply I'm writing only
one line. So, you are saving 29 lines.
You're reducing 29 lines in this place. Same way in this 30
lines, I am removing these 30 lines.
Simply I'm writing 'mycode'.
That's it. I'm giving the name.
Then again here also, reducing 29 lines.
So then here also you are having 30 lines no,
I am removing these 30 lines of code and simply
I'm writing whatever the name I have given for this 30 lines,
'mycode'. Then here also you are reducing 29 line numbers.
So instead of 90 lines, now you are using only 3 lines,
plus in the same code itself you are giving a name for your
30 lines. 30 lines plus name, 31 lines.
So finally you're writing 34 lines instead of 90 lines.
[no audio]
So you almost reduced 60, 56 lines, right?
So you are able to reduce your length of the code.
Let me give a simple example.
You can easily understand that. Suppose
I have a small requirement. The purpose of requirement,
I mean the requirement is, whenever I run my Python script,
it has to clear my command line, then it has to display the
list of files and directories.
Right. See that I am writing
[no audio]
something like,
I need to run commands like 'cls', suppose Windows,
I need to run 'cls' command, and then 'dir' command. Right.
So, to run your commands, you have two options in your Python.
'os.system()', and then sub-process module.
Of course, you are also having 'os.popen', process open,
but anyway, 'os.system()', and sub-process are the best
[no audio]
process to run your commands. And whenever you want to
capture the output, then go with the sub-process. Just simply
if you want to run the command, just go with 'os.system()'.
That's why here my intention is,
just I want to run the command.
So, I am importing 'os'. So simply 'os.system("cls")' I am running. Then
'os.system()', what I am running, 'dir' command, that's it.
Now, let me go to command line. And then, so I have given script
name as 'run_ your_commands.py'. Now let me run it. Yes.
It's clearing your screen and it is displaying something.
But I want to display something like 'print("Please wait")'.
Actually, no need of this, just for clarity
I'm giving number of lines. "Please wait.
[no audio]
Cleaning the screen". That's it.
Then after this, 'print(
"Please wait. Finding the list of directories and files")'.
[no audio]
That's it. Now, let me run it.
[no audio]
Yes, you are getting. But after displaying, "Please wait
finding the list of directories and files",
I want to wait for 2 seconds before executing the next line,
I mean before executing this line. The same way
after printing this line
I want to wait for 2 seconds before executing this
'os.system()', so that I can see this information.
Otherwise, this information is also going to clean on your command line.
So, to wait for sometime you have a 'time' module.
Anyway, we'll discuss about 'time' module in detail.
[no audio]
So, what I am doing is, after displaying this
messages, I don't want to go and execute next line,
I want to wait for 2 seconds.
I want to stop your process with your Python. Then simply
'time.sleep(2)' seconds I want to wait.
The same way after displaying this message,
I want to wait again suppose, sorry 'time.sleep(2)' seconds.
Right. Now, let me run it and see the output.
So, it's waiting 2 seconds. Then after that, it is waiting
again 2 seconds. Then you're getting your data.
That's fine.
Suppose if I copy this code, and if I try to run this on my
Unix-like system,
[no audio]
Right. Let me write simply 'run_your_commands.py' script.
So same script I am copying and pasting here.
Will it work? See the result. So I want to run using Python 3.
[no audio]
See, 'cls' command is not working, because 'cls' is not the
command to clear your command line of your Unix-like system.
'clear' is the command, right?
And anyway 'dir' is working. But I want to get in detail,
in this way I want to get. Then you have to use 'ls - lrt'.
So, on Windows you have 'cls' and 'dir'. For Unix
I need 'clear' and 'ls -lrt' commands.
So, what I am doing is, before running your commands
I'm trying to identify the platform. You know there is a 'platform' module.
And if you remember, we discussed already about this.
Right. "Windows". So, if 'platform.system() == "Windows",
then I want to run this logic.
[no audio]
See the logic.
[no audio]
That's it. 'else', if it is not Windows, what I am doing is, same logic
but difference in commands. See that.
Same logic, the only thing here 'clear', and here 'ls-lrt'.
Now, first let me save it.
So guys, how we designed our script?
It's like a platform independent script. It will execute on
your Windows, as well as Unix-like systems.
And what is finally your purpose? Clear your screen, then
list the directories and files in the current location. Yes.
Now I am running our script.
[no audio]
"Clearing your screen...". Wait 2 seconds. "Finding list of directories
and files." Then you are getting. Now,
what I will do is, I am going to copy this script to your
Unix-like system as well.
So before that, let me clean your old data. Now I am going
to open same script,
I am going to copy and paste the code which you have
on your Windows. Now same logic, same script I'm running now on
your Windows, sorry, Unix-like system.
Now it's working, see that.
That's fine.
But the problem is, if you observe your entire logic,
if you observe your entire logic,
see, if you clearly observe, this thing,
[no audio]
right, and this thing,
[no audio]
same logic you are repeating here and here. The only thing,
commands are different. This command, this command is different.
Windows, this command. Unix, this command. Windows 'dir' command,
Linux 'ls -lrt' but remaining logic is same no.
That means you are repeating same logic in two places.
As of now, only in two places, maybe in real time you
are going to repeat same kind of logic maybe 100 times,
or 10 times, or 5 times, based on your requirement.
[no audio]
As of now, I am repeating my logic in two places. Forget about
commands, logic is same no.
So, what I will do is, in my code itself right, this logic,
I will write somewhere with some name, with some name.
I am giving name for that suppose 'mycode'.
You can provide any name for that.
Okay, we will learn in detail about how to
define your name for your logic.
Everything we'll learn while going forward. For time being just
observe the logic.
[no audio]
That's it.
Now instead of, now forget about this 'cls',
I am taking that as 'cmd1', and I am taking this as
'cmd2'. Okay.
Don't worry why we are taking 'cmd1' and 'cmd2'.
We'll discuss in detail while going forward. But here my intention
is, if you are repeating the logic, why should we repeat that logic?
I don't want to repeat.
[no audio]
Right. See, actually whatever the logic you are repeating, that I removed.
Right. But this is the logic you are repeating, right,
that's why what is the name you have given for this logic?
This is the name you have given.
That name I will write here and here. That's it.
So that your logic is over, but how is it going to work?
We'll discuss it. 'mycode', then here I'm also writing 'mycode'.
Now internally Python will replace
in place of 'mycode' this entire logic.
Same code only, you are not disturbing anything.
The only thing, you are reducing the number of lines.
Instead of rewriting the same logic in different places, what we are
doing? We are writing in one place, and we are giving some
name for that. Whenever it is required in your code instead
of writing that entire logic simple
we're writing the name which you have given for your logic.
Right. So for this, I have given a name called 'mycode'.
That's what I am writing here and here.
Now internally have to assume that 'mycode' is nothing but
this entire lines you can assume in place of 'mycode', and
here also in place of 'mycode', we can assume entirely these lines.
So, finally you are not disturbing with your code. Right.
Whenever I'm working with Windows, I need to run something
like 'cls'. Don't worry how I am giving this syntax.
Okay. Just observe the concept. That's it.
We'll discuss in detail.
[no audio]
That's it. Let me save it. So guys what I am doing,
observe that. The logic which you are repeating, that logic
I have written somewhere, and I have given some name for that.
So, in your code previously wherever you are using this logic
in that place simply I am giving this name. That's it.
Now let me run it. If it is working perfectly then
the way which you used here, that's correct.
Right. Yes, it's working. See that.
That means, whatever the process we followed, that is correct.
So if we follow this process, what is the advantage? You are
going to reduce the number of lines, or in reverse way, you
are able to reuse this logic any number of times in your code.
That is the advantage. Wherever you need
there you don't need to write it no, simply
you are going to you use this name.
Reusability. Right. Now,
let me copy this code to your Unix-like systems as well.
There also I want to run it.
[no audio]
Let me copy and paste it.
Right. I'm going to run it.
So here also it is going to work perfectly. See that.
[no audio]
That's it.
Right. So guys, in case you use this concept, then this is called a function.
And you can reuse this function any number of times in your
code wherever it is required.
[no audio]
Right. So how to define, how to use them, all those things, we'll see.
[no audio]
Okay, and one more thing,
this is a function. Simply,
this is a function. You know function means some operation
is there with function. That's it. Suppose if you take
'f(x) = 3x + 2', 'f(x)' is a function, and its operation
is '3x + 2'. Now here 'mycode' is a function. That is nothing
but 'f(x)', if you compare with your mathematics. 'mycode'
is a function that is 'f(x)'. Right. 'mycode' is simply 'f', here
'cmd1', 'cmd2', assume that 'x'. Now based on
'x' you are getting your 'f(x)' value
by executing this '3x + 2' expression. The same way, based
on 'cmd1' and 'cmd2', you're getting final result for
your 'mycode', by executing this logic.
Right. See, if you are good with your mathematics,
it is very easy to compare. Just assume that '3x + 2' is nothing
but this part, 'mycode' is nothing but 'f',
and 'cmd1', 'cmd2' are nothing but 'x'.
So '3x + 2' depends on 'x' value. So based on
'x' value finally you are getting 'f(x)' value. The same way this
logic depends on 'cmd1' and 'cmd2'. Based on this
'cmd1' and 'cmd2', finally you are getting the result of 'mycode'.
That's why this is simply a function. Same as your mathematics
concepts. That is on paper. This is on memory. That's it.
You're executing with your Python software. That's it.
So this is a function. Right.
This is some variables as of now, but not variables,
we have some other name. We'll see that. In 'f(x)', 'f' is a function,
'x' is the variable. Same way,
this is a function in terms of 'cmd1' and 'cmd2'.
That's it.
Right. So finally, I want to give you the simple definition for
your function. Function is a block of code for some specific
operation. That operation may be anything
based on your requirement in your situation.
Right. Once if you define a function, you can reuse that. Any
number of times you can use it in your code wherever it is required.
Right. And a function executes only when it is called.
See that.
Let me remove this.
Don't worry how to call, we'll discuss that. So as of now just
I defined a function. Nowhere I am using this, right.
Am I using it here?
So, previously you where using here and here, 13th line, 15th line.
Suppose if I comment this code, nowhere you are using that.
If you are not using, then if you run your code, it won't
execute. Even, let me run it on your command line,
then you will get a clarity.
[no audio]
Right. It's not executing. Even though if you have a function,
it won't execute by itself.
If you want to execute a function, you should use that
somewhere in your code. That's it.
That is the concept of your function. Okay.
So guys, this is simply introduction part with your functions, okay.
In later videos we will go with step-by-step,
and we will learn completely about your functions. Right.
And then we will see how to use in real-time
for different scenarios, okay.
Okay guys, thank you for watching this video.
[no audio]