DEV Community

Azlan-Syed
Azlan-Syed

Posted on

Solve This Python Question

Hello brothers got a python question for you all solve this python
code and comment the answer

a = []
for i in range(10):
a.append(i * ++i)
for a[i] in a:
print(a[i])

what will be the output of this code ?

Top comments (11)

Collapse
 
madalinfasie profile image
madalinfasie

Aside from what the others have said, there is also no ++ operator in python.

The line for a[i] in a will work, but it has no point and looks awful and confusing.

You should add a note to point out that this code is a parody or something.

A nice change to the code would've been to write a = a.append(i). I found that many beginners confuse the return type of the append method.

Collapse
 
remcoboerma profile image
Remco Boerma

Syntax error, indention expected on line 3?

Collapse
 
ahmadsaadkhan profile image
Ahmad Saad

undefined capital I in a[I]

Collapse
 
azlan_syed profile image
Azlan-Syed

Sorry for that i accidently putted that here i edited it now

Collapse
 
ahmadsaadkhan profile image
Ahmad Saad

Then after fixing all errors.
Output is
0
1
4
9
16
25
36
49
64
64

Thread Thread
 
azlan_syed profile image
Azlan-Syed

CORRECT 👍🏻

Collapse
 
muhammadahsan profile image
Muhammad Ahsan

I'm not sure but ++i will raise error because its not allowed in python.

Correct me if I'm wrong.

Collapse
 
azlan_syed profile image
Azlan-Syed

try running it bro

Collapse
 
pankaj_singhr profile image
Pankaj Singh

0
1
4
9
16
25
36
49
64
64

Collapse
 
shrihankp profile image
Shrihan

Three errors:
L3 Indentation expected
L3 SyntaxError: there's no ++ operator in Python
L5 Undefined symbol l

Collapse
 
azlan_syed profile image
Azlan-Syed

well it has only one error in the end where the i is in caps but try running that code with
small i