Control flow and loops

Python for R Users

Daniel Chen

Instructor

Whitespace matters

Whitespace and indentation is a fundamental part of writing python code and is not optional

Python for R Users

Control flow

R
if (5 == 5) {
     print('TRUE')
 }
"TRUE"
Python
if 5 == 5:
    print('True')
True
Python for R Users

if-elif-else

R
val <- 2

if (val == 1) {
    print('snap')
} else if (val == 2) {
    print('crackle')
} else {
    print('pop')
}
"crackle"
Python
val = 2

if val == 1:
    print('snap')
elif val == 2:
    print('crackle')
else:
    print('pop')
crackle
Python for R Users

Loops

R
num_val <- c(1, 2, 3, 4)

for (value in seq_along(num_val)){
    print(value)
}
Python
num_val = [1, 2, 3, 4]

for value in num_val:
    print(value)
Python for R Users

Let's practice!

Python for R Users

Preparing Video For Download...