0:00
Welcome to chapter 4 that is control flow
0:03
I'll be explaining the control flow statements in Python program. So there are different control flow that we'll be seeing in order to statement will be executed at the runtime
0:15
So there are conditional statements, transfer statement and iterative statements. When I am talking about conditional statement, we have if, if else if else look or if else look
0:26
Our transfer statements are break, continue and pass and then we have a different statement. then we have a dual as a foreign buying loops so whenever we talk about if loop so there is an
0:35
if condition and then statement or if condition and there n number of statements so for example
0:42
if x equal to equal to print something right this is one of the if leave otherwise you can also have
0:52
print this print that so n number of statement right so in the condition is two statement will be
0:58
executed this is simple if now whenever there is an if else look so it will be if x
1:05
equal to equal to do do something right else do something else so in do you can do anything
1:15
can print or you can do any of the another operations similarly this will be an action
1:22
two so if condition is true then action one otherwise action two right
1:28
Otherwise, if, else, if loop. So it will be first if condition, action one, else condition, action two, else condition
1:40
else condition, action, and so on until last ends, which will execute and default action
1:47
So this is overall structure of if loop, so it will be in a if condition, if that condition
1:54
is true, we will execute a body, otherwise we are coming out of it
1:58
one of the example is if both A and B are having same values so in first we are
2:03
checking if B is greater than A this will be false because both are having same
2:08
values and then second one we are checking if else if a equal to
2:12
product of B this may be true so this particular statement will be in similar
2:16
here number 1 is 5 number 2 if number 1 is less than number 2 so that is true
2:23
this particular statement will be executed right so So you see this example first in our Jupiter notebook Here we have an input function
2:36
What this function will do? It will ask a user to enter something inside it
2:41
And this particular will be a message for a user. And then whenever you are doing taking an input, it will take input from user as a stream
2:50
So we need to convert it into integer. So with typecasting using I&T, you are converting
2:55
you are converting it to integer and then you are performing certain operation
2:59
this is um you are checking if it is divided by 2 and remainder is 0 that is even
3:04
otherwise it is odd so this is basically just to check even and odd if i am giving 25 as
3:10
input it should give me output an odd number right so number is odd otherwise if i am doing
3:15
again so if i am doing 28 this is even number right so number is even so this is an if i
3:21
this loop so this was an example with input function you can just view our user whatever value
3:29
you wanted to type inside it and then enter so that value will be stored inside that particular
3:36
identifier or a number right now we have a reduced statement if you wanted to execute group
3:42
of statement multiple times then we should go for it reduce statements for that we have for loops
3:48
and y u so first syntax is for loop how it starts is for x in sequence colon and do something
3:58
somebody so if one to execute some action for every element present in the sequence it may be
4:04
useful to use a for loop right sequence can be anything string or any collection so for example
4:13
you are starting the loop until the last element is met you are inside a body and performing operation on a next element
4:23
Once last element is there, you are coming out of your execution
4:27
So for example, we have a list with the values 1, 2, 1, 3
4:32
You are checking for value in list means for each element in list
4:36
You can also do, for example, we have list, we will do 1, 2, 3, 4
4:42
So now you are checking for each element in list, column, and you are just printing the element right so instead of value you can use
4:53
element I or anything you wanted to use so it may just take one value at a time and give you output as one two three and 4 right So this way you will use 4 look for while look
5:09
similar way you are just taking the condition while x is greater than 5
5:14
print x, and x equal to x plus 1. For example, this is an example, until an ysis x is less than 5
5:24
you are doing this operation once x is equal to well greater than 5 you are coming out of your loop so once the condition is true body will
5:35
be executed once the condition is false you're coming out of your execution right so we'll
5:41
see the example over here so these are the expenses of the month and you wanted to calculate
5:46
the addition of them so what you will do you will take a sum or a total as one of the
5:51
variable and you will iterate over all these elements of this list and then add them to this total
5:58
So for item in these expenses, that means first item is this, second in this and so on
6:03
You are just adding that item to total. So every time this will be added to 0 first and then it will be 2 340, then 2 340 plus next item
6:13
then all this addition plus next item and so on until and unless total is printed
6:18
So your total will be this much for this month. So in while you, similar way, I is 1 you are defined and then you are telling once the I is less than 5
6:28
or equal to 5, print I and increase the value of I
6:32
So for the loop, we'll have in between counter with it, you do not need to specify
6:38
the next value, but here you need to do this increment operation inside yLU, so that if
6:44
we meet to this particular logic, once it is meet, it will come out of the loom
6:49
So once it is equal to or less than 5, it is printing, once it is 5 or greater than 5
6:55
it is coming out of your flying. Right? So these are nothing but later this statement. Now we are talking about transfer statement. We have two statements, break and continuing. So what is the use of break? For example, you are inside a loop. You are checking something from this list. Here you have an example of garage, living room, chair and plus it. These are the four values inside your list. Now you are checking whenever your input, whatever the user is providing is matching to X, Y, Z value
7:27
here in this case you are matching with chair it should come out of your loop it should not go until the last element to check one by one that is example of break so for example whenever i equal to equal to key location what is key
7:42
location chair and what is i first time it will be garrets second time will be in third time to be chair
7:47
so third time when chair is mass every chair that we say key is formed in particular location
7:53
and if we not go until and check the closet because already we've got an output we are breaking the slow
7:59
That is the break statement. So now continuous statement, we can use a continuous statement to skip the current iteration
8:06
For example, you have the values from 1 to 8. So what is the range function in Python
8:11
It will give you values in between this range, starting with 0 and ending with 8 minus 1
8:18
That is 7. So it will print out or give the values from 0 to 7
8:23
If I am doing a range of 1 to 8 means, it will start with 1 and it will end with 3
8:29
So all the values in the universe will be given by this range function
8:34
So here I am just taking for I in range 1 to 8 where iMOT 2 equal to 0 continue
8:40
Whenever it is divided by 2, that means whenever it is an even number, continue
8:46
Do not do anything. Whenever it is an odd number, do multiplication I into I
8:51
First one will be 1 into 1. Then 2 is skip, third one will be 3 into 3
8:58
then 5 into 5 and so on until 7 into 7 right so this way you can use a continuous statement
9:06
and last one is pass statement whenever you just wanted to define some class or an object maybe
9:14
x x x but you not know the operations to be defined inside that class or object that moment you can just write
9:23
pass and that definition will become today and you can go to the next block of your code right
9:28
So that way we are using a transport segment. So this is an already example that I have given
9:34
Key is not found in garage, not found in living room, but key is found in chair
9:39
So here it is matched. So it is not going towards the last
9:42
Similar way continuous statements we have seen and past statements we have seen
9:46
This is an example of range, 100 values. Whenever it is divided by 9, you are printing, otherwise passing
9:55
All the nine arameters are getting output here. Right. So this was it about our Python control flow statements
10:04
Thank you. See you in the next chapter