0

Why does it give 6?

A=[8,0,-4,-6,1] for n in A: if n<0: A.append(abs(n)) print(n)

25th Sep 2021, 12:05 AM
Moumn Almunawy
Moumn Almunawy - avatar
3 Answers
+ 4
Because you are printing n after the end of the for loop. n will be equal to the last element in the A list wich is the last element you have append (abs(-6)) Note that A will be equal to [8,0,-4,-6,1,4,6] when the loop is end.
25th Sep 2021, 12:24 AM
Abdelrahman Tlayjeh
25th Sep 2021, 12:26 AM
Gordon
Gordon - avatar
+ 1
The code in the for loop appends 4 and 6 to A. That makes 6 the last value in A and after the last iteration of the loop n is the last value in the A. Also: https://code.sololearn.com/W3uiji9X28C1/?ref=app
25th Sep 2021, 12:16 AM
Simon Sauter
Simon Sauter - avatar