Retired Programmer Tries AI Programming in Python. 02) A brief description of Python
In this post, I will focus on the Python functions which will be used in a series of my posts. In other words, it does not cover Python as a whole.
The contents are as follows.
1) function
2) for loop
3) if
4) colon
5) class (OOP)
6) list
7) dictionary
Pygame is a library, so I will not explain it here.
First, please see the actual code below.

Functions start with “def”.
A for loop is “for ~ in”.
The “else if “ of if statement is “elif”.
The source code is heavily dependent on the indent.
Parentheses and curly braces are not used. Only indent.
This is very useful, because it allows us for very quick modifications, especially when adding if statements to the existing code.
And at the end of each statement, there is no end if, no end loop or no end func. As for these cases, the indent determines which commands are included in.
If one command has multiple lines such as if-statement and for-loop-statement, the first line must have colon at the end of the line.


Please allow me to add something about indent.
I think this is probably the main feature in the look and feel of the program, as well as what makes us uncomfortable.
I this this is a very easy way to modify my own program. While creating codes for these posts, I got many benefits from this feature.
For example, if I needed to add an if statement when modifying a program, in Python, all I have to do is to add the if statement and reduce the indent of the existing parts.
Doing this in another language, say JavaScript, would be very troublesome, because we have to put it in curly braces.
This is a great benefit of Python.
However, what about when reading codes written by others? Is it easy to understand it?
If the code is very short, it is easy. But if the code is too long to display it on one screen, I doubt I can understand the code easily.
Is the indent alone enough to understand the code correctly for us?
When we were programmers, we put a comment at the end “if” to say, “This corresponds to which “if”.
But since there is no end-if, we can’t do it in Python.
I am very interested in this point. I would like to check how this is handled in the development projects, if I get a chance.
Next, let me explain the classes.
Some of you may have retired from programming without doing object-oriented programming, OOP.
For such people, the concept of OOP is not warmly welcomed.
The 3 keywords of OOP, “inheritance, encapsulation, and polymorphism”, may confuse you a lot.
However, we are not aiming to make a comeback as programmers, so I believe that a more cursory understanding is fine.
The reason I used class is to make coding easier. Specifically, it requires fewer arguments when calling functions. That’s all.
When executing a chess program, many functions are called.
- Check the range of possible movement
- Move pieces
- Check if the king is checked or not
- and so on.
I wanted to reduce the number of arguments of functions, when calling them.
If Class is not used, multiple functions can exist independently. In that case, I need to prepare parameters to be used as arguments for each function call.
But if Class is used, the situation is different.
Inside a class, there can be variables and functions. Functions inside a class can access variables in the same class at any time to get or change their values.
Hence, when calling another function inside the same class, there is no need to include variables as arguments. As a result, the source is shorter and easier to code. This is a very big advantage, which is why I used class this time.
Global variables?
By the way, when some of you hear this explanation, you may be thinking, “What is the difference from global variables?”
In fact, it is essentially exactly the same thing.
One of the hallmarks of object-oriented programming is encapsulation. And what I just mentioned as an advantage of using classes falls under this encapsulation. Encapsulation is the idea of wrapping a class in a capsule. But this has two meanings.
1) To protect the variables in the class from the outside.
2) To shut the vaiables ins the class and protect the peace of outside.
I think that OOP’s explanations often emphasizes only the 1st one.
However, that explanation alone does not explain
- What has been made convenient by OOP.
- What problems have been solved in the field of programming by OOP
I believe that the greatest benefit of this encapsulation is the 2nd one, i.e., protecting the outside.
Encapsulation means, so to speak, “creating global variables” that is effective only within a class. Therefore, when a function is called within the class, the variable does not need to be included in the arguments.
And since this global variable is effective only within the class, it has no negative impact to the outside world. For example, there will be no more nightmares about bugs caused by using the same name for local variables and global variables.
For our generation, global variables are a minefield of programming, along with “GOTO”. I think there were many projects, where the use of global variables was prohibited.
Encapsulation is just focusing on the good point of global variables. I think you can see the advantage of using classes in these posts.
By the way, I called variables and functions inside a class. But in OOP,
A variable is a property.
A function is a method.
In practice, it does not matter how to call them.
Is my program object-oriented?
My answer is NO.
To be honest, I have rarely used object-oriented methods because I don’t see the benefit other than this encapsulation.
For example, some people might think of a chess program as creating a class called Piece, which is then inherited to Knight class, King class and so on. As object-oriented, that idea may be correct.
However, I don’t see any merit in that approach, so I don’t do it. I think a two-dimensional matrix/array to represent a chess board is enough to control everything.
Therefore, my program is not 100% OOP program.
List/Array
As I said earlier, we use a two-dimensional matrix, a matrix with 8 rows and 8 columns, where the elements of one array become elements of another array to achieve a two-dimensional array. The details are explained in the coding section.
It is said that a library “Numpy” is faster to use two-dimensional arrays. However, since I wanted to develop without using any extra library, I did not use it this time.
Dictionary
A function similar to a list is a dictionary. This is to connect keys and values. Some people might have used a table that maps keys to values, calling it as a “table value”. It is essentially the same thing.
I use this dectionary for
- connecting an image and a piece
- connecting a point and a piece for AI
This is the end of the explanation of Python.
Even if you don’t know Python, you know programming. So, I believe that you will be able to write Python code quickly. So please try to type codes by yourself, not copy&paste.
See you in my next post.
I have downloaded the picture of the chess pieces from the URL below.
By jurgenwesterhof (adapted from work of Cburnett) — http://commons.wikimedia.org/wiki/Template:SVG_chess_pieces, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=35634436