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

Christian Witts cwitts at compuscan.co.za
Mon Oct 10 15:32:28 CEST 2011


On 2011/10/10 03:26 PM, 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-
> >>> myList=[]
> >>> myList.append(8)
> >>> print myList
> [8]
>
> Confused!!!
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

If you seperate your print and the myList.append operation you will get 
what you want, which is why it works in IDLE.  Are you sure you don't 
want to rather build your list, and return it from your function and 
then when you call it you do `print getNumbers(10)` if you wish.

-- 

Christian Witts
Python Developer

//
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111010/f3c7b2f3/attachment.html>


More information about the Tutor mailing list