For Loops [R,Python]

for loops are dedicated to iterate though a specific number of things. Those things can be lists, vectors, etc. We can either set a specific number of iterations or have a loop go though a set list of something and perform a set operations on each element of the list. For example, we can go through a list of values and perform a specific operation such as adding/subtracting/dividing etc. or capitalizing each of the elements. Let’s see how thing can be done with R and Python:

[R]
values <- c(1,2,3,4,5)
# Let's create a for loop
for (i in values){
    print(i + 1)
}

[output]
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6

# The output of the above for loop is a value with an added 1 to it. 
[Python]
values = [1,2,3,4,5]

# Let's create a for loop
for i in values:
  print(i + 1)

[output]
2
3
4
5
6

Leave a comment