-2

I know this is a simple question, but I would like to know why it is printing None. It just prints None when coding it directly from my terminal. If I write the same code in a file or in a online Python compiler and then run it, I don't get the same output.

Coding directly from my terminal:

>>> b = [(1, 2), (3, 4), (5, 6)]
>>> [print(a) for a in b]
(1, 2)
(3, 4)
(5, 6)
[None, None, None]

Running in a Python Online Compiler:

enter image description here

Running Python file:

enter image description here

I know it is not the same array, but it is the same logic.

b = ([100.64188563286582, 101.64188563286582], [-1.0626228, -0.8626228], [-5.0, -15.0], [-float('inf'), float('inf')], [float('inf'), float('inf')], [-float('inf'), float('inf')], [-float('inf'), float('inf')])

[print(a) for a in b]
mikuszefski
  • 3,943
  • 1
  • 25
  • 38
Lit2000hania
  • 97
  • 11
  • 5
    Please replace your images with pasted code (as a text). – Beniamin H Jan 07 '21 at 15:42
  • You probably want to remove the print statement from the list comprehension. What you get is first the print output, then a list of the return values of the print calls. – snwflk Jan 07 '21 at 15:43
  • And there is an answer to your question: https://stackoverflow.com/questions/28812851/why-is-this-printing-none-in-the-output – Beniamin H Jan 07 '21 at 15:44
  • I edited the post with the code – Lit2000hania Jan 07 '21 at 15:44
  • 1
    Basically it's because function `print` returns None. Most statements executed in the interacive interpreter (your terminal) prints returned results. – Beniamin H Jan 07 '21 at 15:47
  • @polalas Only to some extent. There still remain screen capture of your code. Not cool. – pfabri Jan 07 '21 at 15:48
  • @pfabri I put the screen capture just to show the output, that was the idea – Lit2000hania Jan 07 '21 at 15:51
  • Does this answer your question? [Why is this printing 'None' in the output?](https://stackoverflow.com/questions/28812851/why-is-this-printing-none-in-the-output) – pfabri Jan 07 '21 at 15:54

2 Answers2

2

[print(a) for a in b] results in a list of print() objects. As you can verify simply by print(print()), printing a list of print() objects will display a list of None

abe
  • 957
  • 5
  • 10
0

Use without print() , as print(print()) inner print not return any thing so that at last it gives None

>>>b=[(1,2),(3,4),(5,6)]
>>>[a for a in b]
>>>[(1, 2), (3, 4), (5, 6)]

output:

[(1, 2), (3, 4), (5, 6)]
MRUNAL MUNOT
  • 395
  • 1
  • 5
  • 18