L = [] for i in range(1,5,2): if i**2 >= 9: L.append(i) print(L[-1]) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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?

3rd Jan 2023, 8:45 PM
Lidwina Harefa
Lidwina Harefa - avatar
5 Answers
+ 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
3rd Jan 2023, 9:08 PM
Lamron
Lamron - avatar
+ 2
Bob_Li , that's from python challenges, where players compete against each other
4th Jan 2023, 10:16 AM
Lamron
Lamron - avatar
+ 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
4th Jan 2023, 8:09 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 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.
4th Jan 2023, 9:32 AM
Bob_Li
Bob_Li - avatar
0
I still don't understand😢
4th Jan 2023, 1:11 AM
Lidwina Harefa
Lidwina Harefa - avatar