Wednesday, 23 May 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...