1 00:00:00,330 --> 00:00:02,190 Hello everybody and welcome back. 2 00:00:02,230 --> 00:00:07,400 And let us continue with our Python basics in this video we will cover the if loop. 3 00:00:07,500 --> 00:00:14,190 Now if lope is basically the most simple one I would say it basically just does the same as it says. 4 00:00:14,190 --> 00:00:18,880 So if something then do that and else do something else. 5 00:00:19,020 --> 00:00:21,420 So I will explain a little bit better. 6 00:00:21,420 --> 00:00:22,080 Don't worry. 7 00:00:22,080 --> 00:00:28,290 For now on let us open up our second program and let us see how we can actually use the if loop. 8 00:00:28,620 --> 00:00:32,490 So for that first of all let's open the interpreter. 9 00:00:32,490 --> 00:00:35,210 So just wrap your python and you will see this. 10 00:00:35,340 --> 00:00:42,510 Let's say we want to write the most simple if statement that we can write. 11 00:00:42,510 --> 00:00:45,070 So for example a equals three. 12 00:00:45,180 --> 00:00:46,250 This is simple to understand. 13 00:00:46,260 --> 00:00:54,450 We stored value of three to eight and then if a equals equals three then two dots. 14 00:00:54,450 --> 00:00:59,020 Now what this basically does the two equal sign checks. 15 00:00:59,040 --> 00:01:01,140 If the value of A is three. 16 00:01:01,230 --> 00:01:08,940 So if a really is three you can't really specify just one percent sign it will not work since 1 percent 17 00:01:08,940 --> 00:01:12,830 sine means that you are actually giving the value of three to a. 18 00:01:12,830 --> 00:01:14,460 You need to specify two percent. 19 00:01:14,490 --> 00:01:14,980 Pardon me. 20 00:01:14,980 --> 00:01:17,430 Two equal sign in order to check. 21 00:01:17,460 --> 00:01:19,950 Two in order to perform a check on a. 22 00:01:19,980 --> 00:01:21,420 If a really is three. 23 00:01:21,840 --> 00:01:30,630 So if a equals three we will do what we will actually print a is three. 24 00:01:30,630 --> 00:01:38,910 So we print a three and then we can add an or let's actually just leave it on this so we can see if 25 00:01:38,910 --> 00:01:42,030 a equals three print a is three. 26 00:01:42,090 --> 00:01:45,100 And as we can see it printed out a is three. 27 00:01:45,330 --> 00:01:46,950 But let's actually switch it up. 28 00:01:46,950 --> 00:01:52,930 So if a equals four print k is four. 29 00:01:52,950 --> 00:01:59,790 And if we press twice enter you can see it didn't print out anything since A is in four we specified 30 00:01:59,880 --> 00:02:04,950 eight eight to be three in our variable specification. 31 00:02:05,020 --> 00:02:13,810 So this is the most simple use of IF statement what you can do is basically make an if statement which 32 00:02:13,810 --> 00:02:17,750 will actually perform an action if a equals 4. 33 00:02:17,770 --> 00:02:21,650 And it will perform a different action if a isn't equal 4. 34 00:02:21,670 --> 00:02:30,810 So let me actually clear the screen so we can do that so all those dots system clear. 35 00:02:32,290 --> 00:02:33,880 And let's actually do the same thing. 36 00:02:33,880 --> 00:02:40,310 So a equals for example now five so we have a to be set to five. 37 00:02:40,400 --> 00:02:43,910 And right now we want to write our first if else statement. 38 00:02:44,180 --> 00:02:53,440 So if k equals equals five we hear habit and print. 39 00:02:53,730 --> 00:02:56,000 Then we want to print a is five. 40 00:02:56,420 --> 00:02:59,800 So this will only be printed out if a release five. 41 00:03:00,080 --> 00:03:08,270 And what you want to do right here else and then to dot sign and then enter once again tab print a isn't 42 00:03:08,280 --> 00:03:18,050 five a is some different number for example and press enter. 43 00:03:18,050 --> 00:03:24,590 And now if you press Enter second time pops you will see that we get a is five printed out. 44 00:03:24,740 --> 00:03:26,980 Since that is true a release is five. 45 00:03:26,990 --> 00:03:31,820 And it didn't even consider this statement since it got this one to be true. 46 00:03:31,850 --> 00:03:35,200 So if a equals five print a is five. 47 00:03:35,270 --> 00:03:38,900 And we got printed out a equal or a five. 48 00:03:38,960 --> 00:03:45,800 Now in the syntax syntax wise you need to make sure to add after if statement and also after L statement 49 00:03:46,160 --> 00:03:50,830 you need to add these two dots right here since it will not work. 50 00:03:50,870 --> 00:04:00,410 If you do not add that so make sure to double check that before you run your program and let's actually 51 00:04:00,410 --> 00:04:07,370 run our first or not first our second program has a narrow window so as our as our real program and 52 00:04:07,370 --> 00:04:12,770 not a program in interpreter or Python interpreter so unless we have our first program with this type 53 00:04:12,770 --> 00:04:19,250 Python 2 that by which is our second program as we remember our previous video we need to specify the 54 00:04:19,250 --> 00:04:23,140 path to Python so user been python. 55 00:04:23,780 --> 00:04:26,670 And after this we can actually run our code. 56 00:04:26,750 --> 00:04:32,570 So let's actually run a code that will check out if number is odd or even which is simple we will use 57 00:04:32,600 --> 00:04:33,980 and if else statement. 58 00:04:34,310 --> 00:04:40,430 So let's actually type your number one which is our or actually just name one number we need only one 59 00:04:40,430 --> 00:04:41,430 number. 60 00:04:41,450 --> 00:04:51,410 Let's use the input raw input function which will give our users of code are a choice to specify any 61 00:04:51,410 --> 00:04:57,680 number so enter any puts any number two dots closed. 62 00:04:57,680 --> 00:05:03,890 Double quotes then we closed two brackets right here and let's write an if Al statement right here. 63 00:05:03,890 --> 00:05:10,760 So what we want to do is if open up our brackets No. 64 00:05:11,450 --> 00:05:12,820 Module 2. 65 00:05:12,890 --> 00:05:15,850 Now this percent sign stands for module. 66 00:05:16,040 --> 00:05:23,840 Basically we use this in order to check out if our number is evil or it Your if it is odd if it has 67 00:05:23,840 --> 00:05:25,520 any remaining after the division. 68 00:05:25,520 --> 00:05:27,050 So this sign right here. 69 00:05:27,050 --> 00:05:31,940 Make sure to remember that this sign is module we will not be using it that much but it is still a good 70 00:05:31,940 --> 00:05:32,810 thing to know. 71 00:05:32,810 --> 00:05:34,190 And then equals equals zero. 72 00:05:34,190 --> 00:05:41,960 So if the Remain is zero then we need to specify that the number is even if it is not zero the number 73 00:05:41,990 --> 00:05:42,790 will be odd. 74 00:05:42,800 --> 00:05:48,920 So if we just type here two dots or remember to put these two dots right here enter and then tab it 75 00:05:48,950 --> 00:05:54,130 so always tab it in order to make sure that your code looks a little bit prettier. 76 00:05:54,140 --> 00:06:01,070 And in python I believe you cannot really write an if statement under the line in the same order you 77 00:06:01,070 --> 00:06:02,210 need to tab it in. 78 00:06:02,210 --> 00:06:03,320 So it knows. 79 00:06:03,320 --> 00:06:07,040 So the python knows that this belongs to the if statement. 80 00:06:07,040 --> 00:06:09,820 So right here we want to print. 81 00:06:10,070 --> 00:06:11,850 Number is even. 82 00:06:13,610 --> 00:06:20,600 And we want to specify here else which will happen in any other cases that the number isn't even which 83 00:06:20,600 --> 00:06:22,460 is the only case that the number is odd. 84 00:06:22,460 --> 00:06:23,320 So we want to print. 85 00:06:23,360 --> 00:06:24,920 Else print. 86 00:06:25,430 --> 00:06:29,260 Number is odd. 87 00:06:29,260 --> 00:06:31,790 And then we close the double quotes. 88 00:06:31,850 --> 00:06:35,540 So here we can see this is a simple program as well. 89 00:06:35,540 --> 00:06:42,640 So we perform the module and then we actually print if number is even or if number is odd. 90 00:06:42,710 --> 00:06:46,310 So let's control all this central control acts. 91 00:06:46,310 --> 00:06:49,670 Let's make the program to be executable with this command. 92 00:06:49,670 --> 00:06:51,670 So Python 2 the PI. 93 00:06:51,940 --> 00:06:55,730 And right now we can see it's negative mutable and let's run it. 94 00:06:55,730 --> 00:07:02,810 We hear we asked for a number we specify two it will say number is even if we just up here not Python. 95 00:07:02,990 --> 00:07:08,110 If we run once again specify three it will say number is odd which is correct. 96 00:07:08,960 --> 00:07:18,530 So that the simple use of R Python 3 scripts or PI pardon me of our Python 2 script with the if else 97 00:07:18,860 --> 00:07:19,820 statement. 98 00:07:20,000 --> 00:07:28,370 Now there is also another thing that you should know which also we will use is the L if statement which 99 00:07:28,370 --> 00:07:36,920 basically allows you to specify multiple statements and not just two so let's edit our code now let's 100 00:07:36,920 --> 00:07:38,080 actually write a different code. 101 00:07:38,090 --> 00:07:44,510 We will have three different statements so let's delete these two statements right here. 102 00:07:44,600 --> 00:07:51,620 We will leave the number to be here as a variable and we will leave the user to specify which number 103 00:07:51,650 --> 00:07:52,630 it will be. 104 00:07:52,640 --> 00:07:57,820 So let's see if no let's specify open brackets. 105 00:07:57,830 --> 00:08:02,330 If number equals equals to zero type here. 106 00:08:04,770 --> 00:08:12,800 Paw print number four given number is zero. 107 00:08:14,190 --> 00:08:17,860 And then we basically just go into the next statement. 108 00:08:17,880 --> 00:08:24,000 The next statement will be out if and then we need to specify an understatement right here. 109 00:08:24,120 --> 00:08:38,910 If so how if number is bigger than zero then again two dots print not give a number is positive. 110 00:08:40,610 --> 00:08:44,210 And then we call this double quotes and then the last statement will just be. 111 00:08:44,210 --> 00:08:45,040 Else. 112 00:08:45,120 --> 00:08:46,350 So in the other cases. 113 00:08:46,350 --> 00:08:50,520 So any other cases would be the cases where number is not bigger than zero. 114 00:08:50,520 --> 00:08:53,640 And the now and the one number is not equal to zero. 115 00:08:53,640 --> 00:09:00,570 So any other case would be print given number is negative. 116 00:09:00,570 --> 00:09:06,990 And then we close the double quotes so control over to save and control X to exit and you will see that 117 00:09:06,990 --> 00:09:12,900 our program is still executable even though we edited it so we can actually run it right away so Python 118 00:09:12,900 --> 00:09:16,500 to perhaps Python to enter any number. 119 00:09:16,740 --> 00:09:20,100 Let's add three it will say a given number is positive. 120 00:09:20,100 --> 00:09:25,920 If we run it again we give it minus two given number is negative. 121 00:09:25,920 --> 00:09:28,330 So since minus two is a negative number. 122 00:09:28,350 --> 00:09:32,280 And then once again zero even number is zero. 123 00:09:32,670 --> 00:09:37,800 So you can specify as many if statements as you want. 124 00:09:37,800 --> 00:09:43,590 So for example if you need to cover five different statements you would be specifying first in the if 125 00:09:43,740 --> 00:09:47,460 the next three in the Elif and the last one in our statement. 126 00:09:47,520 --> 00:09:54,120 Make sure to get to have the two dots at the end of each once and make sure to specify these statements 127 00:09:54,180 --> 00:09:56,700 in given brackets it is easier. 128 00:09:56,700 --> 00:10:03,120 You can also do it without them but it is a good practice to actually specify them in the brackets. 129 00:10:03,120 --> 00:10:12,660 So that would be about it for these four or pardon me for this if statement video will cover the fourth 130 00:10:12,660 --> 00:10:16,460 statement in the in the next video and I hope I see you there. 131 00:10:16,560 --> 00:10:16,830 My.