[Python] Looking back on what I taught programming beginners from functions

Not limited to Python, when it comes to introducing programs, after environment construction, Hello World, and four arithmetic operations,

  1. Conditional branch by if ... else
  2. Iterative processing of for and while After that
  3. Function I think that often comes out.

But personally, is it best to teach beginners in this order in order to "create a change-resistant business program"? I always thought.

So, when I got a chance to get started with python programming from a small team, I taught them from "functions", which are not general textbook procedures. Then, I first tried to put in the idea of not using "else" in the if statement.

We will look back on the process of the response and summarize what we noticed.

The following is just a description that is obvious to those who are familiar with it, and there are no new techniques or topics. In addition, we will summarize what happened after the environment construction, Hello World, and the four arithmetic operations were completed.

background

One of the members was a beginner in programming, but there was also a member who wrote a program for his own research using "R" and so on.

The team's ultimate goal was "to be able to finally create a business system," so I explained first that the following three points should be emphasized as a starting policy.

――In order to reduce bugs, the curriculum is constructed with the idea that reducing the number of branches is the first priority. ――Assuming that changes will always occur, think together about what programs are resistant to changes. —— Think about the “readability” of programs created by other members and programs you created a few months ago.

environment

theme

The team was aiming for a program to be used for a certain task, so the theme for the introduction was created according to the task theme.

--Calculate the per capita rate according to the number of people according to the price list.

Number of people Fee
less than 10 980
11~20 840
21 or more 720

The first thing I taught was a function

We asked them to think about what they needed as input to get the desired price. Then, I taught how to write a "function" with the target value as the "return value" and the input as the "argument".

fee_calculate.py


#skeleton
def fee_calculate(num): #Function to calculate the charge from the number of people
    #do_something
    fee = 980 #To move for the time being
    return(fee)

num = 5 #Set the number of people
fee = fee_calculate(num) #Find the price using a function
print(fee) #"980" to display the result

There is nothing inside the function, but I imprinted that "the desired value can be obtained by using the function".

The next thing I taught you is not to use else

Next, I taught how to determine the price according to the number of people. In addition, he introduced the idea that "you should be able to read various ways of writing if statements, but you do not have to actively use them yourself."

fee_calculate.py


#Charge calculation
def fee_calculate(num): #Function to calculate the charge from the number of people
    if 1 <= num <= 10:
        fee = 980
        return(fee) #Return as soon as the result comes out

    if 11 <= num <= 19:
        fee = 840
        return(fee) #Return as soon as the result comes out

    if 20 <= num:
        fee = 720
        return(fee)

num = 5 #Set the number of people
fee = fee_calculate(num) #Find the price using a function
print(fee) #"980" to display the result

If you ask for a fee, just return. We discussed with the members how easy it is to write, read, and change compared to anti-patterns that use ʻelse and ʻelif.

#Anti-pattern
def fee_calculate_a(num):
    if 1 <= num <= 10:
        fee = 980
    elif 11 <= num <= 19: #Try using elif
        fee = 840
    else: #Try using else
        fee = 720
    return(fee) #Return result

num = 21 #Set the number of people
fee = fee_calculate_a(num) #Find the price using a function
print(fee) #"720" to display the result

I thought that with such a small program, the difference in ease of writing, readability, and changeability might not be felt ... gave. As a result, it was helpful because it became a place for participants to exchange opinions and think. The team's opinions are democratically decided, so it seems that the beginner members thought, "This is what it is."

Awareness from what I learned from the function

By learning the idea of "getting the value you want by using a function" and remembering how to call a function, I felt that I could have started from an early stage to try using the library. There is. (For example, use calender to find the number of days in a month.)

It is important to think about and create algorithms by yourself, but how do you find and use many "convenient libraries" in the world, and how do you combine them? I think it is important to learn such things if you are learning a programming language now.

What happened after this?

I learned about Python in the form of study sessions divided into several times, but I think that the beginner members are growing steadily. In other words, the behavior of "thinking about what you want to do and first google to find the library" is born. If you understand how to use the function, there are many libraries that you can use if you remember ʻimport`.

In the study session, while practicing to give a theme, think about how to solve it, and create a program, "test", "error handling", and "log output" that are indispensable for creating a business system We are learning together the way of thinking and how to deal with it.

Python3 has a lot of libraries for "testing" tools, "error handling" and "log output", but it is important to learn and think at an early stage how it is made and how it is better to use. Because it becomes.

When did you teach the repetition

To conclude, everyone was able to do it without telling them clearly. Once you learn how to learn, it's faster to learn by yourself than to teach.

By the way, in the charge calculation program mentioned earlier, repeat is used when you want to process multiple applicants at once.

The sample for repeatedly calculating the charge for the list of the number of applicants is as follows.

#repetition
list = [5, 11, 21, 7, 15, 30] #List of number of applicants

def fee_calculate(num): #Function to calculate the charge from the number of people
    if 1 <= num <= 10:
        fee = 980
        return(fee)

    if 11 <= num <= 19:
        fee = 840
        return(fee)

    if 20 <= num:
        fee = 720
        return(fee)

for num in list: #Extract the contents of the list one by one
    fee = fee_calculate(num) #Find the price using a function
    print(fee) #View results

Things that tend to happen when you learn repetition first

The idea is to turn the loop first, and later the function cannot be cut out. (Because it's nothing more than a hassle to function later in this procedure.) I personally think that it will be difficult for you to improve your programming skills if you only make this kind of program except when you make a disposable program.

#Anti-pattern
list = [5, 11, 21, 7, 15, 30] #List of number of applicants

for num in list: #Extract the contents of the list one by one
    if 0 <= num <= 10: #Judge the number of people taken out ...
        fee = 980
    elif 11 <= num <= 19:
        fee = 840
    else:
        fee = 720
    print(fee)

Comparing the two programs, the shorter one is the anti-pattern, but the one that is easier to read, I hope it gives you an opportunity to think about that.

Personally, it's easy to write ʻelse and ʻelif, but when reading, I find it awkward to see the previous conditions. Even in Japanese, it is easier to understand if you say "This is the case." When people say, "In other cases like this," it becomes "Which one?" Therefore, I try not to use ʻelse and ʻelif, which are not inevitable.

reference

Branch anti-pattern

Recommended Posts

[Python] Looking back on what I taught programming beginners from functions
Looking back on Python 2020 around me
I looked back on Python classes again
What Python beginners got hooked on with Django
What I did when updating from Python 2.6 to 2.7
Python Beginner's Guide (Functions)
Python that I would like to recommend to programming beginners
Looking back on ABC155
I sent regular emails from sendgrid on heroku, on python
I thought about a Python beginner's course on blockchain games
What beginners learned from the basics of variables in python
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
Looking back on iOS'Healthcare App' 2019
Python3 programming functions personal summary
Python for super beginners Python #functions 1
I ran python on windows
What I learned in Python
What beginners think about programming in 2016
Notes on accessing dashDB from python
Call C / C ++ from Python on Mac
Update Python on Mac from 2 to 3
I refactored "I tried to make Othello AI when programming beginners studied python"
Looking back on 2019 ~ One year since I started posting to Qiita ~ + Where I got stuck with python pip related