Python: Lists (Arrays)

Python lists, or arrays, are fairly simple to create in Python.  Lists are defined as a basic data type in Python and therefore to create a list of items in python, we simply define the list using square brackets.  To create an empty list, we can simply use square brackets.

x = []

This is an array that has no items in it. We can begin adding items to the empty array by appending or extending the array. Let’s take a look at the difference between these two functions. The functions work as follows:

x = []
x.append(1)
x.extend(2)

By running the above code we get [1,2] in return. In the case of inserting a single value, the append and extend functions have no differences. However, when adding an array of elements, they work differently.

x = [1,2]
x.append([3,4])
# x = [1,2,[3,4]]
x.extend([5,6])
# x = [1,2,[3,4],5,6]

Notice in this example, that the append function adds the array as an array in the 3rd element. The extend function on the other hand adds the list of elements as an extension of the original list, x.

Another task that might be necessary is creating an array that defaults to all 0s. This can be done as follows, where SIZE is a variable that defines size. You simply multiply an array list of zeros by the desired size. You can also do this to create a multidimensional array as well. To create a 3-by-3 2D array of zeros. We create a list of item within the larger list multiplying the dimensions to the necessary sizes as shown in the second part below.

SIZE = 20
x = [0]*SIZE
# x = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
 
DIMX = 3
DIMY = 3
y = [[0] * DIMX] * DIMY
# y = [0, 0, 0], [0, 0, 0], [0, 0, 0]]

As in most languages, accessing the items of a list can be done by providing the key of the item that you would like to access. Remember, Python is a languages which begins with 0 as the first element of the list. Therefore, if we want to access the list within the list in the above example (or the third element) we want to use the key of 2.

x = [1,2,[3,4],5,6]
print(x[2])
# output >> [3,4]

One other function to discuss here is getting the length of an array list. This is relatively simple as the function len( ) provides a means of getting the size of the array. This function outputs the size of a list.

x = [1,2,[3,4],5,6]
size = len(x) 
print(size)

Creating a Multidimensional array can be done by creating lists for elements within the list. The above example creates a single element x[2] that contains a multidimensional aspect. If we wanted to access the value 4, we could do this using x[2][1].

One benefit of python arrays as opposed to other languages is that mixed values can be used; however, in my opinion, it is generally not good to mix the types unless absolutely necessary. By avoiding mixed arrays, you ensure that when processing the array you do not run into potential errors due to an unexpected data type.

For even more information about lists (as python lists provide even more tools for processing lists). Here are a list of a few good resources:

Python: Recursion

Recursion is one of the trickiest things to start understanding in programming.  Most people tend to think iteratively, however, a handful of people tend to think recursively.  Either way is perfectly fine, but when you start understanding and using recursion, you can start using the language to its full potential.  There are problems that are quickly and easily coded using recursion.

Writing recursive functions in Python are simple, just call the function that you’re writing within the function.  Just remember: you need a stop condition!  Below are some code examples of recursive functions in python. Try playing around with the examples to see how they work.

# Recursive factorial function
def factorial( n ):
    if n <= 1:
        return 1
    else:
        return n * factorial( n-1 )
 
# Recursive Fibonacci number for nth value
def fibonacci( n ):
    if n <= 1:
        return n
    else:
        return fibonacci(n-1) + fibonacci(n-2)
 
# Euclidean Algorithm for calculating the
# Greatest Common Divisor
def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a % b)

As shown in class, the easiest way to start understanding Recursion is to build out trees showing the parameters of the function and the value that should be returned.

Python: Finishing Functions

Writing your own functions

In a previous writeup, “Python: Beginning Functions (Modules)“,  I discussed the basics to starting to write functions in python.  In these examples, nothing was returned from the functions in Python.  In this section, we finish up python functions, making them much more useful in code than not being able to return any values.

In class, we discussed the concept of functions and you may have noticed then that a module (previously discussed) is nothing more than a special case of a function.  This is extremely true when we start discussing various programming languages as well.  In python, we use the same way of defining these functions as previously discussed.  In this entry however, we are concerned with the return operator.  The below code is a function for calculating the n-th value of the fibonacci sequence (iteratively).

def fibonacci( n ):
    u = 0
    v = 1
    for i in range(1, n):
        t = u + v
        u = v
        v = t
 
    return v
 
# Main Code
fib_n = fibonacci(10)
print( fib_n )

You may notice now that, unlike the previous discussion of functions, a variable has been written next to the return operator.  Similar to the parameters that are passed into a function, this variable represents a value that is passed back from the function.  Therefore, when we call this function, we can expect some value to be passed back from this function.

In the example above, the 10th fibonacci number should be 55.  Therefore if we trace the program and show v in the function, we can see this value is changing inside of the loop. Since we are returning the variable v, the last value of v that we get is passed back from the function. To use this we must store that value into a variable, which is done as shown in the “# Main code” section. The function simply returns the value from memory and stores that value in the memory allocated for the variable fib_n.

One thing to note about the values that we are passing back: the return operator can only return a single value. This is true to the extent that python includes more abstract data types (lists, tuples, and dictionaries) as a value that can be passed back. Therefore, if you want to return multiple values in an array fashion, this can be done easily in Python. However, in other programming languages, that is not true.

Using Python’s built in functions

Python has many build in functions, some of which we have been discussing already, such as the range( ) or print( ) functions. You can take a look at the list of built in functions if you would like. This may help you when you start writing code and notice that you might need something that has already been built. One function that will be used frequently in this class is the Random Number function. There are several ways this can be completed. Information is in the Python Standard Documentation on the Random number object.  We will learn more about objects in a later class.  For now, you should be able to call this function for random numbers.

You may also run across problems where you need something such as calculating factorials or the Square Root of a number.  If you understand the mathematics behind these calculations, you could simply write your own functions to do this, however, python has a lot of libraries that are included in the language that support a lot of different necessities.  For Example, the math functions can be found in the math library.

The Python documentation might help in building more complex applications if you so choose.  For example, Section 8 is the mathematics library, Section 12 is Data Compression, and Section 20 is Graphical User Interfaces with Tk.  You can also find various non-standard libraries throughout the Internet, then you can simply import those libraries, depending on the library’s documentation, and you’re ready to start coding.  Code reuse is the biggest benefit of writing code in functions or objects (discussed later)!

Python: Repetition Structures

For Loop

The for loop is an iterative loops that is intended to count through a series of numbers. This can be extremely useful when dealing with arrays (which are discussed in a later class). These also make up the basis of many mathematical formulae. For example, summing over a set of values might be as simple as writing a for loop.

The for loop can be achieved using the for keyword. this is followed by some iterative count. In python, we set aside some variable and iterate through a list of items or a range of numbers. The below code uses the range function. This is another block level element in python, and therefore anything which is a part of the loop must be tabbed over once. The loop below simply iterates through 10 numbers and prints each number out.

for i in range(10):
    print(i)

The range function can be used to change the starting and increment of the loop. range( ) can take in 1 – 3 arguments. If only one argument is included, it will count from 0 to n – 1 (where n is the argument passed). In the example above, you should get 0 – 9. If 2 arguments are passed, the first value is the starting value and the second represents the maximum value (again, if 10 is put in the second value, it will result in 9 being the maximum value). The third argument represents the incrementer. This means that instead of counting by one, the incrementer will be used to add to the values. The first loop below shows counting from 1 to 10, while the second shows how to display all values that are multiples of 3.

#Displays the values 1 through 10
for i in range(1, 11):
    print(i)
 
# Display multiples of 3 between 0 and 12
for i in range(0, 13, 3):
    print(i)

We will discuss the for loop again when we discuss arrays in Python.

While Loop

While loops, on the other hand, are conditional loops. Rather than iterating through a set of numbers, we can continue the loop until some condition is met. These can be used heavily in heuristic mathematics where we need to solve the threshold of mathematical formulae (yea…). Or when we’re testing a users input. For example, if we’re waiting for them to input the word “hello”, we could write a loop as shown below.

word = input("Type \"hello\"\n")
while word != "hello":
    word = input("YOU NEED TO TYPE \"hello\" EXACTLY!\n")
 
print("Good job!!!")

You may notice that the same conditionals that were used with if…else statements show back up here. This is what is meant by this being a conditional loop. Now, can we replicate a for loop using a while loop? Absolutely! The code below shows how to count from 0 – 9 as shown in the first example above.

i = 0
while i < 10:
    print(i)
    i += 1  # Equivalent to writing  i = i + 1

The two code examples do the exact same thing, but you may notice that the for loop for the iterative approach is much simpler and shorter to write. Ultimately, deciding which loop to use is up to the programmer. Although you should remember readability when writing the code as well.

break and continue operators

The break and continue operators are related to loops heavily, and were borrowed from the C language. The break command can be used to break out of the loop early. The continue command, breaks out of a single iteration of the loop and continues the loop at the next iteration.

The below code shows what the break loop does. In this, we loop over 10 numbers, but we decide to prematurely end the loop when it reaches 5 (perhaps for debugging purposes).

for i in range(0, 10):
    if i == 5:
        break
    print(i)

Now lets see what happens when we use the continue instead. The below code should no stop at 5, but rather skip over 5 when it arrives, so the output should be: [0,1,2,3,4,6,7,8,9].

for i in range(0, 10):
    if i == 5:
        continue
    print(i)

Do…while and Do…until Loops

Python does not natively support the do…while or do…until loops that were discussed in class. Various other languages may support these types of loops, which is why they were discussed in class. As mentioned in class, loops are fairly interchangeable with some creative logic, therefore all of the assignments are possible with just the above two loops.

Try playing around with some nested loops in python now! Remember your tabbed blocking!

Examples from Class

Python: Decision Structures

Conditionals

When using decision structures, conditional expressions are a necessity. The conditional expressions are the same as most C based languages. The table below lists the conditional operations in python.

operator function
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
== Equal to
!= Not equal to

If…else structure

Perhaps the most well known structure in programming is the if…else chain structure. In python this is relatively simple, and follows our psuedocode well, except the change in the elseif keyword.

First we start with the if statement and give it some conditional statement followed by a colon. We then write what will happen if this condition is true. If we have more conditions to check for, we need to write an elseif statement. This keyword is elif, we follow this with a condition and colon. The last condition is the else condition (simply else:). An example of how this works, where a user is asked to type in a number and the number is checked to determine if it is even or odd.

number = float( input("Give me a whole number:") )
 
if number % 2 == 0:
    print( number, "is even." )
elif number % 2 == 1:
    print( number, "is odd." )
# This gets run if the number isn't whole
else:
    print( "What number is this?!" )

So what happens when we want to add multiple conditions to an if statement, say we were trying to print out a users letter grade. We would need to use the boolean operations and, or, and not in python. The table below lays out how these boolean operations are done in python:

Operation Result
a and b if a is true, then check b, if true, run statements in this block *
a or b if a is false, then check b, if either is true, then run code in this block *
not a if a is false, then switch to true otherwise switch to false. **
* The second condition is only checked if needed.
** NOT is a lower priority than non-boolean operations in python. if a condition is written not x == y, it will be interpreted as not(x == y)

Knowing this we can now print out the letter grades of a user by structuring the code to do something if a number is between a range of numbers.

grade = float( input("What is your grade?") )
 
if grade < 60:
    print("F")
elif grade >= 60 and grade < 70:
    print("D")
elif grade >= 70 and grade < 80:
    print("C")
elif grade >= 80 and grade < 90:
    print("B")
else:
    print("A")

Switch structure

Python does not natively support the switch structure. There are people who have come up with creative ways to using something which is similar to the switch structure. You can Google around if you are interested, but for the most part, the If…else structure can get you through all of the programming in the class here.

Examples from Class

Python: Beginning Functions (Modules)

Modules in Python

In many languages, including python, modules (as defined in the Gaddis book) are simply a specific type of function that return no values (or rather a NONE value in python). This means that what was learned so far regarding modules will look very similar when we get to functions in python. In fact, the definition given by the book isn’t frequently used by many languages.

In python, a module is a set of defined functions within a single file that can be imported for later use with various code. We will discuss this more later in this entry. Importing functions becomes extremely useful when needing to reuse code later.

Defining Functions

The definitions of modules is relatively simple in python. In python, we use the keyword def in order to state that we are defining a new function for use. We then give our function a unique name following the same requirements as those for variable names (Alphanumeric and underscores only). At the end of this line, a semicolon allows us to start defining what the function does.

If we take our “Hello World” program and define this within a function in python, we would be left with something such as:

def hello():
    _hello = "Hello World"
    print(_hello)
    return
 
# Main Code Here, FUNCTION CALL
hello()

Notice the first line is exactly structured as described in the paragraph above. You will also notice that everything underneath of this code is indented by one tab (4 spaces). This is important in python, as tabbing is a part of the languages lexical structure. Anything that will be run within this function needs to be tabbed by one, or it will be considered a part of the Main code. Notice that the function call is not tabbed over. When this is run, the code looks for the function called hello( ) and runs this code.

You may also notice the return at the very end of the function definition. Since we aren’t returning anything, we do not need this line in the code (at least in 3.2), but it is good practice in python to include this line so that the code is more easily read, as we can simply look to see if that function is returning any values. We will discuss this return keyword when we discuss functions a little more in a later class.

Passing Arguments

Python only has a single way of passing arguments into functions, passing by value. Therefore, you may have to rewrite your psuedocode to work if you decided to pass by reference. Passing parameters in python is relatively simple, simply write some variable name in the function argument list.

def hello( name ):
    print("Hello, " + name)
    return
 
# Main Code Here
hello("Kris")

Notice that I simple passed a string without referring to a variable in the Main code. We can also rewrite this to use variables as well, as a variable simply stores a value. Also note that any number of variables can be passed into the function.

def hello( name ):
    print("Hello, " + name)
    return
 
# Main Code Here
user_name = input( "What is your name?" )
hello( user_name )

Since python is loosely typed, we do not need to tell python what type we are passing in. I also mentioned in class that some languages support overriding of functions (writing multiple functions with the same name and different argument lists). Python does not support this, writing two functions named hello in the same code causes complete overriding of the function. We cannot call the hello( ) function if hello( name ) was declared after the declaration of hello( ).

Python Module Imports

So, what is a module in python? A module is a separate file containing python function definitions and statements. If you want to clean up the file that you are working with, you can take all of your function definitions and put them into a completely independent python ( .py ) file and import this file into the main code. To import the file, you simply use the import command with the file name (excluding the .py). An example is below.

# hello_mod.py
def hello( ):
    print("Hello World")
    return
 
def hello( name ):
    print("Hello, " + name)
    return

The above file is saved as hello_mod.py. We can now import this file in our code:

import hello_mod
 
# prints out Hello World
hello( )
 
# prints Hello, Kris
hello( "Kris" )

Note that the files should be in the same directory, or you want to use a relative path to import the file.

Examples from Class

Ch. 3, Ex. 1 — Kilometers to Miles

def showMiles( kilometers ):
    miles = kilometers * 0.6214
    print("The conversion of ", kilometers, " to miles is ", miles, " miles")
    return
 
# Main Code
km = float( input("How many kilometers were travelled?") )
showMiles( km )

Ch. 3, Ex. 8 — Seat Income Problem

def showIncome( a, b, c ):
    incomeA = a * 15
    incomeB = b * 12
    incomeC = c * 9
    print( "Class A seats made ", incomeA, " dollars" )
    print( "Class B seats made ", incomeB, " dollars" )
    print( "Class C seats made ", incomeC, " dollars" )
    income = incomeA + incomeB + incomeC
    print( "Total income is ", income, " dollars" )
 
# Main Code
aCount = int( input("How many class A seats were sold? ") )
bCount = int( input("How many class B seats were sold? ") )
cCount = int( input("How many class C seats were sold? ") )
showIncome( aCount, bCount, cCount )

Python: Input, Output, and Simple Mathematics

Program Output

In the previous post, you copied and pasted a simple hello world program into python notation. This print function displays how simple it is for python to display text to the terminal. It is good to note then that the print function — print( ) — can be used to display any type of output. One benefit of python is that newline characters are automagically inserted at the end of the print function. There are however a few escape characters that you should be familiar with for outputting information in python. You should become familiar with the table below as they will help in proper display of data when writing your programs.

Escape Sequence Meaning
\\ Backslash
\’ Single Quote ( ‘ )
\” Double Quote ( ” )
\n ASCII Linefeed ( newline )
\t ASCII Horizontal Tab ( tab )

These are only a handful of escape characters. You can check the documentation for all of the escape sequences, but these will be the most important ones to know.

Variables

Python is a loosely typed language, which means we do not need to declare the type that we are working with in the code (I do expect it in the psuedocode however). There are several rules you should follow when creating variables (this will also apply to functions/modules later on), which we discussed in class. As a review, to name variable you must:

  1. Start with a letter (UPPER- or lower- cased) OR an underscore ( _ )
  2. This can be followed with letters (UPPER- or lower- cased), any number, OR an underscore

Knowing this, lets rewrite our hello world program to use a variable and print that variable out:

_hello = "Hello World!"
print(_hello)

The only difference now is that we store the string in the variable _hello. We can print a variable just like any other string. NOTE: Strings are always in double quotes, which is why the double quote escape character exists. If we needed to add a quote, say surrounding the word World, we could write the code as such:

_hello = "Hello \"World!\""
print(_hello)
Which tells python the double quotes are part of the string, not a character that breaks the string. Don’t worry, many people will make that mistake when you first start programming. Any escape character can be used inside of the strings.

User Input

User input is also relatively simple to comprehend as only a single function is needed to grab user input.  The function required for grabbing user data is the input function — input( ).  If we want to ask the user’s name, we can store the name in a variable, created by simply naming the variable. The input function can take a string inside of the parenthesis which will be printed for the user to see. For example, the following python code will ask the user their name and tell the user Hello. (This code is for v3.2, for v2.7 use the function raw_input( ) instead.)

name = input("What is your name?")
print("Hello, " + name)

You should notice that the use of the Input function in python is equivalent to two lines of the pseudocode that we are using. Our pseudocode first uses the display, and then grabs user input. The above python code is equivalent to the below psuedocode (that we are using).

DISPLAY "What is your name?"
INPUT name
DISPLAY "Hello, ", name

In many languages, the pseudocode process is what needs to be done. Python is an exception here. Be aware of this as you work in other languages like C++ or Java.

Simple Mathematics

Mathematics in python is relatively simple. By default, Python treats certain arithmetic as floating point numbers (such as traditional division). The table below gives a brief overview of the basic arithmetic functions in python.

Sytax Math Operation Name
a + b a + b Addition
a – b a – b Subtraction
a * b a * b Multiplication
a / b a / b Floating Point Division
a // b a / b Integer Division
a % b a mod b Modulo
-a -a Negation
a ** b ab Exponent

Standard order of operations apply to all mathematics in Python.

One little caveat with getting numbers as input from the user.  Python, as do most languages, grabs a string from the user.  Therefore you need to parse the string into an integer or floating point number.

# Parses the input as an integer type
x = int( input("Type in a number:") ) 
# Parses the input as a floating point number
y = float( input("Type in a floating point number:") )

In order to display integer or floating points together through string concatenation, we must first convert the number to a string using the str( ) function. This will allow us to concatenate strings together. If we wanted to change the above code to print the numbers typed in we could do the following:

# Parses the input as an integer type
x = int( input("Type in a number:") ) 
# Parses the input as a floating point number
y = float( input("Type in a floating point number:") )
# Print the numbers typed in out
print( "You typed " + str( x ) + " and " + str( y ) )

Notice in both sets of code, the use of the # sign. this # symbol allows us to comment our code. Comments should be used to make your code more readable. All text after the # symbol will be ignored by the python interpreter.

Examples from Class

Ch 2, Ex 7 — Miles-per-Gallon

miles = int( input("How many miles have you driven?") )
gallons = int( input("How many gallons of gas were consumed?") )
mpg = miles / gallons
print("Your car gets " + str( mpg ) + " miles per gallon")

Ch 2, Ex 5 — Distance Traveled

_speed = 60
time_t = 5
distance = _speed * time_t
print("You traveled " + str(distance) + " after " + str(time_t) + " hours")
time_t = 8
distance = _speed * time_t
print("You traveled " + str(distance) + " after " + str(time_t) + " hours")
time_t = 12
distance = _speed * time_t
print("You traveled " + str(distance) + " after " + str(time_t) + " hours")

Introduction to Python

Why use Python?

Here at ECTC, we offer three options for the CIT students.  Python is a language that benefits each of the IT students.  For the networking students, Python provides a tools that allows for automation of system shell functions.  Much of what networking students do revolves around the setting up of computer networks and learning to maintain these networks effectively.  Python can help!  Many of the tasks in management of the network are tedious and often menial.  If the option of knowing a language that can automate these tasks, why not learn it to help minimize the total work spent on these easy tasks?

And what about web development? Web development is one of python’s strong points. You can only do so much with HTML / CSS and JavaScript. What if you want a data driven website, something like Amazon or eBay or Facebook? Python can help you create these dynamic web pages by connecting to a database where information about users or items are stored.

Python is also a good tool for the programming option. In all three OSes, it can be used as a stand alone programming language that can create complex GUI applications. Overall, Python is a good choice simply because it can be used for all three options, and it is relatively simple to learn in comparison to many other languages.

Installing Python

If you use Linux, Unix, or Mac OS X (a derivation of Unix), you likely have python installed already, just open up your terminal and type “python”. This will open up python for you. You should check the version to determine if you are using the most recent version (as of this writing, 3.2).

Most people use Windows however, which means you will need to install python on your computer. You can download python on their website: http://www.python.org/.  Just go to the download page and download the latest version’s MSI installer for your version of windows (x86 or x86-64) and just run the install wizard.  You can read the documentation online if you want to change various configuration setting on python, however for this class, the standard python installation should be more than enough.

This version comes with a python command line application, which runs from within command prompt in windows. It also comes with IDLE, which allows you to run python from within a window of the OS. Some people may prefer using the command prompt instead of opening up an entirely different application (The instructions below are for use in command prompt). For this we can change the Environment variables. Go to Control Panel > System and Settings > System > Advanced System Settings (off to the left). This will bring a display window which has a button called “Environment Variables”. Just find the Path variable and at the end add “C:\Python32″ (or the path to the python application location). REMEMBER, path variables are separated by semicolons!

Running Python Scripts

Let’s write a simple python script to test our install and learn how to run the script. You can write python scripts in your favorite text editor (Notepad will work just fine, though I would recommend something like Crimson/Emerald Editor in Windows since it adds syntax highlighting). For now, just copy and paste this program:

print("Hello World!")

Be careful that the program does not add any tabs. We’ll learn that tabbing in python is extremely important and can cause code to fail if tabbing is not done in the code. Save this file in some location saving it with the .py extension.

To run this code, open up your terminal or command prompt and change to the directory that the file has been saved. To run the file, type “python filename.py” where filename.py is the name of the file that you saved. This should give you the expected “Hello World!” output.

More on running python

If you want to know more about running scripts in python, there are several flags that can be used. You can check out the man pages for python to learn more. NOTE: We will see the -c flag used when we start discussing modules. You can check the manual pages for python at: http://www.linuxmanpages.com/man1/python.1.php.

Complex Data Type

Another primitive data type that is implemented in a handful of languages is the complex data type which can be used to represent complex numbers in mathematics.  Complex numbers are numbers in mathematics which contain a real and imaginary part.  If you recall the imaginary part occurs on any square root of a negative number.  Complex (or imaginary) numbers take on the form a+bi where i represents \sqrt{-1}.

In languages that implement this, the complex number is often translated by the compiler as a series of two individual floating point numbers representing a and b.  These languages which implement complex numbers often implement the basic operators (addition, subtraction, multiplication, and division) needed to do mathematics with the complex numbers.

This is good to know about in the rare case that you may need the data type, but we will not be using this data type in this class.

Character Data Type

Character Data Types are the most primitive data type used for writing.  Multiple characters are combined to make up words, sentences, paragraphs, and everything in any language.  Characters represent any symbol which may be important to someone writing, whether they are mathematical symbols, punctuation, letters, numbers, or even the hidden characters that you don’t think of, such as the newline or tab characters.

Characters are always stored in some form of encoding.  This simply means that the symbols that are displayed on the screen are represented using an integer value and then mapped to the defined encoding of the document or program.  The simplest form of encoding used on the computer is ASCII (American Standard Code for Information Interchange).  This standard uses 8 bits [technically 7 with a parity bit is desired] to store character data.  Therefore, the maximum number of characters that can be represented using this information was 2^{7} or 128 characters, enough to represent any symbol necessary in the English Language.

This remained the most popular encoding used on the web until December 2007 when UTF-8 surpassed ASCII.  UTF-8 is now the most popular character encoding used on computers.  UTF (UCS [Universal Character Set] Transformation Format) was designed to be backwards compatible with ASCII by keeping the original ASCII characters as the first 128 of the Unicode character set.  UTF in its current form (UTF-32) uses exactly 32 bits to represent characters, allowing it the ability to encode 4,294,967,296 characters.  This encoding is enough to represent characters in most languages and mathematics.

Because UTF encoding is so big, for this class when we do anything with characters we will assume ASCII.  An ASCII table can be found at <http://www.asciitable.com/>.

Because characters are encoded by storing integers, basic arithmetic can be done using character data types in most programming languages.  Therefore, if we have the character “A” and the character “?”, we can do addition of the characters by looking at the ASCII table:

 [A] + [?] = 128

In this case, if we attempted to store this as a different character in ASCII, we would get an overflow error.  If however we store it as an integer, we would receive 128 as the number.  It is rare that you will do this when programming, but because of the way it is encoded, you should be aware in case you move between languages that have varying support for this and receive an answer you were not expecting.

So how do we store whole words?

In strings, but strings are an abstract data type which we’ll discuss later in class.  In languages such as C, you create strings simply as arrays of characters.  This will make more sense as we get further into the book.