Introduction to Programming (Python) TA Tendency for beginners

This article is the 8th day article of DSL Advent Calendar 2019.

This article may be more of a report for programming educators. Also, I am not trying to criticize a specific person or group, so it would be helpful if you could understand that.

From this year, a lecture called "Introduction to Programming" has started for first-year undergraduate students of all departments of Muroran Institute of Technology. In this class, you will systematically learn programming and data processing using the programming language "Python". The exercise uses the Jupyter Notebook provided by National Institute of Informatics (NII). In addition, one .ipynb of exercises and one assignment is distributed for each lesson so that students can work on the questions in a question-and-answer format.

I participate in the lecture as a TA (Teaching Assistant). I would like to introduce some points that programming beginners who noticed in that process especially stumble when learning Python. I hope that people and faculty members in the same position who have seen this article will find it useful.

Causes of common mistakes

  1. Entering in full-width
  2. I can't be aware of indentation blocks
  3. I can't be aware of the type
  4. The content of the error cannot be read

I will introduce what kind of mistakes I have made about these and what kind of measures should be taken with examples.

Entering in full-width

Example

--Full-width space --Enter symbols, parentheses, etc. in full-width and then convert (parentheses → (), etc.)

Full-width input is a common thing for beginners in programming. If there is a double-byte space, an error will occur and the program will not work. Also, note that Python can be easily used as str even if it is a double-byte character. Those who should be especially careful are those who are typing in basic full-width characters. Especially in modern times, there are some people who are not accustomed to keyboard input rather than programming, as it is said to be the smartphone generation. In that case, it is not possible to know the location of the symbol.

solution

--Get used to the PC keyboard --Make a question sentence that does not require full-width input

I think the best solution is to get used to the keyboard. Other than that, it may be better to make the problem itself a problem that does not require full-width input. (For example, all the parts that output Japanese sentences are changed to English)

I can't be aware of indentation blocks

Example

I often see this mistake.

for i in range(10):
    for j in range(10):
    print(f'i + j = {i + j}')  #error:The indent is out of alignment

Originally, I had to lower the indent with print (), but it was not lowered correctly. If it is a double for statement like the above, there is no mistake so much, but it seems that the number of mistakes will increase as the number of nested structures increases, such as the inclusion of conditional branches such as if.

This is promoted by the program copy / paste and jupyter auto-indent function. It seems that many people are stumbling when copying and pasting a program, rather than pulling in another person's program, when designing a problem by using a part of the previous problem and incorporating it into the next problem. For example, if the problem is that only the conditional branch is created in the first question and it is incorporated in the for loop in the second question, the nest structure may be out of order with the auto indent function and it may not work. In addition, I see patterns in which the structure becomes more and more difficult to understand when indentation is taken in multiple spaces.

solution

--Understanding the nesting structure --Tab key to indent --Read the error

I think that these three points will usually work. In Python, unlike C language, loops are controlled by indentation instead of parentheses, so special attention should be paid to the nesting structure. It is becoming important that the order will tell us first the importance of indent Whatever the program. Also, using the Tab key makes the completion function and indentation work a little easier, so I think it is important to teach how to use it early to solve this problem. And reading the error is the best solution, but I think it is essential to understand the structure because it is not possible to know what is wrong or how it is wrong depending on the code.

I can't be aware of the type

Example

This is such a mistake.

#pattern 1
a = 1
b = "1"
print(a + b)  #error: int +str cannot
print(a + int(b))  # 2

#Pattern 2
a = (1, 2, 3)
b = [1, 2, 3]
a.append(4)  #error:Tuples cannot append
b.append(4)  # [1, 2, 3, 4]

Pattern 1 is a common one for programming beginners, but I think that you have to be especially careful if you are a programming language that Python is learning for the first time because you declare variables after declaring types in languages other than Python. .. The same is true for pattern 2, where a is a tuple and b is a list, but when you want to add elements to these "arrays", append is used, but if you are a beginner in programming, even if the array is a list, it is a tuple. It seems that it (even in a dictionary) looks almost the same.

solution

--Type confirmation using type () --Clarify the differences in methods and properties for each type --Read the error --Declare types explicitly using the typing module

I don't know the difficult story of the type, so I'm sorry, but it's okay because it's quick to check the type of variables etc. that I basically don't understand in Python with type (). Also, if you understand the difference between types, the above should not happen, so it may be better to spend more time teaching. And since this can also be solved by reading the error, the error display is still great. The rest is about the typing module. This depends on the Python environment, but this usage alone is likely to take about an article, so I will omit a detailed explanation, but using the typing module may make you more aware of the type. ..

I can't read the details of the error

It is especially difficult to give an example here, so I would like to propose a little solution. Also, this may be the most important because most of the problems listed above should be solved just by being able to read the error.

solution

--Summary of expected error sentences and their solutions --Get in the habit of searching the Web for the details of the error as it is

I think these two points can be easily done. As long as the content of the error solves the same problem, it is thought that most people often stumble at the same place. Therefore, I think it is good to summarize the expected error contents and the countermeasures in advance. Regarding Web search, programming beginners do not have the habit of googled errors. With Python, most errors can be solved by simply copying the error statement and copying the search results, so it may be easier to make such a habit.

Other causes of unsolvable problems

From here, I would like to briefly list what seems to be a mistake caused by using teaching materials and jupyter rather than the cause of the mistake. I think this is a problem that can be easily solved by both the teaching side and the teaching side by understanding the specifications of jupyter.

  1. If there is no error in the Jupyter cell, I misunderstand that it is the correct answer
  2. The problem is divided into multiple cells and the result is not as expected.

If there is no error in the Jupyter cell, I misunderstand that it is the correct answer

In the currently provided Jupyter Notebook environment, when cell is executed, green is displayed if there is no error, and red is displayed if there is an error. Basically, there is no problem if all green is displayed, but on the contrary, it may be hindering the progress. For example, in a cell that defines only a function, even if the behavior of the contents is a little strange, no error is displayed, so the cell can be executed without any problem. Therefore, there is a problem that you do not know where to look when an error occurs after performing some processing using that function in the lower cell.

The problem is divided into multiple cells and the result is not as expected

This problem is related to ipynb distributed by my university in the lecture, and it is closely related to the above problem, but the problem is divided into multiple cells and the behavior when executed is strange. .. Please refer to the image below for an example.

スクリーンショット 2019-12-08 4.53.51.png

If each problem depends on the execution result of the above cell as above, the behavior may be suspicious. In the above example, q3 is asked using the list and function created by q1 and q2, but in reality, it may be a more complicated process or problem. What's wrong with this is that the cell execution order is only assumed to be from top to bottom. As a result, if you execute it again, the result may be different from the existing output. Also, when the notebook stops responding due to a server error etc., it may be necessary to restart the kernel, but many people only execute it from the middle of the cell. (This is because I don't understand that all variables etc. are reset by rebooting)

Since the specification of this jupyter is awkward, I think it is safe to combine one problem into one cell to solve this.

Summary

I briefly mentioned what I thought when I was doing TA for the introduction to Python programming, but I had the impression that I often stumbled because the teachers did not give enough consideration. I'm tired of writing for too long, so I'm going to put a brush here, but in reality, I'm stumbling on a more trivial point, so it's still difficult to teach programming. felt. I hope this article helps someone.

Recommended Posts

Introduction to Programming (Python) TA Tendency for beginners
An introduction to object-oriented programming for beginners by beginners
An introduction to Python Programming
~ Tips for beginners to Python ③ ~
Introduction to Python For, While
An introduction to Python for non-engineers
[Python] Introduction to graph creation using coronavirus data [For beginners]
An introduction to Python for machine learning
An introduction to Python for C programmers
IPynb scoring system made with TA of Introduction to Programming (Python)
Memo # 4 for Python beginners to read "Detailed Python Grammar"
Python for super beginners Python for super beginners # Easy to get angry
Memo # 3 for Python beginners to read "Detailed Python Grammar"
Introduction to Python language
Memo # 1 for Python beginners to read "Detailed Python Grammar"
Introduction to OpenCV (python)-(2)
python textbook for beginners
Try to calculate RPN in Python (for beginners)
[Introduction to Udemy Python3 + Application] 43. for else statement
Memo # 2 for Python beginners to read "Detailed Python Grammar"
Memo # 7 for Python beginners to read "Detailed Python Grammar"
Python Beginner's Guide (Introduction)
Memo # 6 for Python beginners to read "Detailed Python Grammar"
How to make Python faster for beginners [numpy]
[For beginners] How to study programming Private memo
OpenCV for Python beginners
Memo # 5 for Python beginners to read "Detailed Python Grammar"
[Introduction for beginners] Working with MySQL in Python
[For beginners] Introduction to vectorization in machine learning
Introduction to Graph Database Neo4j in Python for Beginners (for Mac OS X)
Understand Python for Pepper development. -Introduction to Python Box-
[For beginners] How to use say command in python!
Python that I would like to recommend to programming beginners
[For beginners] How to study Python3 data analysis exam
[Introduction to python] A high-speed introduction to Python for busy C ++ programmers
Preparing to start "Python machine learning programming" (for macOS)
[Introduction for beginners] Reading and writing Python CSV files
Python # How to check type and type for super beginners
[Introduction to Python] How to write repetitive statements using for statements
AOJ Introduction to Programming Topic # 1, Topic # 2, Topic # 3, Topic # 4
Introduction to Python Django (2) Win
Learning flow for Python beginners
Python3 environment construction (for beginners)
3 Reasons Beginners to Start Python
AOJ Introduction to Programming Topic # 5, Topic # 6
Python #function 2 for super beginners
Basic Python grammar for beginners
[Introduction to Python] <list> [edit: 2020/02/22]
Introduction to Python (Python version APG4b)
100 Pandas knocks for Python beginners
Python for super beginners Python #functions 1
Python #list for super beginners
Tips for Python beginners to use Scikit-image examples for themselves 4 Use GUI
[Introduction to Udemy Python3 + Application] 42. for statement, break statement, and continue statement
■ Kaggle Practice for Beginners --Introduction of Python --by Google Colaboratory
[Explanation for beginners] Introduction to convolution processing (explained in TensorFlow)
How to learn TensorFlow for liberal arts and Python beginners
How to convert Python # type for Python super beginners: int, float
[For beginners] Script within 10 lines (4. Connection from python to sqlite3)
Introduction to Python for VBA users-Calling Python from Excel with xlwings-
Tips for Python beginners to use the Scikit-image example for themselves