marks = [95, 98, 97]
i = 0
while i < len(marks):
print(marks[i])
i = i 1
I want to understand this print(marks[i]) and i = i 1, what is its use here and why can it not be written before print function?
I am learning about while loop in list data type and I am not able to understand this code.
CodePudding user response:
The idiomatic way to loop over a list is simply
for item in marks:
print(item)
There are situations where you want to keep track of where you hre in the list, though then, maybe use enumerate
:
for idx, item in enumerate(marks, 1):
print("item at position", idx, "is", item)
In lower-level languages, you are forced to always keep track of the index, as subscripting is the only way to access an individual array or list member. Even then, the Python idiom of choice would be range(len(...))
:
for i in range(len(marks)):
print(marks[i])
CodePudding user response:
# create an array with the numbers 95, 98, 97
# remember that arrays are 0 indexed
# marks[0] is 95
# marks[1] is 98
# marks[2] is 97
marks = [95, 98, 97]
# set the variable i to 0
i = 0
# len(marks) returns the lenght of the array (3) so while i is less than 3
# as we are using less i will be 0, 1, 2. When i is 3 it's no longer less then 3 and the loop will end.
while i < len(marks):
print(marks[i])
# add one to i 1 becomes 2 etc (itterate)
# if you do this before print you would look at marks[1] during first itteration rather than marks[0]
i = i 1
CodePudding user response:
marks
is a list
, that is, an object that contains a number of other objects. Three integers (numbers) in this case.
print
outputs something to the console.
while
defines a loop with a condition. In this case the condition is that the counter variable i
remains less than
the length (len
) of the list marks
.
marks[i]
means, get the object with the index i
from the list marks
. Indexes of lists start at 0.
i = i 1
increments i
by 1. It takes the current value of i
, adds 1 to it and writes the result back to i
(the =
sign).
So, to sum it up, this piece of code just iterates (runs through) the list marks
and prints out every item in the list to the console.
CodePudding user response:
So here marks
is a list.
i
is intiated with value 0
Then we start a loop which has to run while i < len(marks)
. Here marks has 3 elements therefore len(marks) = 3
.
As i
is 0
which is less than 3
the loop runs. We print marks[i]
which means the element in marks at position i
=> marks[0]
which is 95
.
Then we add 1
to i
, i.e. i = i 1
=> 0 = 0 1
=> i = 1
Now i
i.e. 1
is still less than len(marks)
so the loop runs again to print marks[1]
which is 98
and so on.....
CodePudding user response:
We create a Python list of three integers:
marks = [95, 98, 97]
declare a counter named i and define it to 0
i = 0
Then using a while loop, we check if the condition i.e. i < len(marks) is evaluated to true.
And if it is true that the i counter is less than the length of the list, the body of the while loop is executed.
The body 'i = i 1' says that each time the condition is true , we add 1 to i. So, i is incremented each time when the condition is true.
while i < len(marks):
print(marks[i]) //prints the element each time at that i index
i = i 1
When the condition is evaluated to false i.e. the condition is no more true, the loop terminates and so does the body of the loop.
So in our case, the program prints the whole content of the list using a while loop and i counter that increases by comparing itself to the actual length of the list i.e. len(marks).
CodePudding user response:
marks = [95, 98, 97]
i = 0
while i < len(marks):
print(marks[i])
i = i 1
In this code, a list marks
has been declared and initialised. This list contains 3 elements.
Next, there is a variable i
initialized to 0
.
And next there is a while
loop. With the while
loop we can execute a set of statements as long as a condition is true. Here, the condition is i < len(marks)
. And we know that i
is initialized to 0
. So, as long as i
is less than the size of the marks
list, the body of while
loop will be executed. In the body of the while loop, there are 2 statements. print(marks[i])
prints the ith element of the marks list.i = i 1
increments the i
value by 1 so that next element of the list marks
can be accessed.