Posts: 30
Threads: 10
Joined: Apr 2021
Apr-29-2021, 09:18 AM
(This post was last modified: Apr-29-2021, 09:38 AM by jip31.)
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?
Posts: 722
Threads: 113
Joined: Dec 2017
Hi,
Perhaps you mean
print(x).
or
for x in a:
print(x)
Paul
Posts: 30
Threads: 10
Joined: Apr 2021
(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
Posts: 1,936
Threads: 8
Joined: Jun 2018
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.
Posts: 586
Threads: 1
Joined: Aug 2019
Apr-29-2021, 10:45 AM
(This post was last modified: Apr-29-2021, 10:45 AM by ibreeden.)
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
Posts: 30
Threads: 10
Joined: Apr 2021
(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....)
Posts: 1,345
Threads: 2
Joined: May 2019
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
Posts: 6,196
Threads: 16
Joined: Feb 2020
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
Posts: 3
Threads: 0
Joined: Apr 2021
No, no and no. Never write such a code. This ain’t Java, this is Python
Posts: 1,822
Threads: 2
Joined: Apr 2017
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
|