SYSTEM NOTICE

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

30 Days of Python #7: Naming a Set of Tasks (Functions)

"Reading Data" is a series on Python and machine learning for clinicians and medical researchers. We start by acquiring programming skills to build the power to "read and interpret" your own data. In this #7, we will learn about "functions," which allow you to group and call frequently used processes. We are finally in the final stages of the basics. The required time is 5 minutes: 3 minutes to read and 2 minutes to practice.


Do you rewrite the same steps every time?

Imagine a situation where you calculate BMI. You divide weight by the square of height. If you calculate for the first person, write the same formula for the second, and then for the third... if you keep repeating the same steps, the risk of typos in the formula increases, and above all, it is tedious.

In clinical practice, we name and share such fixed procedures. If you say "calculate BMI," the person calculating can provide the result without checking the internal calculation every time. Because the procedure itself has a "common name," you can call it without explaining the contents every time.

Programming has the same mechanism. You name a series of processes and call them by that name when needed. That is a function.


Key Points

  • A function is a mechanism that allows you to name a series of processes and call them as many times as you like. You create one with "def name():".

  • You pass "ingredients (arguments)" inside the parentheses and return the "result" with return. If you change the ingredients, the same function can be used for anything.

  • Once created, you can call it by name without worrying about the internal steps. print( ) and append( ) are also functions prepared in this way.


1. Naming a process

Let's create a function to calculate BMI in a new cell.

def bmi(weight, height):
    return weight / (height ** 2)

Even if you execute it, nothing is displayed. This is because you have only declared "I have defined a function named bmi." The contents have not run yet.

Reading the code, def bmi(weight, height): means "create a function named bmi that receives two ingredients: weight and height." ** is "exponentiation," and height ** 2 represents the square of height. return is an instruction to "return this result."

2. Calling by name

The created function is called by passing ingredients to the name. Let's calculate for a height of 1.7m and a weight of 65kg.

bmi(65, 1.7)

When you execute this,

22.49...

is displayed. 65 was put into weight and 1.7 into height, and the internal formula was calculated and returned. If you change the ingredients, you can output the BMI of another person in one line.

bmi(80, 1.75)
26.1... 

appears. There is no need to rewrite the formula. You just call the once-created bmi by its common name.

3. You were already using functions

Actually, print( ) and append( ) (from Part 4) that we have used so far were functions with the same mechanism. Someone had prepared these functions that perform internal processing in advance, and we were able to use them just by calling them by their common names.

print("BMI:", bmi(65, 1.7))
BMI: 22.49...

It will be displayed. The result of bmi( ) is being passed to print( ).

Passing the result of a function to another function. When we work with machine learning later, we will be calling functions that 'take data and return a prediction.' The way to call them is no different from today's bmi(65, 1.7).


Today's Practice (2 minutes)

Try the following in a new cell.

  1. Create a function that takes two numbers and returns their sum. On the line after def add(a, b):, indent and write return a + b.

  2. Call add(3, 5) and verify that it returns 8.

  3. If you have time, create a function def mean2(a, b): return (a + b) / 2 that returns the average, and verify that mean2(140, 160) returns 150.

The line after def must be indented. This is the same rule as for and if.


Summary of Today

A function is a mechanism for naming a series of processes, passing in ingredients, and receiving a result.

Once created, you can call it by name without worrying about what is inside. The tools we will use in machine learning are, in essence, functions that 'take data and return a result.' As of today, you already have the pattern for how to call them.


There is only one topic left in the basics. Putting values in boxes (variables), arranging them (lists), repeating (for), branching by conditions (if), and naming procedures (functions). You now have the five tools that form the backbone of programming. Next time, we will learn the last one, 'dictionaries,' and then step directly into 'data itself.'

Next time, Part 8: 'Recording a Single Patient in Full'

We will learn about 'dictionaries,' which record items and values as pairs. This is the final polish of the basics and serves as a bridge to the data section.

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