I want to put the results of the for statement in the array.

Asked 2 months ago, Updated 2 months ago, 1 views

Sorry for the simple question.

def date():
  for num in range (0,100):
      num+=1
      print(num)

What should I do if I want to put this result (all results from 1 to 100) in the array?

python

2022-09-30 14:12

2 Answers

How about this

def date():
  result=[]# A list of results is provided
  for num in range (0,100):
      num+=1
      print(num)
      result.append(num)# Add Results
  return result# Returns a list of results


2022-09-30 14:12

If you don't have print(), you can write like this using the list composition.

def date():
  return [ x+1 for x in range (0,100) ]


2022-09-30 14:12

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.