WEBVTT

00:00.000 --> 00:13.920
Hi everyone, in this video we are going to learn the if statement in Python.

00:13.920 --> 00:16.400
So this is the syntax for the if statement.

00:16.400 --> 00:20.760
We write the key word if, followed by the condition and a full colon.

00:20.760 --> 00:23.160
And under it, we can write the statements.

00:23.160 --> 00:27.280
And if the condition is satisfied, then the statements here will be executed.

00:27.280 --> 00:29.440
Otherwise, it won't be executed.

00:29.440 --> 00:33.800
Now, let us see an example program which performs division operation.

00:33.800 --> 00:36.480
And we know that division by zero is not possible.

00:36.480 --> 00:40.480
And if we try to divide by zero, we will get a zero division error.

00:40.480 --> 00:41.960
So we have to restrict it.

00:41.960 --> 00:45.640
So we can use if statement to make sure that the denominator is never zero.

00:45.640 --> 00:47.720
So see here we have the code.

00:47.720 --> 00:53.080
Here we ask the user to enter, numerator and denominator, which are stored to num1 and

00:53.080 --> 00:55.360
num2 respectively.

00:55.360 --> 01:00.480
Those user enters 10 and 2, so these numbers are stored to num1 and num2.

01:00.480 --> 01:05.640
Now we have the if statement, num2 not equal to zero is the condition.

01:05.640 --> 01:12.080
And here num2 equal to 2, which is not equal to zero, so the condition is true.

01:12.080 --> 01:17.360
And the statement inside will be executed, and num1 divided by num2 will be calculated

01:17.360 --> 01:20.280
which is 10 by 2 equal to 5.

01:20.280 --> 01:22.000
And it will be printed.

01:22.000 --> 01:26.360
Now see another case, where the user enters the integers 10 and 0.

01:26.360 --> 01:30.240
So we have to perform 10 divided by zero, which is not possible.

01:30.240 --> 01:33.240
So the if statement actually blocks that operation here.

01:33.240 --> 01:35.720
Say the condition, it is false here.

01:35.720 --> 01:41.960
Num2 is equal to zero, so it skips the inner statements and, and this is how we can use,

01:41.960 --> 01:44.080
if statement in many cases.

01:44.080 --> 01:47.400
So see a real time example to understand it more clearly.

01:47.400 --> 01:49.160
If rainy, take umbrella.

01:49.160 --> 01:52.200
So the condition here is, it is raining.

01:52.200 --> 01:55.520
And if the condition is true, you want to take the umbrella.

01:55.520 --> 01:57.800
And if the condition is false, then you don't want to.

