Python Programming Tutorial - 21: Nested Loops

preview_player
Показать описание
In this tutorial we'll learn to use nested loops in python along with the break statement to display prime numbers as well as non prime numbers between 2 and 100.
Рекомендации по теме
Комментарии
Автор

Great video. Took me 20 minutes staring at the code to understand it, but eventually got it. Btw, I could understand you relatively well, but then again, I have years of IT experience dealing with Indian support. Thanks for the tutorial.

lorikampsnider
Автор

This was exactly what I was looking for thank you for making this video!

alexanderstatan
Автор

When I want the bad tutorials. I come here. Thanks!

retiredshitposter
Автор

I understand the logic for all numbers except for the number 2. It appears to me 2 only evaluates to 1 in the inner loop, so 2 % 1 is obviously == 0, howeve itr is (correctly) flagged as "prime" in the output. Could someone explain to me how this happened? What am I missing here?

jorgeespinoza
Автор

Sir I have a doubt in the second for loop

sindhujadoolam
Автор

Very nicely done.Took some time to understand why you put flag=true in the second line there.But I get it.

clearofcloud
Автор

m = 0
for x in range (1, 3):
for y in range (4, 6):
m = m + x + y
print (m)


please explain this program

pankajptl
Автор

how can we recognize where we should use 'while loop' and where 'for loop'?? Is there any trick to understand that?

aafaquequreshi
Автор

what does the mean (var1%var2==0) what does % do and here what does 2==0 means ?

retarddevil
Автор

I couldn't make out some words in this video so i didn't understand about flags and how the second loop is working. Can someone explain it to me?

weezybusy
Автор

second loop var-1 is not working in Python 3.
If hope it should be var only..
Below program is working fine for me :

for var1 in range(2, 101):
flag=True
for var2 in range(2, var1):
if(var1%var2==0):
print(var1, "is not a prime number")
flag=False
break
if flag:
print(var1, "is a prime number")

ranjan
Автор

same program copy kara, and run kiya but output different aa raha h. help

ayushsharma
Автор

for var in range(1, 100):
var1=var;
c=1;
for i in range(2, var1):
if(var1%i==0):
c=2;

if c==1:
print(var);



i have used this logic gives an error "unindent does not match any outer idention level"

please help...n thanx

subhasisparija
Автор

Your program doesn't count for n-1, 7 is not being divided by 6 which works is this case because no number is divisible by a number-1. For people wanting to know what variables are being passed at each point in the nested loop. This might help make things clearer. My code is as follows: -
for x in range(2, 101): #it's 2 to 10, 1 less than the range
flag=True
for y in range(2, x):
print("x is: ", x, "\n", "y is: ", 2, "to ", x, "Y\'s value is now: ", y)
if(x%y==0):
print(x, "is not a prime number")
flag=False
break
if flag:
print(x, "is a prime number")

Thank you for the tutorial. Hit like.

cdlV
Автор

Madhur, your microphone is not working properly in this video (21) and also the next video (22)

eb
Автор

what will happen if we don't write 'break' in the program? Can anyone provide answer in details??

aafaquequreshi
Автор

It said 19 is a prime number, even though it's divisible by 9 and 10!

robertlatta
Автор

for num in range(2, 10):
for i in range (2, num):
if(num%i==0):
print(num, "is not prime")
break
else:
print(num, "is a prime")
break



this is wud work as well

kannu