SYSTEM NOTICE

Auto translation by AI. Be sure, accuracy, nuances and authorial intent may not be fully reflected.
見出し画像

30 Days of Python #6: If this value (if)

"Reading Data" is a series on Python and machine learning for clinicians and medical researchers. We start by acquiring programming skills to build the ability to "read and interpret" your own data. In this #6, we will learn about "conditional branching (if)," which allows you to split processing based on conditions. This is the first step toward finding only those patients who exceed reference values. The estimated time is 5 minutes: 3 minutes to read and 2 minutes to practice.


Not everyone is treated the same

Last time, we learned how to process the contents of a list one by one. However, in clinical practice, we do not treat everyone the same way.

When we look through blood test results, we unconsciously draw lines. "If potassium is over 5.5, be careful," or "If blood pressure is 140 or higher, it is high." We compare them against reference values; if they exceed them, we stop and take action, and if not, we move on.

We repeat this "if this, then that" branching judgment every day.

We want to make the computer do the same thing. Look at the value and process it only when it meets the condition. That mechanism is the conditional branch, if.


Key points

  • Writing "if condition:" executes the indented code only when that condition is met.

  • Use comparisons for conditions. "Greater than or equal to" is >=, "greater than" is >, and "equal to" is == (two equals signs).

  • By adding else, you can also write code for "otherwise." When combined with for, you can pick out only those who meet the condition from the entire group.


1. Execute only when the condition is met

Let's write this in a new cell and run it.

k = 5.8
if k >= 5.5:
    print("高カリウム血症の疑い")

When you run this,

高カリウム血症の疑い

is displayed. "if k >= 5.5:" means "if k is 5.5 or greater." Since the condition is met, the indented print is executed.

If you try changing it to k = 4.2 and run it, nothing will be displayed this time.

k = 4.2
if k >= 5.5:
    print("高カリウム血症の疑い")

Because it does not meet the condition, the indented content is skipped. Like for, the indentation in if indicates the "range to execute when the condition is met."

Note that when checking for equality, you must use == with two equals signs. Since a single = was used for "putting a value into a box" (Part 2), two equals signs are used to distinguish it during comparison.

2. Adding "otherwise"

To split processing between "when it matches" and "when it does not," use else.

bp = 130
if bp >= 140:
    print("高い")
else:
    print("基準内")

Since bp is 130, it does not meet the condition and goes to the else side

基準内

is displayed. Either if or else, one of the two paths will always be displayed.

3. Combining with loops

This is where it gets interesting. By combining this with the for loop from last time, you can "pick out only the people who meet the criteria from everyone."

bp_list = [120, 145, 138, 162]
for bp in bp_list:
    if bp >= 140:
        print(bp)

When you run this

145
162

Out of the blood pressure readings for four people, only the two with 140 or higher were displayed. We use for to extract them one by one, if to check the condition, and print only those who match. The two levels of indentation represent nesting, meaning "inside the loop, and further inside the condition."

This is the most basic prototype of a process for extracting patients who exceed a threshold from data on thousands of people. When you actually handle data, this way of thinking will always be useful.


Today's practice (2 minutes)

Let's try the following in a new cell.

  1. Prepare age = 70, and display print("Elderly") only when if age >= 65:.

  2. Add else so that it displays print("Not elderly") if they are under 65, and try both paths by changing the value of age.

  3. If you have time, loop through age_list = [60, 72, 45, 80] using for, and try displaying only those who are 65 or older.

Be especially conscious and careful about the number of indentation levels (one level for just if, two levels for if inside for).


Today's summary

if is a mechanism to branch processing based on conditions. By combining it with for, you can pick out only those who meet the criteria from a large group. You can have the computer take over the "if this, then that" judgments you repeat every day in clinical practice. You have now reached the threshold of that capability.


Next time, Part 7: "Giving a name to a set of tasks"

We will learn about "functions," which allow you to group and call frequently used processes. We are finally reaching the end of the basics.

いいなと思ったら応援しよう!