filmov
tv
Python for Neuroimagers, Part 3: Conditional Statements & For-Loops

Показать описание
Control statements allow you to run certain blocks of code using conditional statements, or to automatically assign each element of a list to a variable using a for-loop. Both types of control statements act like traffic signs to direct your code and make it more efficient. In this video we will learn how to code both types of statements, and understand the concepts in greater detail.
--------------
Code used (NOTE: "Greater than" and "Less than" signs are not allowed in YouTube descriptions, so I have replaced them with the strings "gt" and "lt" below. When running it as your own code, replace them with greater than and less than signs):
my_List = ["Hello", 21, "Python"]
my_List[0]
my_List[1]
my_List[2]
for x in my_List:
print(x)
num_elems = len(my_List)
range(num_elems)
for i in range(num_elems):
elem = my_List[i]
print(f"Element {i}: {elem}")
my_Comp = [print(x) for x in my_List]
money = 8.0
if money gt 10.0:
print("Get the extra guac")
elif money lt 8.0:
print("Go hungry")
else:
print("Get the regular chicken burrito")
subjects = ["sub-01", "sub-02", "sub-03", "sub-04", "sub-05"]
outlier_subject = ["sub-02"]
for sub in subjects:
if sub == outlier_subject[0]:
print(f"Subject {sub} is an outlier, skipping preprocessing")
else:
print(f"Processing subject {sub}")
subjects = ["sub-01", "sub-02", "sub-03", "sub-04", "sub-05"]
pipelines = ["preprocessing", "denoising", "QA checks"]
for sub in subjects:
for pl in pipelines:
print(f"Running pipeline {pl} for subject {sub}")
Table of Contents
==============
0:00 What are Control Statements?
0:48 For-Loops
1:32 The Range Command
1:50 String Formatting
2:19 Comprehensions
2:47 Conditional Statements (feat. Burrito)
4:22 Nesting Conditional Statements within For-Loops
5:15 Nesting For-Loops within other For-Loops
5:57 Summary
--------------
Code used (NOTE: "Greater than" and "Less than" signs are not allowed in YouTube descriptions, so I have replaced them with the strings "gt" and "lt" below. When running it as your own code, replace them with greater than and less than signs):
my_List = ["Hello", 21, "Python"]
my_List[0]
my_List[1]
my_List[2]
for x in my_List:
print(x)
num_elems = len(my_List)
range(num_elems)
for i in range(num_elems):
elem = my_List[i]
print(f"Element {i}: {elem}")
my_Comp = [print(x) for x in my_List]
money = 8.0
if money gt 10.0:
print("Get the extra guac")
elif money lt 8.0:
print("Go hungry")
else:
print("Get the regular chicken burrito")
subjects = ["sub-01", "sub-02", "sub-03", "sub-04", "sub-05"]
outlier_subject = ["sub-02"]
for sub in subjects:
if sub == outlier_subject[0]:
print(f"Subject {sub} is an outlier, skipping preprocessing")
else:
print(f"Processing subject {sub}")
subjects = ["sub-01", "sub-02", "sub-03", "sub-04", "sub-05"]
pipelines = ["preprocessing", "denoising", "QA checks"]
for sub in subjects:
for pl in pipelines:
print(f"Running pipeline {pl} for subject {sub}")
Table of Contents
==============
0:00 What are Control Statements?
0:48 For-Loops
1:32 The Range Command
1:50 String Formatting
2:19 Comprehensions
2:47 Conditional Statements (feat. Burrito)
4:22 Nesting Conditional Statements within For-Loops
5:15 Nesting For-Loops within other For-Loops
5:57 Summary