KwicKode Tutorial Part C: Lists and File I/O Commands
Order Kwickode Today:
KwicKode – Springboard to Python
Go to Tutorial Part A: The Basics
Go to Tutorial Part B: Loops and Functions
Go to Home Page
| Caution: This tutorial presents only a cursory overview of the KwicKode commands. It in no way purports to teach programming. Designing an application and details of program structure are covered in more detail in my book, KwicKode-Springboard to Python. |
In Part A you leaned eight basic KwicKode commands and in Part B you added the four commands associated with loop blocks and function blocks. In this section, more details and three commands relating to lists will be covered, as well as the two KwicKode commands that are necessary for file input and output that are essential for retrieving and storing data permanently to disk.
List Commands and Control
Lists are such an important part of computer programming data structures that they usually require several dedicated commands in most languages. They are often referred to as ‘arrays’ in languages more oriented toward mathematics operations, in that lists may be composed of lists, creating 2-dimensional arrays of data, similar to spreadsheets.
As previously mentioned, a list is a group of individual items grouped together under a single variable name. In many languages, the type of information must all be the same, such as all numbers, or all text strings, etc. In KwicKode (as in Python), you are free to mix and match elements of all types in your lists.
Individual items within a list are accessed by attaching an index number enclosed in square brackets immediately after the variable name. So, if your list ‘member_names’ includes 957 member names for your organization, then ‘member_names[258]’ will contain the name of the 259’th member in the list. (Recall that indexing always begins at zero.)
An important Python function that you can use in KwicKode commands is ‘len(…)’, which returns the number of items in a list. Using the above example, ‘Assign len(member_names) to member_count.‘ would place the value 957 in the variable ‘member_count’. Once this command has been executed, you can write code for a loop that cycles through the entire list of members with the following command:
[Repeat while indx < member_count with indx:
Since the counting variable ‘indx’ starts at zero, it will cycle 957 times from zero to 956. At the end of the 956th cycle, indx is incremented by +1 and the program returns to the ‘[Repeat …’ line, at which point indx, now having a value of 957, will no longer be less than the value in ‘member_count’ and the loop ends. (Note: If the variable ‘member_count’ isn’t used anywhere else in the program, the ‘len(…)’ function can be used inside the ‘[Repeat…’ conditional expression, eliminating the ‘Assign…’ statement entirely.)
Create list xyz. This KwicKode command does exactly what it says. It creates the variable name ‘xyz’ and gives it list status. The same thing happens automatically when you use an ‘Assign…’ command with the word ‘list’, as described earlier. The difference is that, if ‘xyz’ already exists, the ‘Create…’ command empties the list whereas the ‘Assign…’ command appends the new values to the end of the existing list. So, it is mainly used to delete the contents of a list so it can be used again.
Add abc, mno, qrs to list xyz. This is essentially a redundant command, producing exactly the same result as ‘Assign abc, mno, qrs to list xyz.’ If the xyz doesn’t yet exist, it is created. If it does exist, the new values are appended onto the end of the existing list.
Remove qrs from list xyz. As the command implies, this removes the first occurrence of the value represented by qrs from the xyz list. If the value appears more than once, only the first occurrence, the one with the lowest index number, is removed. If the object isn’t found, an alert message is generated and the program continues unaffected.
In addition to the ‘len(…)’ function, if all the items in the list are numbers, these three Python functions can be used: ‘sum(…)’, ‘max(…)’, and ‘min(…)’. So the following command can produce the average:
Assign sum(number_list) / len(number_list) to list_average.
Finally, a frequent operation with lists is locating a specific item in the list. This is where the index numbers come in handy. Going back to the ‘member_names’ list, suppose you want to retrieve the records for a specific member. The code below provides an example on how this might be done. (The accent marks ```` are used to force Word Press to recognize tab indents.)Comment: Example for an indexed search.
Assign "Dan", "Ann", "Joe", "Sally" to list member_names.
Display "Enter the last name for the person you are searching for...".
Get string for search_name.
Assign "False" to found.
[Repeat block while indx < len(member_names) with indx:
````[If member_names[indx] == search_name then:
````````Assign "True" to found.
````````Display search_name, " is member number ", indx + 1," in the list.".
````````End of if block.]
````[If found == "True" then:
````````Assign len(member_names) to indx.
````````End of if block.]
````End of repeat block.]
[If found == "False" then:
````Display search_name, "is not in your member list.".
````End of if block.]
Note that other languages in which the loop counter is lost when the loop terminates require additional code within the loop that assigns the value of indx to a second variable if a match is found. Since KwicKode preserves indx outside the loop, this additional coding isn’t necessary. But note also that the conditional expression for the loop must also include a test for when the last name in the list has been tested and the loop must be terminated without a match.
File Input and Output Commands
Create file xyz.txt. or Create file xyz.csv. These two commands work the same way as the ‘Create list…’ command in that, if the file already exists, it is emptied of any data it might contain. Your choice of text file or comma-separated-variable file depends on whether you are saving lines of text, as in a document, or other types of data. Once the file has been created, it can then be used to save data from your computer using:
Copy data from mno to xyz.txt. or Copy database from mno to xyz.txt.
Copy data from mno to xyz.csv. or Copy database from mno to xyz.csv.
where ‘mno’ is a variable that contains text or other information to be saved. For .csv files, ‘mno’ may hold a single value or it may be a list. The values are appended to the existing values already in the file. If ‘mno’ represents a database (a list of lists, as described in detail in the book) use the word ‘database‘ in place of ‘data’ in the above command for .csv files.
The same commands, but with the variables reversed, is used to retrieve data from a disk file into the program.
Copy data from xyz.txt to mno. or Copy database from xyz.txt to mno.
Copy data from xyz.csv to mno. or Copy database from xyz.csv to mno.
It should be noted that the single ‘Copy data…’ KwicKode command line requires over a dozen lines of rather arcane Python code to achieve the same result.
Final Notes
You have now covered all of the sixteen commands and variations that define KwicKode. Additional examples and notes are available in the book KwicKode – Springboard to Python. Refer to the Table of Contents presented on an earlier page. Examples include a complete interactive Tic-Tac-Toe for Two program and a full-fledged database program that could easily be adapted to handle any number of common application.
You may find that KwicKode covers all of your programming needs. However, since its foundation is based on Python, you will find the transition to Python particularly easy, should you decide to go that route. In fact, the last chapter in the book is focused on a comparison between the two languages and will facilitate a transition to Python.
There are two things you will need to set up before you can proceed with KwicKode. First, you need to go to https://python.org and download the Python environment on your computer, which includes the Idle text editor in which you will type in your KwicKode command lines. These you will save as a text file with the .txt extension. Second, you will need a copy of the kk_to_py_translator.py program that converts your KwicKode .txt files into standard Python .py files. The translator program is listed in Appendix C of the KwicKode book, but with over 500 lines of code, you will want to contact me at authordbishop@gmail.com for a free digital copy of the program, which you can then copy into the Idle editor and save as a Python program.
The procedure, then, for running a KwicKode program is as follows.
- Write the program in the Idle text editor and save it with a .txt file extension.
- Load and run the kk_to_py_translator.py program, which asks you to give it the name of your KwicKode program.
- If there are no errors in the KwicKode program, you are told that the Python version of your program is ready to run.
- If the translator finds errors, it list all the errors with their line numbers. Edit your program and run the translator program again.
- When you are told that your Python program is ready to run, open it (it will have the same name as your KwicKode program, but with a .py extension. Then run it (Run–>Run Module). If there are still errors, you may be able to figure out what needs to be corrected by looking at the Python code. Then go back to the .txt KwicKode file, make corrections, and repeat the translating process. Be sure to close the old Python .py file before translating. If it is still open, when you go to run the Python file, it will be your old uncorrected program that will execute.
Order Kwickode Today:
KwicKode – Springboard to Python
Go to Tutorial Part A: The Basics
Go to Tutorial Part B: Loops and Functions
Go to Home Page