## Lab Exercise 2

### Introduction

Almost every week, we will do three things before starting to work on the lab exercise. You did them last week, too.

First, create a directory on your USB device for this lab, say, lab02 as a sub-directory inside your cs1510 directory.

Next, launch IDLE, the program that provides us with a Python shell and a Python-savvy program editor. You can find IDLE by selecting Start | All Programs | Python 3.4 | IDLE (Python GUI). This will start you off with a Python shell window. At the end of the session, you will save your shell window as a transcript of your interactions throughout the lab.

Finally, open a new text file called responses.txt using whatever editor you like. You will use it to record answers and questions as you work through the lab.

### Representing Characters

You have learned that computers ultimately use the binary digits 0 and 1 to represent all data. So, every character can be given a number as a code. This includes letters such as a and Z, punctuation marks such as # and @, and even characters like the space and "new line". The first standardized set of characters was ASCII. It is the basis today for a more extensive coding, Unicode.

In Python, you can find out the decimal equivalent of any character using the ord() function:

```
>>> ord('a')
97
```

To find out which character corresponds to a particular decimal number, use the chr() function:

```
>>> chr(66)
'B'
```

Now, use these functions in your Python shell to answer these questions:

1. What is the ASCII code for 'F'?
2. What is the ASCII code for ' ' (the space)?
3. What is the ASCII code for '\n' (the new line character)?
4. What is the character has an ASCII value of 73?
5. What is the character has an ASCII value of 117?
6. How far apart are 'e' and 'w'?
7. How far apart are '2' and '4'?
8. Create a 'q' from an 'e'.

You may evaluate as many Python expressions as you would like to answer each question. However, you may use only these Python operators and functions:

```    +         addition
-         subtraction
ord()     ASCII value for a character
chr()     character for an ASCII value
```

If think you want or need to use something else, ask me first.

### Representing Numbers

You have learned that computer scientists often use binary numbers to represent numbers, because this mimics how computers are built out of transistors. Binary numbers use 2 as the base for counting.

With only two digits, 0 and 1, binary numbers grow very long very quickly. For convenience, computer scientists sometimes use hexadecimal numbers as a shorthand. Hexadecimal numbers use 16 as the base for counting. 16 = 24, so "hex" has all of the advantages of base 2 and plus the extra advantage of shorter numbers. (Even shorter than decimal!)

If you would like a refresher, take a look at one or more of these short tuturials:

Now, evaluate a series of expressions in your Python shell to answer each of these questions:

1. What is the decimal value of 101 (binary)?
2. What is the decimal value of 1001001 (binary)?
3. What is the decimal value of 40 (base 16)?
4. What is the decimal value of FE (base 16)?
5. What is the binary representation for 29 (decimal)?
6. What is the hexadecimal representation for 73 (decimal)?

You may evaluate as many Python expressions as you would like to answer each question. However, you may use only these Python operators:

```    //    integer division
%     integer remainder
*     multiplication
**    exponentiation
```

If think you want or need to use something else, ask me first.

### A Short Program

Time permitting, let's solve a real problem with a little Python code.

A friend is planning a trip to Europe. Her bank will exchange US dollars for euros for a \$10 service fee. It converts the rest of the dollars to euros at the current exchange rate. As of this morning, the exchange rate is 1 euro for 1.31 US dollars.

For example, if your friend wants to exchange \$100, the bank will keep \$10 and give your friend \$90 / 1.31 = 68.70 euros.

Open a new program window by selecting File | New File. In it, write a short program that:

• takes in an amount of US dollars,
• calculates the equivalent value in euros given in exchange by the bank, and
• prints a message giving the amount of euros.

Feel free to use seconds_left.py from class as a template. You can also use the shell to test out expressions before copying them to your program window.

Try your program out on the example above, plus a couple others of your choosing.

### Finishing Up

Make sure that your responses file and program file are complete and saved. Save your shell window to a file called transcript.py.

• Select CS 1510, Intro to Computing, Wallingford from the drop-down menu and press Continue.

• Select lab02 -- Representing and Manipulating Data from the radio-list menu and press Continue.

• Press Choose File for each of the files to submit. Navigate to the files on your USB drive and select them. When both are selected, press Continue.

• Verify that your files were submitted. You should see something like this:
```    Uploading the following files.

transcript.py of size 1467 bytes
responses.txt of size 716 bytes
currency_converter.py of size 416 bytes
The files listed above were uploaded.
You are responsible to make sure that this list is complete.
You may replace or add file up until the deadline by using this system again...
```

If you need any help, let me know.

Eugene Wallingford ..... wallingf@cs.uni.edu ..... September 3, 2014