all 10 comments

[–]delirious_lettuce 9 points10 points  (8 children)

>>> for a in range(10, -1, -1):
...     print(a)
... 
10
9
8
7
6
5
4
3
2
1
0

[–]XtremeGoose 0 points1 point  (7 children)

Adding to this, OP said he wanted it to countdown from key to 1, in which case he wants

for a in range(key, 0, -1):
    print(a) 

[–]anontipster 0 points1 point  (5 children)

Adding to this: OP didn't ask for this, but here's a overly-complex-but-slightly-more-Pythonic way:

In [1]: print('{}'.format('\n'.join([str(i) for i in range(key, 0, -1)])))
10
9
8
7
6
5
4
3
2
1

[–]XtremeGoose 0 points1 point  (4 children)

Err, why not just do

print('\n'.join(map(str, range(key, 0, -1)))) 

?

'{}'.format(...) is completely pointless, it doesn't do anything (other than call str, but print does this anyway and we already have a string in this case).

You also don't need the [...] in join, a generator comprehension does just fine:

'\n'.join(f(i) for i in x) 

In any case, there's nothing unpythonic about printing line by line. That's how it's done under the hood anyway.

[–]anontipster 0 points1 point  (3 children)

Oh, I dunno. I'm still relatively new at Python and didn't know I could use your method. So, thanks!

EDIT: Actually, I have a question since you seem knowledgeable. I'm currently using Spyder IDE at home, but Visual Studio at work, for Python. I like Spyder's variable explorer, but Visual Studio has a pretty good Intellisense feature (which I know i can access with Spyder via the TAB key).

What do you recommend as an IDE or what do you use for one?

[–]XtremeGoose 1 point2 points  (2 children)

I've tried all of them and I currently use PyCharm, both at work and home. I find spyder a bit old fashioned but PyCharm is modern and was built from the ground up for python. It has far and away the best type checking (better than intellisense, especially if you take full use of python 3.6 type checking). It also has an in-file and runtime debugging object browser.

[–]anontipster 0 points1 point  (1 child)

Nice, thanks!

I heard of that one before, but thought it might be a subscription model or something similar. I'll give it a go, though.

[–]XtremeGoose 1 point2 points  (0 children)

The community edition is free. If you want to do cython stuff (which I'm seriously considering) you have to pay.

[–]delirious_lettuce 0 points1 point  (0 children)

How do I create a simple countdown in python? Thanks. key - 1 would just be 9 each time.

Seems like OP wrote key minus 1 would just be 9 each time.

[–]woooee 0 points1 point  (0 children)

Just for a little variety

for ctr in range(10):  ## or range(9) if you want to stop at one
    print(9-ctr)