Python For Loops Are Cool
Let's look at a for loop in Javascript
for (i = 0; i < 3; i++){
console.log(i);
}
> 0
> 1
> 2
Now let's write that same loop in python
for i in range(0, 3, 1):
print(i)
> 0
> 1
> 2
The range command is flexible
for i in range(4, -4, -2):
print(i)
> 4
> 2
> 0
> -2
If it isn't flexible enough, you can run for on lists
for i in [1, -2, 14, 24]:
print(i)
> 1
> -2
> 14
> 24
Lists can have more than integers
for i in [1, -2, 3.14, "foo", [4]]:
print(i)
> 1
> -2
> 3.14
> foo
> [4]
def f(x):
return x
def g(x):
return x + 1
def h(x):
return x * 2
list = [f, g, h]
for i in list:
print(i(5))
> 5
> 6
> 10