#write a program that generates the following output using a for loop

#write a program that generates the following output using a for loop.

‘for alpha in range(65,91):
print(chr(alpha),end=’‘)
print()
for alpha in range(122,96,-1): /// doubt
print(chr(alpha),end=’,‘)
print()’

output is 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 z, y, x, w, v, u, t, s, r, q, p, o, n, m, l, k, j, i, h, g, f, e, d, c, b, a,

QUESTION- When I use for alpha in range(122,97) i don’t get the same output as above? Can anyone PLZ explain why?

range is normally used for increasing values, so the fact that you want to go down, you need to add the -1.

Python basically starts with 122, then checks if it matches the condition, which is false without the -1.

1 Like

What output do you get, how is it different?

1 Like

If you count upwards (the default for range), starting at 122, will you reach 97? If you start at 122, what should you add each time, in order to reach 97?

(Also: what happens if you try using help(range) at the interpreter prompt? Does this help you understand the problem?)

1 Like

Thanks Eric!!

1 Like

I get the output as 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
There is no output for the second part.

Woah haha! Thanks Karl. I understod now! Thanks for the tip - help(range) Have a good day!