Wednesday, May 23, 2018

List of zeros in python





How can I create a list which contains only zeros? I want to be able to create a zeros list for each int in range(10)



For example, if the int in the range was 4 I will get:



[0,0,0,0]



and for 7:



[0,0,0,0,0,0,0]

Answer



#add code here to figure out the number of 0's you need, naming the variable n.
listofzeros = [0] * n



if you prefer to put it in the function, just drop in that code and add return listofzeros



Which would look like this:



def zerolistmaker(n):
listofzeros = [0] * n
return listofzeros



sample output:



>>> zerolistmaker(4)
[0, 0, 0, 0]
>>> zerolistmaker(5)
[0, 0, 0, 0, 0]
>>> zerolistmaker(15)
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
>>>


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...