#๐Ÿ”’ range() with float

35 messages ยท Page 1 of 1 (latest)

prime lotus
#

how can i do a for loop with the output 0, 0.1, 0.2, 0.3 ..... like range() funktion with floats?

raven mountainBOT
#

@prime lotus

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.

late fog
#

You can set step to 0.1

#

Like range(0, 1, 0.1) for example,
this will increase by 0.1

#

etc

limpid horizon
#

That could produce unpredictable results.

grim pebble
limpid horizon
#

oh right it's also a TypeError

tawdry escarp
#

!e use integer and divide

for i in [x/10 for x in range(10)]:
     print(i)
raven mountainBOT
#

@tawdry escarp :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | 0.0
002 | 0.1
003 | 0.2
004 | 0.3
005 | 0.4
006 | 0.5
007 | 0.6
008 | 0.7
009 | 0.8
010 | 0.9
limpid horizon
#

@prime lotus it's probably best to just iterate over a range of integers then do the math yourself

limpid horizon
tawdry escarp
#

Yeah, it's just to give an idea

grim pebble
limpid horizon
#

@prime lotus The question is: what is it you're trying to do?

tawdry escarp
#

And I think numpy has a float range

late fog
grim pebble
late fog
#

similar to range()

#

it accepts floats

#

I think

#

!e

import numpy as np

for i in np.arrange(0, 1, 0.1):
  print(i)
#

!e

import numpy as np

for i in np.arange(0, 1, 0.1):
  print(i)
raven mountainBOT
#

@late fog :white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | 0.0
002 | 0.1
003 | 0.2
004 | 0.30000000000000004
005 | 0.4
006 | 0.5
007 | 0.6000000000000001
008 | 0.7000000000000001
009 | 0.8
010 | 0.9
grim pebble
#

0.30000000000000004 ๐Ÿ—ฃ๏ธ

late fog
#

something like that

#

Anyway, I think you are dealing with it better than me.

tawdry escarp
grim pebble
raven mountainBOT
#
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.