Hello everybody.
And welcome back to the Python basics.
Now right now we cover all of the things but we still do have a few things left such as functions in
this video.
So right now we will I will show you how you can create simple functions in order to automate some of
the processes.
Now you can create functions for basically anything such as printing something adding something calculating
something or basically anything you want.
Now right now we will use some of the simple functions but let's actually try to implement the function
on a program that we already coded before.
So let me just find that if I just type your less Python Wanda by let me find it.
It is not that one.
It is this one.
So let's actually use a function which will actually automate this process.
So now I turn to the PI.
Right now we can actually put this in the function and we can call that function with a simple line.
So first of all how do we actually declare a function.
You declare a function with basically three letters which is death and then the name of the function
itself.
So what we want to do is after this rescue that so we can declare a function right here.
Since this part of the code will be going in our function.
So every time we want to use it we only specify the name of the function and then the number in brackets
and we will be able to perform all of this with just that.
So let's start off by actually declaring over function.
So we as I said we need to take a death which declares a function and then right now we want to type
the name of our function.
So that would be the you can name it basically anything you want let's name it the activity or something
like that.
It doesn't even matter as we can see the name of the function cell is different colors.
So that's also how you know that you are correct the correct me using syntax and in the brackets you
can put something you don't have to.
Basically no function we must put something so this function can have a use of it and we will put basically
any number.
Now you might be asking when needed to declare any number anywhere.
Well this is this is something that you will provide to this function later on you do not need to declare
this.
This is not any variable.
What you will declare is this variable light right here afterwards.
So it will basically use this variable throughout the function itself.
So if you just type your any number you will need to change all of this as well.
So any number and this to any number and also the next thing you want to do is that all of this one
time.
So everything we typed just tab it so it belongs to the function.
And right now we basically finished creating our own function.
So what this will do is once we call it with any number in the brackets it will perform the this task
right here for any number.
So we asked for no input.
And right now in order to call that function we call it like this.
So activity loops which is the name of our function and then we specify the number we want to perform
this task on which in our case is the user input number which is under the label number.
So we just specify here number.
And that's basically it.
This is the entire function Britain for a certain number.
So let's actually recap what this will do.
So we named it we declared it.
We also put something in it.
So it only takes one argument which is any number in our case you could have named it anything basically
but you would have to change all of this as well to match the name of this in the brackets.
So after that we actually just use the code from the previous video.
So this is just to check if number zero positive or negative.
And basically after that we called that function on our user input number.
So let's see how that will work.
So control 0 2 safe control extra exits and let's run by tend to once again enter into number three
given number is positive.
So enter number zero given number zero and minus five or minus three given number is negative.
So you can see that this function does work.
We just called it with this and you can call it basically anything you want you can call it right here.
Activity basically of three activity or minus three loops and activity of zero.
And let's delete this so it doesn't asks for.
For this we can also put this under a hash that is also a useful thing to know.
So basically a hashtag will perform or will actually not be read as a code.
The everything that comes after the hashtag equal put it as a comment.
So everything after this will not be executed by Python.
So right now we only have a function and I basically just written here three numbers that will perform
all of this on them.
So first of all really positive.
And the second one will be negative.
And the third one will be zero.
So let's actually see how that works.
So control 0 to save control x2 exits and then we run the program once again as we can see given number
is positive given number is negative gearing number zero.
Let me just add the end right here.
Since it doesn't put it.
So given let's say this once again so that is simple use of a function.
Now let's actually make another function that will actually do something with the string.
So we did something with numbers let's actually do something that will include strings as well.
So let's now know Python 5 the pipe.
Let's add that back to our Python folder.
Now right now let's first of all take an input of a string so we want to type string.
We will put it in a variable we will name its like that so raw input in order to take the input from
a user.
And basically type any string you want.
And right now basically the user will get this input to specify anything it wants and we know right
now let's see what we can do with it.
Let's actually try to reverse that sentence or any string that the user inputs.
So let's do that.
We will be reversing everything a user input so we make a function for that of course we will not be
doing it without the function.
This is a medium for functions defined and we can call it anything we want so let's call it reverse
string.
So it's long name but it doesn't matter.
So let's call it reverse underscore string so it looks a little bit better.
And then here we specify more we name it anything also and a string as in the previous video we can
name it anything we want right here.
But make sure that this and a string that you use in the rest of your function call so we tab right
now in order to perform the next code for our function.
And the only thing we want to do is use the simple python print statement so print and a string then
open these brackets then two times type these two dots and then minus one.
This right here will basically reverse anything they type it will print us everything in reverse order.
So this is the entire function.
Basically just with this simple print statement we can do that and all we have to do right now is called
the reverse string reverse underscore string and we call it on our user input string so which is called
string
so we control all enter.
Now basically this might be a little bit confusing to you if you are an ultimate beginner but make sure
to actually go around and check some other things as well.
For Python since people really make you better understand what we will be doing later on.
So this is just something basic that you need to know in order to able to be able to follow later on
what we will be doing.
So just wanted to say that.
So let's call this right now.
We sent it.
We LSC in order to make our Python five an executable and right now we run Python five.
It will say type anything we want.
We type here for example I know Bert and we can see that it actually prints off the reverse order of
the word of the word.
Let's try something else so hello world.
We can see it reversed all of that.
So even two words can work.
So hello world light here and that is basically our simple use of a function that actually does something
with the strings so let's actually do another function that will include lists for example.
So let's use the same file so we will just delete our previous code right here.
So we don't create a new file.
This doesn't really matter.
This is a simple code anyway.
Now let's actually first of all create a list.
So numbers number lists equals and then we specify one come up to a common treatment for common five.
So this is our list containing these five numbers.
Now let's actually create a function that will for example print.
If the number is odd or even
or even better let's actually try to write a function that will print the number of odd numbers in the
list and the number of even numbers in the list.
So let's call it deaths for example or even on underscore even doesn't even matter.
And what we want to specify right here is any list.
So we'll just call it lists since it doesn't really matter.
You can call the analyst you want but make sure to use this name in the brackets in the rest of the
day or in the rest of the function code.
We specified two local variables which is count even numbers and we said that two to zero.
And also count odd numbers.
And we will set that to zero as well.
So we said basically two variables.
One of them will count our even numbers.
And one of them will count our old numbers.
So right now let us for first of first time use the for loop in our function.
So for Tau I for example in list.
So for I in the list that we specified in our function.
Basically if and then we open brackets if the AI module the two equals equals zero then our number is
basically even.
So we want to add one to the count even number since we found one even number so we can do that with
count even numbers plus equals 1.
This is basically the shorter version of equals counting the numbers plus 1.
So we can just type your plus equals 1 which will just add one to the previous value of even numbers
so and then we want else.
So else pops you can see right here I forgot to specify two dots.
So this code will get an error.
So right now we want one what we want to specify is else and we can just do the same with odd numbers
so count odd numbers as equals 1.
So if the number isn't even equal add one to the odd numbers.
If the number isn't even it will add 1 to the even numbers.
So we can see how many even and how many odd numbers there are.
And we can do that with basically a simple print statement at the end.
So this will finish the loop for every number in the list.
And right now we can print for example the number or even numbers is that we specified the percent D
because the quotes and then percent count even numbers.
And then we also do the same so print the number looks a number of numbers is percent D and then percent
count odd numbers.
So it is rather simple as we can see right here it will just print out the number of even a number of
our numbers and all we have to do right now is call the function on our already pre specified list right
here.
So odd even out of nonetheless.
So number on this call list and let's see how this will work.
So percent x we already have it specified as an executable.
And right now we just type your python five.
The PI.
So the number of even numbers is two and the number of odd numbers is three which is correct.
So that would be about it for the simple use of functions.
This is something very useful and this something that we will use.
So make sure to fully understand this look at some other of this if you want to and make sure to actually
understand everything that we do in this python crash course it will be reported later on.
So that be about it for this Collier tutorial.
I will see you in the next one and take care.
Bye.