How do you make a list of numbers 1 to 100 in python?
Asked 9 years, 1 month ago Show Viewed 1.6m times How do I create an ascending list between two values? For example, a list between 11 and 16:
Mateen Ulhaq 22.2k16 gold badges86 silver badges127 bronze badges asked Aug 16, 2013 at 4:43
Use
In Python 3,
Note:
The second number in To increment by steps of
See: How do I use a decimal step value for range()?
Mateen Ulhaq 22.2k16 gold badges86 silver badges127 bronze badges answered Aug 16, 2013 at 4:47
JaredJared 24.6k7 gold badges52 silver badges61 bronze badges 9 You seem to be looking for
For incrementing by
answered Aug 16, 2013 at 4:46
devnulldevnull 113k33 gold badges227 silver badges216 bronze badges 1 Try:
That is a list in Python 2.x and behaves mostly like a list in Python 3.x. If you are running Python 3 and need a list that you can modify, then use:
ah bon 8,3797 gold badges48 silver badges100 bronze badges answered Aug 16, 2013 at 4:49
Mike HouskyMike Housky 3,8711 gold badge16 silver badges30 bronze badges If you are looking for range like function which works for float type, then here is a very good article.
Output:
answered Mar 15, 2016 at 3:21
Rajesh SuranaRajesh Surana 7831 gold badge9 silver badges15 bronze badges 1 assuming you want to have a range between x to y
use list for 3.x support answered Aug 16, 2013 at 4:47
v2bv2b 1,4169 silver badges14 bronze badges Use list comprehension in python. Since you want 16 in the list too.. Use x2+1. Range function excludes the higher limit in the function.
ah bon 8,3797 gold badges48 silver badges100 bronze badges answered Aug 16, 2013 at 4:46
4 In python you can do this very eaisly
or you can create a recursive function that returns an array upto a given number:
output: ar=[10,9,8,7,6,5,4,3,2,1,0] answered Jun 4, 2019 at 19:01
I got here because I wanted to create a range between -10 and 10 in increments of 0.1 using list comprehension. Instead of doing an overly complicated function like most of the answers above I just did this
By changing the range count to 100 I now get my range of -10 through 10 by using the standard range function. So if you need it by 0.2 then just do range(-200, 200) and so on etc answered Aug 3, 2020 at 21:25
JoseJose 1,49117 silver badges19 bronze badges The most elegant way to do this is by using the
This produces the output:
As expressed before by @Jared, the best way is to use the answered Jan 11, 2018 at 11:22
MichaelMichael 2,2621 gold badge32 silver badges53 bronze badges 2 Every answer above assumes range is of positive numbers only. Here is the solution to return list of consecutive numbers where arguments can be any (positive or negative), with the possibility to set optional step value (default = 1).
For instance, standard command Updated: now step may be negative. Thanks @Michael for his comment. answered Dec 4, 2017 at 15:42
Denis RasulevDenis Rasulev 3,3764 gold badges33 silver badges41 bronze badges 5 While
@Jared's answer for incrementing works for
Instead I needed something like this myself, working not just for
answered Jun 18, 2020 at 13:50
YTZYTZ 7276 silver badges21 bronze badges @YTZ's answer worked great in my case. I had to generate a list from 0 to 10000 with a step of 0.01 and simply adding 0.01 at each iteration did not work due to rounding issues. Therefore, I used @YTZ's advice and wrote the following function:
answered Oct 1, 2021 at 13:13
How do I make a list of numbers in Python?Python comes with a direct function range() which creates a sequence of numbers from start to stop values and print each item in the sequence. We use range() with r1 and r2 and then convert the sequence into list.
How do you make a list of numbers from 1 to N in Python?List of Numbers From 1 to N in Python. Create a User-Defined Function to Create a List of Numbers From 1 to N.. Use the range() Function to Create a List of Numbers From 1 to N.. Use the numpy.arange() to Create a List of Numbers From 1 to N.. How do you create a range of numbers in Python?In Python 2, we have range() and xrange() functions to produce a sequence of numbers.. Here, start = 0 and step = 1 as a default value.. If you set the stop as a 0 or some negative value, then the range will return an empty sequence.. If you want to start the range at 1 use range(1, 10) .. How do I print a list of numbers in Python?Use the Built-In Map Function to Print a List in Python. If you want to print a list of integers, you can use a map() function to transform them into strings. Then you can use the join() method to merge them into one string and print them out.
|