in Education by
Is there a way to step between 0 and 1 by 0.1? I thought I could do it like the following, but it failed: for i in range(0, 1, 0.1): print i Instead, it says that the step argument cannot be zero, which I did not expect. Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
We have many ways to get the decimal point value but what you are doing is not supported in Python:- To use a floating-point step value, you can use the arange() function for that you need to import numpy library. Below is the code that show how to do it:- import numpy as np np.arange(0.0, 1.0, 0.1) But the best thing you can do to get decimal values is to use the linspace function from the NumPy library (which isn't part of the standard library but is relatively easy to obtain). linspace takes a number of points to return and also lets you specify whether or not to include the right endpoint. Below is the code that illustrates how to do it:- import numpy as np np.linspace(0,1,10,endpoint=False) Another alternative way is to use list comprehension. What you were doing is using Python's range() function which can only do integers, not floating point. In your specific case, you can use a list comprehension instead. [x/10 for x in range(0, 10)]

Related questions

0 votes
    What makes using range() to make the process of generating a quadrillion values in a instant of time, I am amazed by ... o += 1 return Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    I have two integer values a and b, but I need their ratio in floating point. I know that a < b, and I want ... the following? c= a / b Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
0 votes
    How can someone parse a numeric string like "121.5555" to it's corresponding float value 121.5555? or parse a ... str to an int. Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
0 votes
    Apparently xrange is faster but I have no idea why it's faster (and no proof besides the anecdotal so far that it ... xrange(0, 20): Select the correct answer from above options...
asked Jan 27, 2022 in Education by JackTerrance
0 votes
    How can I round off b to 16.75 >>> b 16.749999999999999 >>> round(, 2) 16.749999999999999 I tried round function but it doesn't worked. Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    If I use a while loop for my below code, it is not giving the desired output, but when i use for loop i am anle ... x += 1 print(Comm) Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    I'm using TensorFlow Alpha 2.0. I have TFRecords files I'm reading from, each one holding a short ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 1, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 17, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    I have the following dataframe using pandas df = pd.DataFrame({'Last_Name': ['Smith', None, 'Brown ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 8, 2022 in Education by JackTerrance
0 votes
    I have a dataframe with which if a cell has a value other than "." then I need python to return ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 3, 2022 in Education by JackTerrance
0 votes
    I'm trying to make a function that will compare multiple variables to an integer and output a string of three ... like this possible? Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
0 votes
    I want to assign a value to a TensorFlow variable in Python and I am using this: import tensorflow as tf import ... do to correct this? Select the correct answer from above options...
asked Jan 24, 2022 in Education by JackTerrance
0 votes
    I have two fields a string field and a numeric field in a directory. The key of the dictionary as well as the ... based on the keys? Select the correct answer from above options...
asked Jan 21, 2022 in Education by JackTerrance
...