Comment Addendum for Intro to Coding: Part 2 - String Variables

  • These are my comments and addendums for Intro to Coding series: String Variables.
  • This is Part 2 of my comments and addendums. My plan is to create such comments for each lesson as I go along my learning journey. See Part 1 and Part 7.
--[[ 
What is a string exactly?  In programming, a string is a "basic" or 
"primitive data type.  Per Wikipedia, "a data type (or type) is an attribute
that tells the interpreter or compiler how the programmer intends to use the 
data." Strings of data are similar to actual, physical strings of thread or 
yarn.  Strings are simply long lengths of characters.  In fact, in many 
programming languages, a single character (such as a, B, c, or 1, 2, or #) is
a data type itself. In Lua, however, there is no primitive "character" data type
- just string.  Therefore, in Lua, a single character is stored as a string data
type comprised of only a single character. 

Why does this matter?  When a variable is declared, physical system memory is 
allocated for the storage of the type of data that will be stored when the 
interpreter (or compiler) runs.  Data types are defined language by language, so
their memory values may differ.  For example, in C programming, when declaring a 
variable that will store a character data type and the compiler will allocate 
1-byte (8 bits) of memory for storage.

-- ### BEGIN EXAMPLE

C Programming syntax:
	char my_character = 'A'
	printf("The character returned is %c.", my_character)
OUTPUT
	The character returned is A

If you attempt to store additional characters in the variable, only the last
character will be returned when the variable is called:

C Programming Syntax:
    char my_character = 'ABC'
    printf("The character returned is %c.", my_character)
OUTPUT:
    The character returned is C.

-- ### END EXAMPLE

Why does THIS matter?  Well, a string is actually an array, or "ordered list"
of characters.  If I have a string comprised of the six (6) characters 'ABCDEF',
the compiler will need to allocate 6 * 8-bytes = 48 bytes of memory to store 
the string.  Although arrays have not been covered yet, think of the
relationship between strings and arrays like this:

STRING: A   B   C   D   E   F
ARRAY: [1] [2] [3] [4] [5] [6]

In context to the string 'ABCDEF', if I want the third letter of the string, I
can reference the third element [3] of the array.  That's it.  Again, arrays
are covered later.

In Lua, there are actually only a few data types compared to other 
programming languages.  Lua has nil, boolean, number, string,
 function, userdata, thread and table.  That's it.  In contrast, Python
has str (string), int (integer), float (decimal), complex, range, tuple, list, 
dict, set, frozenset, bool (Boolean), bytes, bytearray, and memoryview.  
This reduced set makes things easier in terms of programming but 
potentially more inefficient in terms of resource management. 
For example, if I only want to store a single character in my program,
the most efficient thing to do would be only to set aside 8 bytes of memory. 
If I were to store a single character in a string data type, additional memory
is allocated to set up the array and wasted; however, computing resources, 
such as memory (RAM) is cheap today. A long time ago resources weren't
cheap, so programmers had to scrap and save for every byte of memory
they could.  Today, many newer programming languages don't bother with
the character data type because of the ease-of-use advantage that "non-strict" 
data types and memory management improvements provide.

Functions have not yet been covered.  For the time being, understand that
Lua has several built-in "functions" that can be used to perform basic 
functionality in Lua.  The print() function is one such built-in function.  
Built-in means that the print() function is part of the Lua installation.  
Functions created by you are not built-in. Lua has one such built-in function 
called type().  According to the Lua Reference Guide, type() is a library 
function that returns a string describing the type of a given value.
--]]

--### BEGIN EXAMPLE:
--### NOTE: The type() function is "nested" in a print() function in order
--### to provide output in the Output window for the sake of the example.

print(type("This is a string"))
-- # OUTPUT:  string

print(type(7))
-- # OUTPUT:  number

print(type(type(7)))
-- # OUTPUT:  string

--[[ 
GOTCHA! Remember, the type() function returns STRING describing the
type. This statement nested back-to-back type() functions; the first type 
returned the string value "number".  The second type() function was passed
the string value"number" from the nested type(7) function so the string "number" 
processed as a string!
--]]

-- ### END EXAMPLE

print("Hello, world!")

-- The print() function simply returns a string (there it is again) output.  

-- ### STRING CONCATENATION
--[[
The word "concatenate" means "to link together; unite in a series or chain."
A sentence in English is simply the concatenation of multiple words and 
spaces. In the example above, "Hello, world!" is the concatenation of THREE
separate strings:

1.)  Hello,
2.)  " "(a space; minus the double quotation marks)
3.)  world!

Let's visualize this.  Another built-in Lua function is the len() function of a 
stringobject (or the # operator, covered later).  The len() function works 
on string data to return the string's length.
--]]

--### BEGIN EXAMPLE:

print("Hello, world!")
print(string.len("Hello, world!"))

-- # OUTPUT:
-- Hello, world!
-- 13

-- VISUALIZATION:
--  H   e   l   l   o   ,       w   o   r    l    d    !
-- [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13]

-- ### END EXAMPLE

Could be simplfied a lot. A lot of repetive additional info that isnt needed.

For a beginner it is better to learn the core concept then develop on it as you go along e.g explaining that it holds information of different types and building their knowlage onwards from that.

Theres no use explaining bits used and datatypes to someone until they come across it later on

The best way of learning what a variable is to do maths as opposed to string

Thanks for the feedback. The only response I have is that this is in direct response to the training tutorials from the Education hub. Their tutorial introduced the string type so I was expanding on that topic.

I like this, this seems very practical although it may be simplified even further.