Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to print an index 0 of a list, with for loop?

Why does this code, does not return (or print) the 0 index of the list?

N = int(input())

letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

for i in range(N):

    print(letters[i]*i)

output: should be:

A
BB
CCC
DDDD
EEEEE
FFFFFF
GGGGGGG

But im getting this, without the "A":

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

B
CC
DDD
EEEE
FFFFF
GGGGGG

>Solution :

Because i starts counting at zero.

If you type:

for i in range(10):
print(i)

You’ll see:

0
1
2
3
4
5
6
7
8
9

If you want to count from 1 to N instead of 0 to N-1, type:

print(letters[i]*(i+1))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading