#๐Ÿ”’ Range to the negative value

28 messages ยท Page 1 of 1 (latest)

frail seal
#

Why is the value 0 for this code?

dusky dirgeBOT
#

@frail seal

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.

willow pendant
#

i for i in range(0, -2) means it goes from 0 to -2.

#

Have you tried printing it?

frail seal
#

Yeah.

willow pendant
#

!e

print([i for i in range(0, -2)])
dusky dirgeBOT
#

@willow pendant :white_check_mark: Your 3.12 eval job has completed with return code 0.

[]
frail seal
#

!e

dusky dirgeBOT
#
Missing required argument

code

willow pendant
#

In this case.

frail seal
#

Why it didn't print negative distance the same way as positive did?

willow pendant
#

Since you are incrementing by +1 (it's defaulted this way) nothing is in the list.

rough bloom
#

!e

print([i for i in range(0, -2, -1)])
dusky dirgeBOT
#

@rough bloom :white_check_mark: Your 3.12 eval job has completed with return code 0.

[0, -1]
willow pendant
#

You would need to specify what you are incrementing by.

frail seal
#

Oh I see.

rough bloom
#

yes. thats the 3rd param which for memory is called step. Defaults to 1. Though can make it what you want.

frail seal
#

The third value is how many values you want to leap over, yes?

willow pendant
#

Yes.

frail seal
#

If it's not mentioned, it will add 1 by default.

willow pendant
#

In your case you want -1.

#

Therefore you must specify.

#

It works for others too

#

!e

print([i for i in range(0, 20, 2)])
dusky dirgeBOT
#

@willow pendant :white_check_mark: Your 3.12 eval job has completed with return code 0.

[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
dusky dirgeBOT
#
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.