0

Python For I in

In python it is creating a list, and it uses this: [i**3 for i in range(5)] The thing is that it never told me what any of those things were, and what customizing things do. What variable is i? where did this variable come from? what is the **3? And what does in range(#) mean??? Sorry if this seems so noob, but nobody has explained it well enough yet, so if someone could please tell me what this is as if you are speaking to a twelve year old (im 17), please do!!! Thanks a ton -Lukaka

19th Dec 2016, 2:10 PM
Lucas
Lucas - avatar
3 ответов
+ 3
Hi Lucas. "i" is just the name of a variable, that will take values as is iterates thru the range. "range(5)" is shorthand for 0..4, meaning a loop that starts with 0, ends with 4 (not 5!), and increments by 1 every loop step. And "**" is the power operator (or exponential operator). For example, 2**3 = 2*2*2 = 8. So basically, "[i**3 for i in range(5)]" computes i**3 when "i" takes values 0, 1, 2, 3 and 4.
19th Dec 2016, 3:12 PM
Álvaro
+ 1
Okay Thank you a bunch Alvaro! That really clears it up! i**3 is what it does, while in the background it is doing i in range (5) and setting the result as i
28th Dec 2016, 1:40 PM
Lucas
Lucas - avatar
0
i is a placeholder (variable) for whatever is to be printed. in the example below i = hello! without you having to type i = hello and then the rest of the code: for i in range(5): print("hello!") Result: >>> hello! hello! hello! hello! hello! >>>
24th Feb 2017, 8:30 AM
Jamal Bee
Jamal Bee - avatar