0
L = [] for i in range(1,5,2): if i**2 >= 9: L.append(i) print(L[-1])
can anyone explain about this code?
5 Antworten
+ 5
L — is an empty list.
for i in range(1,5,2):
This will make a range of numbers: 1, 3
It will start from 1, and go up to 5, each time it will increase by 2 (3 arguments of range function)
if i**2 >= 9: L.append(i)
This will take values of range function within for loop and make them to the power of two i.e. square them. If the result of that is greater than or equal to 9, the value of "i" will be added to the list called "L"
print(L[-1]) — will just output the last item of the list.
Example here:
https://code.sololearn.com/cnh4iif48yky/?ref=app
+ 1
Lidwina Harefa
Lamron has given a good explanation, let me try to show you in a different way
https://code.sololearn.com/c8yL4gwj5627/?ref=app
+ 1
Where did you get the code?
What is the level of your python knowledge?
To understand it, you must first know about list, the append method and range function.
0
I still don't understand😢