7. Simple Tables¶
One of the things loops are good for is generating tabular data. Before computers were readily available, people had to calculate logarithms, sines and cosines, and other mathematical functions by hand. To make that easier, mathematics books contained long tables listing the values of these functions. Creating the tables was slow and boring, and they tended to be full of errors.
When computers came of age, one of the initial reactions was, “This is great! We can use the computers to generate the math tables, so there will be no errors.” That turned out to be true, but also shortsighted. Why? As soon as it became cheap enough to create small computers (aka calculators), tables became mostly obsolete. Why carry large books of tables around when a relatively small machine could carry out the same calculations on demand?
For some operations, however, computers can use tables of values to get an approximate answer and then perform computations to improve the approximation. In some cases, there have been errors in the underlying tables, most famously in the table the Intel Pentium processor chip used to perform floating-point division.
Although a power of 2 table is not as useful as it once was, it still makes a good example of iteration. The following program outputs a sequence of values in the left column and 2 raised to the power of that value in the right column:
The string '\t'
represents a tab character. The backslash character in '\t'
indicates the beginning of an escape sequence. Escape sequences are used to represent invisible characters like tabs and newlines. The sequence \n
represents a newline character.
An escape sequence can appear anywhere in a string. In this example, the tab escape sequence is the only thing in the string. How do you think you represent a backslash in a string?
As characters and strings are displayed on the screen, an invisible marker called the cursor keeps track of where the next character will go. After a print
function is executed, the cursor normally goes to the beginning of the next line.
The tab character shifts the cursor to the right until it reaches one of the tab stops. Tabs are useful for making columns of text line up, as in the output of the previous program. Because of the tab characters between the columns, the position of the second column does not depend on the number of digits in the first column.
Check your understanding
-
What is the difference between a tab (
- A tab will line up items in a second column, regardless of how many characters were in the first column, while spaces will not.
- Assuming the size of the first column is less than the size of the tab width.
- There is no difference
- Tabs and spaces will sometimes make output appear visually different.
- A tab is wider than a sequence of spaces
- A tab has a pre-defined width that is equal to a given number of spaces.
- You must use tabs for creating tables. You cannot use spaces.
- You may use spaces to create tables. The columns might look jagged, or they might not, depending on the width of the items in each column.
'\t'
) and a sequence of spaces?