[Tutor] I am trying to print list elements but i am getting 'none'

Steven D'Aprano steve at pearwood.info
Mon Oct 10 15:51:36 CEST 2011


Praveen Singh wrote:
> This is my code-
>  def getNumbers(num):
>     myList=[]
>     for numbers in range(0,num,2):
>           print myList.append(numbers)
> 
> 
> output-
>>>> getNumbers(10)
> None
> None
> None
> None
> None
> 
> Then i find out that list.append doesn't return anything.Then what should i
> use for this kind of operation.but if i do something like this on idle's
> interpreter it gives me answer-


If you want to print the list after each append, then print the list 
after each append:

def getNumbers(num):
     myList=[]
     for number in range(0, num, 2):
         myList.append(number)
         print mylist


If you want to print the list once, at the end, then print it once, at 
the end, *outside* the loop:

def getNumbers(num):
     myList=[]
     for number in range(0, num, 2):
         myList.append(number)
     print mylist


-- 
Steven



More information about the Tutor mailing list