1 00:00:00,200 --> 00:00:00,860 Hello everybody. 2 00:00:00,960 --> 00:00:03,090 And welcome back to the Python basics. 3 00:00:03,090 --> 00:00:10,140 Now right now we cover all of the things but we still do have a few things left such as functions in 4 00:00:10,140 --> 00:00:11,120 this video. 5 00:00:11,160 --> 00:00:16,140 So right now we will I will show you how you can create simple functions in order to automate some of 6 00:00:16,140 --> 00:00:17,310 the processes. 7 00:00:17,310 --> 00:00:23,250 Now you can create functions for basically anything such as printing something adding something calculating 8 00:00:23,250 --> 00:00:26,150 something or basically anything you want. 9 00:00:26,160 --> 00:00:32,580 Now right now we will use some of the simple functions but let's actually try to implement the function 10 00:00:32,640 --> 00:00:35,460 on a program that we already coded before. 11 00:00:35,460 --> 00:00:41,250 So let me just find that if I just type your less Python Wanda by let me find it. 12 00:00:41,250 --> 00:00:42,840 It is not that one. 13 00:00:42,840 --> 00:00:43,530 It is this one. 14 00:00:43,530 --> 00:00:48,330 So let's actually use a function which will actually automate this process. 15 00:00:48,330 --> 00:00:52,090 So now I turn to the PI. 16 00:00:52,320 --> 00:00:58,870 Right now we can actually put this in the function and we can call that function with a simple line. 17 00:00:58,890 --> 00:01:03,380 So first of all how do we actually declare a function. 18 00:01:03,630 --> 00:01:09,090 You declare a function with basically three letters which is death and then the name of the function 19 00:01:09,090 --> 00:01:09,600 itself. 20 00:01:09,630 --> 00:01:17,130 So what we want to do is after this rescue that so we can declare a function right here. 21 00:01:17,260 --> 00:01:21,730 Since this part of the code will be going in our function. 22 00:01:21,730 --> 00:01:27,130 So every time we want to use it we only specify the name of the function and then the number in brackets 23 00:01:27,490 --> 00:01:31,570 and we will be able to perform all of this with just that. 24 00:01:31,600 --> 00:01:35,130 So let's start off by actually declaring over function. 25 00:01:35,140 --> 00:01:42,310 So we as I said we need to take a death which declares a function and then right now we want to type 26 00:01:42,310 --> 00:01:44,220 the name of our function. 27 00:01:44,260 --> 00:01:52,930 So that would be the you can name it basically anything you want let's name it the activity or something 28 00:01:52,930 --> 00:01:53,320 like that. 29 00:01:53,350 --> 00:01:58,560 It doesn't even matter as we can see the name of the function cell is different colors. 30 00:01:58,570 --> 00:02:06,220 So that's also how you know that you are correct the correct me using syntax and in the brackets you 31 00:02:06,280 --> 00:02:08,800 can put something you don't have to. 32 00:02:08,830 --> 00:02:16,600 Basically no function we must put something so this function can have a use of it and we will put basically 33 00:02:18,100 --> 00:02:19,500 any number. 34 00:02:19,570 --> 00:02:22,530 Now you might be asking when needed to declare any number anywhere. 35 00:02:22,540 --> 00:02:28,690 Well this is this is something that you will provide to this function later on you do not need to declare 36 00:02:28,690 --> 00:02:28,990 this. 37 00:02:28,990 --> 00:02:30,910 This is not any variable. 38 00:02:30,910 --> 00:02:35,080 What you will declare is this variable light right here afterwards. 39 00:02:35,080 --> 00:02:40,150 So it will basically use this variable throughout the function itself. 40 00:02:40,150 --> 00:02:44,710 So if you just type your any number you will need to change all of this as well. 41 00:02:44,710 --> 00:02:54,540 So any number and this to any number and also the next thing you want to do is that all of this one 42 00:02:54,540 --> 00:02:55,140 time. 43 00:02:55,500 --> 00:03:00,500 So everything we typed just tab it so it belongs to the function. 44 00:03:00,510 --> 00:03:04,320 And right now we basically finished creating our own function. 45 00:03:04,590 --> 00:03:11,640 So what this will do is once we call it with any number in the brackets it will perform the this task 46 00:03:11,640 --> 00:03:13,410 right here for any number. 47 00:03:13,410 --> 00:03:15,650 So we asked for no input. 48 00:03:15,650 --> 00:03:19,020 And right now in order to call that function we call it like this. 49 00:03:19,050 --> 00:03:27,780 So activity loops which is the name of our function and then we specify the number we want to perform 50 00:03:27,780 --> 00:03:33,270 this task on which in our case is the user input number which is under the label number. 51 00:03:33,270 --> 00:03:35,450 So we just specify here number. 52 00:03:35,700 --> 00:03:37,200 And that's basically it. 53 00:03:37,200 --> 00:03:41,430 This is the entire function Britain for a certain number. 54 00:03:41,430 --> 00:03:45,370 So let's actually recap what this will do. 55 00:03:45,370 --> 00:03:48,240 So we named it we declared it. 56 00:03:48,250 --> 00:03:50,500 We also put something in it. 57 00:03:50,530 --> 00:03:57,460 So it only takes one argument which is any number in our case you could have named it anything basically 58 00:03:57,490 --> 00:04:03,040 but you would have to change all of this as well to match the name of this in the brackets. 59 00:04:03,040 --> 00:04:08,330 So after that we actually just use the code from the previous video. 60 00:04:08,320 --> 00:04:11,900 So this is just to check if number zero positive or negative. 61 00:04:11,950 --> 00:04:17,620 And basically after that we called that function on our user input number. 62 00:04:17,650 --> 00:04:21,460 So let's see how that will work. 63 00:04:21,460 --> 00:04:28,420 So control 0 2 safe control extra exits and let's run by tend to once again enter into number three 64 00:04:29,080 --> 00:04:30,640 given number is positive. 65 00:04:30,670 --> 00:04:37,490 So enter number zero given number zero and minus five or minus three given number is negative. 66 00:04:37,540 --> 00:04:40,170 So you can see that this function does work. 67 00:04:40,210 --> 00:04:47,710 We just called it with this and you can call it basically anything you want you can call it right here. 68 00:04:47,710 --> 00:05:00,030 Activity basically of three activity or minus three loops and activity of zero. 69 00:05:00,550 --> 00:05:03,550 And let's delete this so it doesn't asks for. 70 00:05:03,660 --> 00:05:09,490 For this we can also put this under a hash that is also a useful thing to know. 71 00:05:09,490 --> 00:05:15,600 So basically a hashtag will perform or will actually not be read as a code. 72 00:05:15,610 --> 00:05:19,020 The everything that comes after the hashtag equal put it as a comment. 73 00:05:19,030 --> 00:05:22,480 So everything after this will not be executed by Python. 74 00:05:22,870 --> 00:05:28,900 So right now we only have a function and I basically just written here three numbers that will perform 75 00:05:29,200 --> 00:05:30,190 all of this on them. 76 00:05:30,190 --> 00:05:31,420 So first of all really positive. 77 00:05:31,420 --> 00:05:33,040 And the second one will be negative. 78 00:05:33,400 --> 00:05:36,130 And the third one will be zero. 79 00:05:36,130 --> 00:05:38,680 So let's actually see how that works. 80 00:05:38,680 --> 00:05:45,460 So control 0 to save control x2 exits and then we run the program once again as we can see given number 81 00:05:45,450 --> 00:05:48,220 is positive given number is negative gearing number zero. 82 00:05:48,220 --> 00:05:50,280 Let me just add the end right here. 83 00:05:50,410 --> 00:05:53,320 Since it doesn't put it. 84 00:05:53,320 --> 00:06:02,260 So given let's say this once again so that is simple use of a function. 85 00:06:02,250 --> 00:06:07,230 Now let's actually make another function that will actually do something with the string. 86 00:06:07,230 --> 00:06:13,180 So we did something with numbers let's actually do something that will include strings as well. 87 00:06:13,350 --> 00:06:17,770 So let's now know Python 5 the pipe. 88 00:06:18,450 --> 00:06:21,840 Let's add that back to our Python folder. 89 00:06:21,900 --> 00:06:27,710 Now right now let's first of all take an input of a string so we want to type string. 90 00:06:28,530 --> 00:06:34,260 We will put it in a variable we will name its like that so raw input in order to take the input from 91 00:06:34,260 --> 00:06:35,480 a user. 92 00:06:35,880 --> 00:06:41,260 And basically type any string you want. 93 00:06:43,040 --> 00:06:51,180 And right now basically the user will get this input to specify anything it wants and we know right 94 00:06:51,200 --> 00:06:53,230 now let's see what we can do with it. 95 00:06:53,300 --> 00:06:58,230 Let's actually try to reverse that sentence or any string that the user inputs. 96 00:06:58,250 --> 00:06:59,560 So let's do that. 97 00:06:59,630 --> 00:07:07,790 We will be reversing everything a user input so we make a function for that of course we will not be 98 00:07:07,790 --> 00:07:09,090 doing it without the function. 99 00:07:09,140 --> 00:07:15,680 This is a medium for functions defined and we can call it anything we want so let's call it reverse 100 00:07:15,980 --> 00:07:16,930 string. 101 00:07:17,670 --> 00:07:19,610 So it's long name but it doesn't matter. 102 00:07:19,610 --> 00:07:23,180 So let's call it reverse underscore string so it looks a little bit better. 103 00:07:23,840 --> 00:07:31,190 And then here we specify more we name it anything also and a string as in the previous video we can 104 00:07:31,190 --> 00:07:32,940 name it anything we want right here. 105 00:07:33,170 --> 00:07:38,360 But make sure that this and a string that you use in the rest of your function call so we tab right 106 00:07:38,360 --> 00:07:42,020 now in order to perform the next code for our function. 107 00:07:42,020 --> 00:07:51,890 And the only thing we want to do is use the simple python print statement so print and a string then 108 00:07:51,950 --> 00:07:57,560 open these brackets then two times type these two dots and then minus one. 109 00:07:57,560 --> 00:08:04,830 This right here will basically reverse anything they type it will print us everything in reverse order. 110 00:08:04,880 --> 00:08:07,700 So this is the entire function. 111 00:08:07,700 --> 00:08:13,040 Basically just with this simple print statement we can do that and all we have to do right now is called 112 00:08:13,040 --> 00:08:21,890 the reverse string reverse underscore string and we call it on our user input string so which is called 113 00:08:21,950 --> 00:08:22,370 string 114 00:08:25,370 --> 00:08:27,810 so we control all enter. 115 00:08:27,860 --> 00:08:35,150 Now basically this might be a little bit confusing to you if you are an ultimate beginner but make sure 116 00:08:35,150 --> 00:08:38,630 to actually go around and check some other things as well. 117 00:08:38,780 --> 00:08:43,940 For Python since people really make you better understand what we will be doing later on. 118 00:08:44,090 --> 00:08:50,250 So this is just something basic that you need to know in order to able to be able to follow later on 119 00:08:50,250 --> 00:08:50,990 what we will be doing. 120 00:08:50,990 --> 00:08:53,440 So just wanted to say that. 121 00:08:53,450 --> 00:08:54,990 So let's call this right now. 122 00:08:55,010 --> 00:08:55,760 We sent it. 123 00:08:56,420 --> 00:09:04,340 We LSC in order to make our Python five an executable and right now we run Python five. 124 00:09:04,390 --> 00:09:06,800 It will say type anything we want. 125 00:09:06,860 --> 00:09:14,990 We type here for example I know Bert and we can see that it actually prints off the reverse order of 126 00:09:14,990 --> 00:09:16,730 the word of the word. 127 00:09:17,540 --> 00:09:21,800 Let's try something else so hello world. 128 00:09:21,800 --> 00:09:23,750 We can see it reversed all of that. 129 00:09:23,750 --> 00:09:25,010 So even two words can work. 130 00:09:25,010 --> 00:09:32,210 So hello world light here and that is basically our simple use of a function that actually does something 131 00:09:32,300 --> 00:09:40,690 with the strings so let's actually do another function that will include lists for example. 132 00:09:40,700 --> 00:09:47,230 So let's use the same file so we will just delete our previous code right here. 133 00:09:48,500 --> 00:09:50,070 So we don't create a new file. 134 00:09:50,070 --> 00:09:52,310 This doesn't really matter. 135 00:09:52,310 --> 00:09:53,930 This is a simple code anyway. 136 00:09:55,220 --> 00:09:57,950 Now let's actually first of all create a list. 137 00:09:57,960 --> 00:10:06,410 So numbers number lists equals and then we specify one come up to a common treatment for common five. 138 00:10:06,440 --> 00:10:09,710 So this is our list containing these five numbers. 139 00:10:09,710 --> 00:10:16,180 Now let's actually create a function that will for example print. 140 00:10:16,480 --> 00:10:18,560 If the number is odd or even 141 00:10:22,360 --> 00:10:27,820 or even better let's actually try to write a function that will print the number of odd numbers in the 142 00:10:27,820 --> 00:10:30,460 list and the number of even numbers in the list. 143 00:10:30,970 --> 00:10:39,460 So let's call it deaths for example or even on underscore even doesn't even matter. 144 00:10:39,940 --> 00:10:43,270 And what we want to specify right here is any list. 145 00:10:43,270 --> 00:10:47,240 So we'll just call it lists since it doesn't really matter. 146 00:10:47,290 --> 00:10:53,700 You can call the analyst you want but make sure to use this name in the brackets in the rest of the 147 00:10:53,740 --> 00:10:56,120 day or in the rest of the function code. 148 00:10:56,210 --> 00:11:05,660 We specified two local variables which is count even numbers and we said that two to zero. 149 00:11:06,110 --> 00:11:09,930 And also count odd numbers. 150 00:11:10,370 --> 00:11:12,200 And we will set that to zero as well. 151 00:11:12,230 --> 00:11:14,600 So we said basically two variables. 152 00:11:14,600 --> 00:11:16,810 One of them will count our even numbers. 153 00:11:16,820 --> 00:11:19,610 And one of them will count our old numbers. 154 00:11:19,610 --> 00:11:24,710 So right now let us for first of first time use the for loop in our function. 155 00:11:24,710 --> 00:11:29,900 So for Tau I for example in list. 156 00:11:30,170 --> 00:11:34,260 So for I in the list that we specified in our function. 157 00:11:34,490 --> 00:11:45,770 Basically if and then we open brackets if the AI module the two equals equals zero then our number is 158 00:11:45,770 --> 00:11:47,030 basically even. 159 00:11:47,180 --> 00:11:54,170 So we want to add one to the count even number since we found one even number so we can do that with 160 00:11:54,290 --> 00:12:01,800 count even numbers plus equals 1. 161 00:12:01,820 --> 00:12:06,590 This is basically the shorter version of equals counting the numbers plus 1. 162 00:12:06,590 --> 00:12:12,740 So we can just type your plus equals 1 which will just add one to the previous value of even numbers 163 00:12:13,860 --> 00:12:15,760 so and then we want else. 164 00:12:15,870 --> 00:12:22,470 So else pops you can see right here I forgot to specify two dots. 165 00:12:22,500 --> 00:12:24,430 So this code will get an error. 166 00:12:24,480 --> 00:12:29,640 So right now we want one what we want to specify is else and we can just do the same with odd numbers 167 00:12:29,640 --> 00:12:35,210 so count odd numbers as equals 1. 168 00:12:35,220 --> 00:12:39,700 So if the number isn't even equal add one to the odd numbers. 169 00:12:39,720 --> 00:12:43,050 If the number isn't even it will add 1 to the even numbers. 170 00:12:43,050 --> 00:12:47,430 So we can see how many even and how many odd numbers there are. 171 00:12:47,430 --> 00:12:50,330 And we can do that with basically a simple print statement at the end. 172 00:12:50,330 --> 00:12:54,330 So this will finish the loop for every number in the list. 173 00:12:54,330 --> 00:13:06,330 And right now we can print for example the number or even numbers is that we specified the percent D 174 00:13:07,020 --> 00:13:14,520 because the quotes and then percent count even numbers. 175 00:13:14,520 --> 00:13:24,750 And then we also do the same so print the number looks a number of numbers is percent D and then percent 176 00:13:24,990 --> 00:13:28,390 count odd numbers. 177 00:13:29,460 --> 00:13:37,710 So it is rather simple as we can see right here it will just print out the number of even a number of 178 00:13:37,710 --> 00:13:43,890 our numbers and all we have to do right now is call the function on our already pre specified list right 179 00:13:43,890 --> 00:13:44,070 here. 180 00:13:44,070 --> 00:13:48,070 So odd even out of nonetheless. 181 00:13:48,070 --> 00:13:52,240 So number on this call list and let's see how this will work. 182 00:13:52,230 --> 00:13:56,540 So percent x we already have it specified as an executable. 183 00:13:56,550 --> 00:13:58,690 And right now we just type your python five. 184 00:13:58,690 --> 00:13:59,250 The PI. 185 00:13:59,730 --> 00:14:06,110 So the number of even numbers is two and the number of odd numbers is three which is correct. 186 00:14:06,120 --> 00:14:10,350 So that would be about it for the simple use of functions. 187 00:14:10,350 --> 00:14:13,770 This is something very useful and this something that we will use. 188 00:14:13,800 --> 00:14:20,820 So make sure to fully understand this look at some other of this if you want to and make sure to actually 189 00:14:21,360 --> 00:14:26,280 understand everything that we do in this python crash course it will be reported later on. 190 00:14:26,730 --> 00:14:29,760 So that be about it for this Collier tutorial. 191 00:14:29,760 --> 00:14:32,370 I will see you in the next one and take care. 192 00:14:32,430 --> 00:14:32,700 Bye.