help for use print in REPL - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: help for use print in REPL (/thread-33485.html) Pages:
1
2
|
help for use print in REPL - jip31 - Apr-29-2021 Hi I try to test this basic code in REPL a = [1, 2, 3, 4, 5] for x in range(len(a)): print a[x]I don't succeed to display the list result : 1, 2,3 After for x in range(len(a)): there is an identation So I am doing tab + print(a) but there is an stdin error what is wrong please? RE: help for use print in REPL - DPaul - Apr-29-2021 Hi, Perhaps you mean print(x). or for x in a: print(x) Paul RE: help for use print in REPL - jip31 - Apr-29-2021 (Apr-29-2021, 09:18 AM)DPaul Wrote: Hi, Hi No Like explained, I need to display the list and the name list is a RE: help for use print in REPL - perfringo - Apr-29-2021 No, no and no. Never write such a code. This ain’t Java, this is Python: for item in iterable: print(item)And if you are using Python2 then support of it has ended quite some time ago. Consider upgrading. RE: help for use print in REPL - ibreeden - Apr-29-2021 Hi Jip31, Although it is right what Perfringo said, for the purpose of studying: what your code gives me is the following:
print a[x] is old Python 2 syntax. In Python 3 "print()" is a function. When you correct this:a = [1, 2, 3, 4, 5] for x in range(len(a)): print(a[x])... the result is:
RE: help for use print in REPL - jip31 - Apr-29-2021 (Apr-29-2021, 10:45 AM)ibreeden Wrote: Hi Jip31, thanks ibredeen (sorry I have one week python background....) RE: help for use print in REPL - jefsummers - Apr-29-2021 And a tip - whenever you have range(len(... there is a better way. In this case a = [1, 2, 3, 4, 5] for x in a: print(x)Reiterating Perfringo, but specifically this case. For x in a pulls the elements of the list a. You then print (using Python 3 syntax for the print statement) You may also want to look at list comprehensions. Not the best example, but a = [1,2,3,4,5] [print(x) for x in a]Gets it down to 2 lines RE: help for use print in REPL - deanhystad - Apr-29-2021 I think it is poor practice using a comprehension or any code for its side effects. My linter agrees. RE: help for use print in REPL - bernardand - Apr-30-2021 No, no and no. Never write such a code. This ain’t Java, this is Python RE: help for use print in REPL - ndc85430 - Apr-30-2021 Really, people should be learning how to program more declaratively anyway (a good talk on this: https://youtu.be/nrVIlhtoE3Y). To be fair to Java, that's done by making use of the Streams API |