Often when we are programming, we will want to repeat a block of code over and over again. Loops give us an easy way to do this.
A for loop lets us repeat a block of code a set number of times, or iterate all the way through an array and operate on each element. A while loop, on the other hand, allows us to repeat a block of code an indefinite number of times. This means we can start looping without knowing exactly when we will be done. For now, we are going to stick with the for loops.
In Python, the basic structure of a for loop is:
for i in range(3):
do_something(i)
The function range(n) builds an array with n numbers in it, starting at 0. So if n = 3, the array returned by range(n) is:
[ 0 1 2 ]
So, with each iteration of the loop, the variable i will get the next integer in the sequence created by range(3). Notice how there are no curly braces { } or end statements to close the loop. This is because, in Python, indentation matters. Indents in other languages are helpful to make code readable, but indents in Python are necessary to make the code work. Each layer of indent is a new block of code, and if the indentations aren't lined up, your code either won't run, or will not behave well.
Open Spyder, and create a new script:
for i in range(10):
print("Hello, world!")
and run it. If you wanted to add more lines of code to execute inside this for loop, you can just make them indented to the same level as the rest of the loop, and Python treats it all as one block of code.
The range() function is a handy way to make a loop happen a set number of times, but we can do better. Typically, you aren't printing "Hello, world!" 10 times to your console. Usually, you will be modifying some array or matrix by looking at each entry in it. This is where Python's for loop shines. Instead of created a dummy variable, i, and an artificial range() value, we can access the array we want to modify directly.
Take this for example:
prime_numbers = [2, 3, 5, 7, 11]
for num in prime_numbers:
print("The next prime number is: ", num)
This loop gets each entry in the prime_numbers array and prints it to the console. This makes iterating through a dataset readable for others and you! It also happens to be more efficient than using range().
A for loop lets us repeat a block of code a set number of times, or iterate all the way through an array and operate on each element. A while loop, on the other hand, allows us to repeat a block of code an indefinite number of times. This means we can start looping without knowing exactly when we will be done. For now, we are going to stick with the for loops.
In Python, the basic structure of a for loop is:
for i in range(3):
do_something(i)
The function range(n) builds an array with n numbers in it, starting at 0. So if n = 3, the array returned by range(n) is:
[ 0 1 2 ]
So, with each iteration of the loop, the variable i will get the next integer in the sequence created by range(3). Notice how there are no curly braces { } or end statements to close the loop. This is because, in Python, indentation matters. Indents in other languages are helpful to make code readable, but indents in Python are necessary to make the code work. Each layer of indent is a new block of code, and if the indentations aren't lined up, your code either won't run, or will not behave well.
Open Spyder, and create a new script:
for i in range(10):
print("Hello, world!")
and run it. If you wanted to add more lines of code to execute inside this for loop, you can just make them indented to the same level as the rest of the loop, and Python treats it all as one block of code.
The range() function is a handy way to make a loop happen a set number of times, but we can do better. Typically, you aren't printing "Hello, world!" 10 times to your console. Usually, you will be modifying some array or matrix by looking at each entry in it. This is where Python's for loop shines. Instead of created a dummy variable, i, and an artificial range() value, we can access the array we want to modify directly.
Take this for example:
prime_numbers = [2, 3, 5, 7, 11]
for num in prime_numbers:
print("The next prime number is: ", num)
This loop gets each entry in the prime_numbers array and prints it to the console. This makes iterating through a dataset readable for others and you! It also happens to be more efficient than using range().