1
00:00:00,510 --> 00:00:05,460
What would your friends in this lesson, we're going to understand loupe concepts in Python.

2
00:00:05,970 --> 00:00:06,930
So let's get started.

3
00:00:08,510 --> 00:00:10,220
So what are these loops?

4
00:00:11,240 --> 00:00:17,450
Well, loops are statements that make code run repeatedly, as the name may imply.

5
00:00:18,260 --> 00:00:20,690
So in general, statements are executed sequentially.

6
00:00:20,720 --> 00:00:21,020
Right.

7
00:00:21,350 --> 00:00:25,040
The first statement in a function is executed first, followed by the second and so on.

8
00:00:29,320 --> 00:00:33,910
There may be a situation when we need to execute a block of code, several numbers of times.

9
00:00:34,360 --> 00:00:39,790
Now, at that point, a loop statement will allow us to execute a statement or a group of statement

10
00:00:40,120 --> 00:00:41,200
multiple times.

11
00:00:43,220 --> 00:00:48,290
Now there are two kinds of loob statements while loops and for loops.

12
00:00:49,580 --> 00:00:51,530
So let's start with a while loops.

13
00:00:53,030 --> 00:00:59,480
While loops will allow us to repeat one or more actions while a condition remains true.

14
00:01:02,400 --> 00:01:09,120
So on a condition becomes false, program control passes to the line immediately following the loop.

15
00:01:10,360 --> 00:01:11,640
Have a look and see how that works.

16
00:01:12,540 --> 00:01:16,620
So let's define a variable name eye and print it.

17
00:01:16,620 --> 00:01:17,180
Repeat it.

18
00:01:18,090 --> 00:01:19,350
I equals zero.

19
00:01:19,950 --> 00:01:22,410
While I is less than 15.

20
00:01:30,990 --> 00:01:32,250
Print I.

21
00:01:37,480 --> 00:01:38,950
I plus equals one.

22
00:01:40,480 --> 00:01:48,700
So here we define the eye variable's value zero and we created a while loop and the loop printed the

23
00:01:48,700 --> 00:01:51,280
eye values from zero to 15.

24
00:01:51,460 --> 00:01:53,980
And then our loop ended after reaching 14.

25
00:01:55,240 --> 00:02:00,220
Now try to print Hello World 20 times I equals zero.

26
00:02:00,640 --> 00:02:02,920
While I was less than 20.

27
00:02:08,010 --> 00:02:09,660
Print Hello, world.

28
00:02:18,570 --> 00:02:20,280
I plus equals one.

29
00:02:22,440 --> 00:02:29,670
As you can see that our loop will work a while and then it stops because we defined a limit and every

30
00:02:29,670 --> 00:02:34,440
time it reruns after, our eye value will increase by one.

31
00:02:35,220 --> 00:02:37,530
So when it reaches that limit, it stops.

32
00:02:39,270 --> 00:02:42,870
So what if our eye value doesn't increase?

33
00:02:44,010 --> 00:02:44,850
Well, let's have a look.

34
00:02:45,540 --> 00:02:48,690
Eye equals zero while eye is less than 20.

35
00:02:54,900 --> 00:02:55,740
Print I.

36
00:03:08,620 --> 00:03:09,430
Oh, boy.

37
00:03:10,390 --> 00:03:14,380
As you can see, our loop will not stop and yeah, it will run forever.

38
00:03:14,870 --> 00:03:18,760
And that's why it's called an infinite loop or endless loop.

39
00:03:20,320 --> 00:03:21,400
So there you go.

40
00:03:21,400 --> 00:03:27,040
If you do try to run this code, then you've got to be able to click the interrupt the kernel here.

41
00:03:33,170 --> 00:03:35,900
And then click restart at the kernel here.

42
00:03:37,430 --> 00:03:42,240
OK, the colonel has restarted now and Jupiter's back working again.

43
00:03:43,710 --> 00:03:47,640
So as you just saw and maybe you did, maybe you didn't try it on your own.

44
00:03:48,240 --> 00:03:55,680
If we don't increment the variable I the value of the variable I remains constant zero and the loop

45
00:03:55,680 --> 00:03:57,090
condition remains constant.

46
00:03:57,120 --> 00:03:57,630
True.

47
00:03:58,500 --> 00:04:04,020
So then Python will print the eye variable repeatedly until we stop the kernel.

48
00:04:05,970 --> 00:04:07,500
Now we can talk about our other.

49
00:04:10,470 --> 00:04:17,040
The four statement allows you to repeat an action or several actions for each item in a sequence of

50
00:04:17,040 --> 00:04:17,460
items.

51
00:04:18,210 --> 00:04:22,730
For example, a string is a sequence of individual characters, right.

52
00:04:23,550 --> 00:04:24,750
So why don't we see how it works?

53
00:04:25,790 --> 00:04:27,290
Four characters in Python.

54
00:04:34,830 --> 00:04:36,270
Print character.

55
00:04:40,770 --> 00:04:48,300
Upon entering the statement, it signs the pea and python to the target variable between keywords for

56
00:04:48,450 --> 00:04:49,320
and in.

57
00:04:50,040 --> 00:04:51,420
In this case, character.

58
00:04:52,640 --> 00:04:56,600
And after executing the code, Python assigns to character.

59
00:04:56,840 --> 00:05:04,850
The next item in the sequence that is the Y in Python, then it executes the code again, and then this

60
00:05:04,850 --> 00:05:05,700
will continue.

61
00:05:05,720 --> 00:05:09,230
While there are more items in the sequence to process.

62
00:05:10,160 --> 00:05:14,840
So in this case, the statement terminates after displaying the letter N.

63
00:05:17,100 --> 00:05:20,400
So now let's talk about some fundamental stuff and loops.

64
00:05:21,810 --> 00:05:23,200
Maybe you have this question.

65
00:05:23,670 --> 00:05:31,290
So what should we do if we want to exit before the loop is complete or we want to change the loop flow?

66
00:05:32,740 --> 00:05:36,220
Well, in Python, we can do that with a control statement.

67
00:05:37,550 --> 00:05:44,660
Loop control statements change execution from its normal sequence, so when execution leaves a scope,

68
00:05:45,020 --> 00:05:49,040
all automatic objects that were created in that scope are destroyed.

69
00:05:50,480 --> 00:05:52,640
Python supports the following control statements.

70
00:05:53,880 --> 00:05:57,840
Break statement, continue statement and pass statement.

71
00:05:59,660 --> 00:06:01,820
So our first control statement is break.

72
00:06:02,030 --> 00:06:05,780
It is most commonly used statement by programmers and loops.

73
00:06:06,650 --> 00:06:10,220
So with the help of this statement, we can interrupt an ongoing process.

74
00:06:10,910 --> 00:06:14,450
Thus, the cycle is terminated without being bound to any condition.

75
00:06:21,080 --> 00:06:23,750
For example, number equals zero.

76
00:06:29,450 --> 00:06:31,910
While no, this is less than 15.

77
00:06:40,560 --> 00:06:41,280
Print, no.

78
00:06:48,390 --> 00:06:50,370
No plus equals one.

79
00:06:54,470 --> 00:06:55,580
Now we know this loop, right?

80
00:06:56,270 --> 00:06:58,340
So let's use a break statement.

81
00:06:58,640 --> 00:07:01,640
Number equals zero, while number is less than 15.

82
00:07:09,440 --> 00:07:10,190
Print, no.

83
00:07:14,900 --> 00:07:17,240
If I equals nine.

84
00:07:23,550 --> 00:07:24,090
Break.

85
00:07:27,030 --> 00:07:28,890
Number plus equals one.

86
00:07:30,900 --> 00:07:36,540
And as you can see, when the number variable's value reaches nine, the loop stopped and terminated.

87
00:07:37,470 --> 00:07:43,230
The most common use for a break is when some external condition is triggered, requiring a hasty exit

88
00:07:43,230 --> 00:07:43,710
from a loop.

89
00:07:44,880 --> 00:07:48,810
Also, the brake statement terminates only the loop in which it exists.

90
00:07:49,260 --> 00:07:55,800
So if there are other nested loops and a brake is used in the innermost loop, only that innermost loop

91
00:07:55,800 --> 00:07:56,400
ends.

92
00:07:59,170 --> 00:08:01,240
So our second statement is continue.

93
00:08:03,200 --> 00:08:10,430
The task of the continue statement is to ensure that everything that follows is Skift and then return

94
00:08:10,430 --> 00:08:11,270
to the beginning of the loop.

95
00:08:12,380 --> 00:08:15,560
So let's look at our examples to understand the continuous statement.

96
00:08:16,460 --> 00:08:17,420
No equal zero.

97
00:08:23,070 --> 00:08:25,050
While number is less than 10.

98
00:08:31,720 --> 00:08:33,100
If no equals to.

99
00:08:34,080 --> 00:08:35,400
Number plus equals one.

100
00:08:37,080 --> 00:08:37,680
Continue.

101
00:08:39,510 --> 00:08:40,650
Print, no.

102
00:08:45,200 --> 00:08:46,550
Number plus equals one.

103
00:08:53,480 --> 00:08:55,310
The result is, as you see.

104
00:09:01,840 --> 00:09:04,510
So our last control statement is pass.

105
00:09:05,950 --> 00:09:10,120
So we use pass when a statement is required syntactically.

106
00:09:10,630 --> 00:09:13,960
But you don't want any commander code to execute.

107
00:09:14,920 --> 00:09:17,590
So the past statement is a null operation.

108
00:09:17,980 --> 00:09:19,810
So nothing happens when it executes.

109
00:09:20,530 --> 00:09:22,480
Generally, we use it as a placeholder.

110
00:09:23,260 --> 00:09:24,220
I'll give you an example.

111
00:09:34,510 --> 00:09:37,870
And when we run this code, Python returns an error.

112
00:09:39,100 --> 00:09:40,240
But let's try with Pat.

113
00:09:41,900 --> 00:09:43,580
For character in Python.

114
00:09:51,850 --> 00:09:52,330
Part.

115
00:09:53,690 --> 00:09:56,870
And now you see that we don't get errors in our code.

116
00:09:59,840 --> 00:10:04,850
So now we'll talk about one more topic in this lesson, and it's the range function.

117
00:10:05,630 --> 00:10:08,150
So what is the range function?

118
00:10:10,840 --> 00:10:13,480
It's a predefined function in Python.

119
00:10:14,200 --> 00:10:17,470
It creates a structure based on the values that we give it.

120
00:10:18,940 --> 00:10:26,620
This structure creates a sequence of numbers that is similar to lists starting ending and optionally

121
00:10:26,680 --> 00:10:27,430
incrementing.

122
00:10:28,450 --> 00:10:33,400
So in Python, we can use the range function to represent numbers within a certain range.

123
00:10:33,910 --> 00:10:35,170
So when we see how it works.

124
00:10:36,040 --> 00:10:38,290
Range zero 15.

125
00:10:43,470 --> 00:10:49,680
And of course, when we run this code, it creates a sequence of numbers from zero to 15 and 15 is not

126
00:10:49,680 --> 00:10:50,280
included.

127
00:10:51,150 --> 00:10:54,810
So if we want to print this sequence, we can use the asterisk.

128
00:10:56,150 --> 00:10:56,570
Print.

129
00:10:58,970 --> 00:11:01,160
Asterisk range 015.

130
00:11:06,240 --> 00:11:14,700
It creates a sequence of numbers from zero to 15 and 15 is not included in the range function is a function

131
00:11:14,700 --> 00:11:17,130
that will take three different parameters.

132
00:11:18,740 --> 00:11:23,360
Now, we know that if the first parameter is zero, we don't need to specify it.

133
00:11:30,900 --> 00:11:36,780
However, if we are going to use the third parameter in the range function, that is to give a command

134
00:11:36,780 --> 00:11:42,990
like range zero 10 to, we have to specify all three parameters.

135
00:11:44,190 --> 00:11:51,780
So this will create numbers from three to 30 by giving for print answers range 334.

136
00:11:57,580 --> 00:12:04,060
Now, if we do not specify all the parameters here, Python cannot understand which number corresponds

137
00:12:04,060 --> 00:12:09,400
to which parameter range three, 10, five, five.

138
00:12:12,730 --> 00:12:18,490
So when we run this code, Python gives us an error and says range expected at most three arguments.

139
00:12:18,780 --> 00:12:19,690
And we got four.

140
00:12:21,130 --> 00:12:27,280
So now let's employ the structure that we created with the range function, with the for loop.

141
00:12:27,580 --> 00:12:31,000
Did I steal your idea for number in range?

142
00:12:34,790 --> 00:12:36,230
Zero 15.

143
00:12:40,490 --> 00:12:41,690
Print, no.

144
00:12:45,530 --> 00:12:46,040
That's it.

145
00:12:47,860 --> 00:12:53,830
All right, so in this lesson, we have learned the basic things that we need to know about lubes and

146
00:12:53,830 --> 00:12:54,970
range functions.

147
00:12:56,020 --> 00:12:59,500
Also, we've learned about how to control the loops with control statements.

148
00:12:59,650 --> 00:13:03,460
So in our next lesson, we're going to learn some other data types in Python.

149
00:13:03,940 --> 00:13:04,750
Want to see you there.

150
00:13:05,380 --> 00:13:06,130
Have a nice day.
