2 Respuestas
+ 1
cubes = [i**3 for i in range(5)] So here is what it does. The code takes values of i in the range(5) meaning it will take 0, 1, 2, 3, 4. Now, for each value that it takes it puts it in the variable i then it does i**3 which is the cube of i, or i*i*i if you want. And when it gets that result it adds it to the list cubes. So: First: i = 0, then it does i**3 = 0 * 0 * 0 = 0 and add it to the list Second: i = 1, then it does i**3 = 1 * 1 * 1 = 1 and add it to the list Third: i = 2, then it does i**3 = 2 * 2 * 2 = 8 and add it to the list Fourth: i = 3, then it does i**3 = 3 * 3 * 3 = 27 and add it to the list Fifth: i = 4, then it does i**3 = 4 * 4 * 4 = 64 and add it to the list So cubes contains 0 1 8 27 64
25th Apr 2018, 12:14 PM
cyk
cyk - avatar
+ 1
It's a for loon. Range(5)= 0,1,2,3,4 i**3= i to the power 3 For using "for" loop. it means: 0^3=0 1^3=1 2^3=8 3^3=27 4^3=64
25th Apr 2018, 12:19 PM
Mahmudul Hasan Fahim
Mahmudul Hasan Fahim - avatar