#๐Ÿ”’ WHY DOES THE 2ND CODE not output '1'

11 messages ยท Page 1 of 1 (latest)

timber cairn
#

n = [1, 2 ,3 ,4, 5, 6]

code 1

i = 1
while i < len(n):
print(i)
i = i + 1

code 2

j = 1
while j < len(n):
print(n[j])
j = j + 1

OUTPUT :
1
2
3
4
5
6

2
3
4
5
6

eternal bearBOT
#

@timber cairn

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

placid hill
#

indices start at 0

dapper estuary
#

Because list indexing in python starts at 0, that means list[1] refers to the 2nd element in a list.

timber cairn
#

@placid hill@dapper estuary what is the difference between print(j) and print (n[j])

dapper estuary
#

print(j) prints the value of the variable j. That can be whatever, an integer, a string, a list ...
print(n[j]) prints the value of whatever is in the list n at position j. That can also be whatever (str, list, int, ...) but j has to be an integer (a whole number).

timber cairn
#

Thanks

dapper estuary
#

!e

n = [1, 2, 3, 4, 5, 6]

print(n)
for j in range(6):
    print(f"j: {j}, n[j]: {n[j]}")```
to visualize this
eternal bearBOT
eternal bearBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.