06.09.2021 Views

How to Think Like a Computer Scientist - Learning with Python, 2008a

How to Think Like a Computer Scientist - Learning with Python, 2008a

How to Think Like a Computer Scientist - Learning with Python, 2008a

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

66 Iteration<br />

1.0 0.0<br />

2.0 1.0<br />

4.0 2.0<br />

8.0 3.0<br />

16.0 4.0<br />

32.0 5.0<br />

64.0 6.0<br />

Because of the tab characters between the columns, the position of the second<br />

column does not depend on the number of digits in the first column.<br />

Logarithm tables may not be useful any more, but for computer scientists, knowing<br />

the powers of two is!<br />

As an exercise, modify this program so that it outputs the powers of<br />

two up <strong>to</strong> 65,536 (that’s 2 16 ).Printi<strong>to</strong>utandmemorizeit.<br />

The backslash character in ’\t’ indicates the beginning of an escape sequence.<br />

Escape sequences are used <strong>to</strong> represent invisible characters like tabs and newlines.<br />

The sequence \n represents a newline.<br />

An escape sequence can appear anywhere in a string; in the example, the tab<br />

escape sequence is the only thing in the string.<br />

<strong>How</strong> do you think you represent a backslash in a string?<br />

As an exercise, write a single string that<br />

produces<br />

this<br />

output.<br />

6.4 Two-dimensional tables<br />

A two-dimensional table is a table where you read the value at the intersection<br />

of a row and a column. A multiplication table is a good example. Let’s say you<br />

want <strong>to</strong> print a multiplication table for the values from 1 <strong>to</strong> 6.<br />

A good way <strong>to</strong> start is <strong>to</strong> write a loop that prints the multiples of 2, all on one<br />

line:<br />

i = 1<br />

while i

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!