If you are here, it means you are going to be a coder soon. You will solve real-world computer problems, make your own programs, and a lot more. Yeah, sounds good!
Now let's talk about for. It is also a loop like 'while'. Let's see how it works.
marks = [78,98,50,37,45] for m in marks: print m
Just this and it's done. Let me explain you the code.
for m in marks - m is a variable which will go to every element in 'marks' and will take its value.
So, in the first iteration, m will be the 1st element of the list.
In the second iteration, m will be the 2nd element of the list an so on.
As you have seen above, in the first iteration the value of m is 78, in the second iteration, its 98, then 50 and so on.
Example of sum of marks
marks = [78,98,50,37,45] sum = 0 for m in marks: sum = sum+m print sum
I think you got the code. If not, then let me explain a bit.
sum = 0 - We are taking any variable and assigning it an initial value of 0.
In the 1st iteration
m is 78. So, sum+m is 0+78. Now, the value of sum is 78
In the 2nd iteration
m is 98. So, sum+m is 78+98. Now, the value of the sum is 176.
And similarly at last, sum is 308.
I hope it is clear now.
|Do You Know ?|
|Table of 12||Add||And you get table of 13|
So, let's do it with the help of for loop.
table_12 = [12,24,36,48,60,72,84,96,108,120] table_13 =  z = 1 for i in table_12: table_13.append(i+z) #using append to add an element in front of list z = z+1 print table_13
z = 1 - We have to add 1,2,3... respectively to each element. And that's what we are doing. Add 1 then increase z by 1 for the next turn (z = z+1)
table_13.append() - append() is a function of list which adds an element in front of a list. So, (i+z) will add (12+1),(12+2),...(12+10) in their respective turns in the list.
By writing your codes, your mathematics is gonna be fun.
How difficult life may seem, there is always something you can do and succeed at.