Get the full source code of application here:
Show More Show Less View Video Transcript
0:00
uh hello guys uh welcome to this video
0:02
so in this video i will show you a
0:03
python script which will actually find
0:05
out prime numbers which are between a
0:08
starting and ending range so this is a
0:11
simple python program which is actually
0:13
asked inside competitive exams or
0:16
interview questions as well so how to
0:18
find out prime numbers in a given range
0:21
so this is actually a python program
0:22
that i written right here if i visualize
0:25
the execution right here so i will show
0:28
you step by step how this program is
0:30
executing so first of all we are taking
0:32
input from the user using this uh input
0:35
function so here we'll be accepting the
0:38
start range and the end range so here as
0:41
you can see we need to provide the
0:43
starting range so let's suppose i want
0:45
to find out all the prime numbers
0:47
starting with two and up till 30 so we
0:52
will find out all these prime numbers
0:54
between 2 and 30 so this is my starting
0:57
range and this is my end range so this
1:01
is basically this is your start variable
1:03
two and end variable is 30 so these are
1:06
two global variables and now we are
1:08
simply printing out the prime numbers
1:10
between start and end so as you can see
1:15
so now we come to this for loop and for
1:18
this specific purpose we are using this
1:20
simple for loop inside python so we are
1:22
starting it from the start variable to
1:26
end so we are looping through 2 up till
1:31
31 so 31 is not included as you can see
1:35
n + 1 is not included so we are looping
1:37
through 2 up till
1:39
30 and now inside this for loop the very
1:42
first thing we are checking right here
1:44
that the number must be greater than one
1:47
because as you all know one is not a
1:49
prime number so now in the next line
1:51
here we are comparing that the number
1:54
must be greater than one so in this case
1:56
2 is greater than one so in this
2:01
case the condition will be evalued to
2:04
true and now inside this inner for loop
2:07
here for i in range 2 now again we are
2:11
having this again for loop so we are
2:14
looping through two up till the actual
2:17
number so in this case the number is two
2:19
so the condition will evaluate to true
2:23
and we will be coming under this for
2:26
loop so the condition will be false
2:28
because the number the 2 2 so two is not
2:32
included so so this for loop will end
2:35
and it will print out the actual number
2:37
right here so two will be the prime
2:40
number as you can see the prime numbers
2:42
are those numbers which are div
2:44
divisible by itself and two so it must
2:48
be at least two numbers so two is
2:50
actually a prime number it is divisible
2:53
by itself and also it is divisible by
2:56
two as well so it's a even prime number
2:59
and now we come to the next number which
3:01
is three 3 is greater than
3:04
1 so then we run this inner for loop to
3:08
actually check that if it is divisible
3:10
by two or not so here we simply check
3:13
that three is not divisible by two
3:17
so this condition will evaluate to false
3:20
here
3:22
so now we will check for the uh we will
3:26
iterate through now the i value will
3:29
become three so it will end and again
3:33
three will be printed out three is also
3:36
a prime number so now we come to the
3:37
next iteration which is four four is
3:40
also greater than one so it will come
3:42
into this inner for loop again we will
3:44
be running this inner for loop so now
3:47
we'll be comparing if four is divisible
3:50
by two which in this case is true four
3:53
is not a prime number it's a even number
3:55
so this condition will evolutes to true
3:58
and now the loop will break and now the
4:01
condition will break and as you can see
4:05
it this four number will not be printed
4:07
out and the control will shift to the
4:11
next number which is uh five so now as
4:16
you can see the num value is five here
4:18
four is not a prime number so it is not
4:20
printed out now again five is again
4:22
greater than one so it will come into
4:24
this inner for loop and again we will
4:27
check here five is not divisible by two
4:30
it will break then the i value will be
4:33
three five is also not divisible by
4:36
three so this will again go to the next
4:40
number which is four five is also not
4:44
divisible by
4:47
four and now it will go to the next so
4:51
as soon as i become five the for loop
4:54
will break and then five will be printed
4:57
out so as you can see the control has
4:59
shifted to the else condition so here we
5:01
are printing so it's a very simple
5:03
program guys as you can see how it is
5:05
simple is it is 2 3 5 so in this
5:09
case now six with six is not a prime
5:13
number so it will come into this for
5:15
loop six is divisible by two so now the
5:19
loop will break and now it will move to
5:23
the next number which is seven so again
5:25
seven will be looping through so this is
5:27
a very simple logic that we have written
5:30
inside this so we are simply having the
5:33
inner the outside for loop is for
5:37
checking that the number must be greater
5:39
than one and in the inner for loop we
5:41
are
5:42
checking for the divisibility if it's
5:45
not an even number so we are checking
5:47
that it should not be divisible by any
5:50
even number so seven as you can see it's
5:54
not divisible by two neither it's three
5:57
four so it will run
5:59
until 7 so as soon as it reaches seven
6:04
the uh loop will break so you can just
6:07
see the flow guys the program flow 2 3 5
6:10
7 so now we have it will loop through
6:16
all the time so eight the loop will
6:18
break now it will go to the ninth so
6:20
just you can see the flow here let me
6:23
just uh do the you can
6:27
see the flow of the
6:29
program it will run up till 30
6:34
number so as you can see in this way the
6:37
program will run and it will print out
6:40
all the prime numbers which are residing
6:42
between two and 30 so you can see the uh
6:46
control flow of the program
6:50
so it will
6:53
reach it will loop through all the
6:55
numbers which are
7:00
there so as you can see it
7:07
is you can pause the video and write
7:10
this program guys it's a very simple
7:12
logic once you understand it keep
7:15
watching this video you will understand
7:17
how it is executing it it is using for
7:21
loop here and inside
7:24
this so you can
7:28
see that's all so it printed out all the
7:32
prime numbers which are residing between
7:34
2 and
7:36
30
7:39
so this is your outer for loop which is
7:44
looping
7:46
through the number starting and ending
7:49
inside this we have if condition then
7:51
inside inner loop we are checking for
7:54
the divisibility of
7:57
two if it is divisible by two then we
7:59
are breaking it if it is not then we are
8:02
printing the number so in this way you
8:05
can check out and then you can paste
8:08
this program inside visual studio as
8:10
well and run this program as well so if
8:12
i run this program here also you will
8:14
see it will ask for
8:17
the starting range let me provide two
8:20
and 10 so it will print out all the
8:23
prime numbers so you can run it in vs
8:25
code as well just pasting this program
8:27
right here very simple program and thank
8:30
you very much for watching this video
8:32
and also check out my website
8:34
freemediattools.com
8:35
uh which contains thousands of tools
#Programming
#Scripting Languages
#Computer Education
