Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help for use print in REPL
#1
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?
Reply
#2
Hi,

Perhaps you mean
print(x).
or
for x in a:
print(x)

Paul
Reply
#3
(Apr-29-2021, 09:18 AM)DPaul Wrote: Hi,

Perhaps you mean
print(x).
or
for x in a:
print(x)

Paul

Hi
No
Like explained, I need to display the list and the name list is a
Reply
#4
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
Hi Jip31,
Although it is right what Perfringo said, for the purpose of studying: what your code gives me is the following:
Error:
File "forum03.py", line 3 print a[x] ^ SyntaxError: Missing parentheses in call to 'print'. Did you mean print(a[x])?
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:
Output:
1 2 3 4 5
Reply
#6
(Apr-29-2021, 10:45 AM)ibreeden Wrote: Hi Jip31,
Although it is right what Perfringo said, for the purpose of studying: what your code gives me is the following:
Error:
File "forum03.py", line 3 print a[x] ^ SyntaxError: Missing parentheses in call to 'print'. Did you mean print(a[x])?
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:
Output:
1 2 3 4 5


thanks ibredeen (sorry I have one week python background....)
Reply
#7
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
Reply
#8
I think it is poor practice using a comprehension or any code for its side effects. My linter agrees.
buran and ndc85430 like this post
Reply
#9
No, no and no. Never write such a code. This ain’t Java, this is Python
Reply
#10
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using my REPL to bisect numbers and lists with classes (PyBite #181) Drone4four 2 2,034 Sep-24-2020, 01:47 PM
Last Post: Drone4four
  Using matplotlib in Spyder v/s REPL peterjv26 2 2,551 Jul-04-2020, 10:57 AM
Last Post: snippsat
  Online python repl? Gribouillis 4 38,827 Apr-09-2020, 12:19 PM
Last Post: Gribouillis
  REPL grkiran2011 1 2,103 Jan-08-2020, 11:47 PM
Last Post: Gribouillis
  Embedding or adding IDE like "repl" inside Flask app sray 1 2,217 Jul-03-2019, 03:13 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020