# Review Questions

## Review Questions

#### Determine the Data Type

Q03.01 Find the data type of `a`

if `a=9`

Q03.02 Find the data type of `a`

if `a=9.`

Q03.03 Find the data type of `a`

if `a='9.'`

Q03.04 Find the data type of `a`

if `a=(9)`

Q03.05 Find the data type of `a`

if `a=False`

Q03.06 Find the data type of `a`

if `a=[1,2,3]`

Q03.07 Find the data type of `a`

if `a=(1,2,3)`

Q03.08 Find the data type of `a`

if `a={'key'=9}`

Q03.08 Find the data type of `a`

if `a=1 + 9j`

#### Numeric Data Types

Q03.10 Set `a=1`

and `b=2`

. What data type is `a/b`

?

Q03.11 Set `a=1`

and `b=2`

. What data type is `a*b`

?

Q03.12 What is `5.1`

plus `0 + 3j`

?

Q03.13 What floating point number converts to the boolean `False?`

Show this in code and the `bool()`

function.

Q03.14 Create the floating point number 0.001 \times 10^{-0.2} and assign it to the variable `b`

.

Q03.15 Show that `3e2`

is the same as `3E2`

with the comparison operator `==`

#### Booleans

Q03.20 Predict the output if the lines `n=5`

and `(n<3) and (n<7)`

are run. Then run the the two lines of code.

Q03.21 Predict the output if the lines of code below are run. Then run the code.

```
>>> ans='Yes'
>>> ans=='Yes' or ans=='No'
```

Q03.22 Pick a number `n`

to make the following statement `True`

: `(2<n) or (n==2+n)`

Then run the code to show your number works.

Q03.23 Pick a number `n`

to make the following statement `False`

: `not (n<6) and (n<4)`

Then run the code to show your number works.

Q03.24 Create the floating point number 0.001 \times 10^{-0.2} and assign it to the variable `b`

.

Q03.25 Show that `(n>5) and (n<=10)`

is equivalent to `5 < n <= 10`

using the two different numbers for `n`

.

Q03.26 Show that `(n<5) or (n>=10)`

is equivalent to `not(5 =< n < 10)`

using the two different numbers for `n`

.

#### Strings

Q03.30 Define a string that contains the word Problem.

Q03.31 Define one string as the word Problem and define another string as the word Solving. Combine these two strings to make the statement Problem \ Solving.

Q03.32 (a) Define a string that contains the number 8 and a string that contains the number 5. Combine these two strings with the plus operator `+`

.

(b) Define an integer as the number 8 and an integer as the number 5 and combine these two integers with the plus operator `+`

(c) Explain why the output from (a) was different from the output of (b)

(d) Multiply the strings 8 and 5 with the multiplication operator `*`

. Compare the output to multiplying the integers 8 and 5. Why is the out different?

Q03.33 Complete the following index and slicing operations after the string `word = 'Problem'`

is defined.

(a) Pull out the letter P from `word`

(b) Pull out the first three letters Pro from `word`

(c) Pull out the second through the fourth letters rob from `word`

(d) Pull out every other letter from `word`

starting with P

(e) Use indexing and slicing to ouput `word`

backwards to produce melborP.

Q03.34 Define the strings below:

(a) Define a string `a`

as *coffee*, define a string `b`

as *it's* and define a string `c`

as *hot!* and string `d`

as , (a comma).

(b) Combine the strings `a`

, `b`

and `c`

and `d`

to produce the string *coffee, it's hot* (notice the comma)

(c) Print out the statement *she said "coffee, it's hot"*

Q03.35 Create the string `path`

with the value *C:\Users\Gabby\Documents*

Q03.36 Convert the string `Problem`

to the list `['P','r','o','b','l','e','m']`

without rewriting the list from scratch.

Q03.37 Use the string `over board`

and slicing to get the words:

(a) `over`

(b) `board`

(c) `oar`

Q03.38 Use the string `rotten tomatoes`

and slicing to get the words:

(a) `to`

(b) `no`

(c) `ten`

(d) `oat`

#### Lists

Q03.40 Create a list that contains the numbers 1, 2.9 \times 10^8, and the word game.

Q03.41 Create a list that contains the words problem, solving, with, python.

Q03.42 Create a list with one value, the number 6. Convert the list to a boolean with the `bool()`

function.

Q03.43 Create an empty list. Convert the empty list to a boolean with the `bool()`

function.

Q03.44 Create a list with the letters C, D, and R. Pull the letters C and D out of your list with indexing.

Q03.45 Create a list with the numbers 1 to 10 (counting by ones). Use slicing to pull out the number 5 from the list.

Q03.46 Create a list with the numbers 1 to 10 (counting by ones). Use list slicing to pull out all of the numbers 5 or less.

Q03.47 Create a list with the numbers 1 to 10 (counting by ones). Use list slicing to pull out all of the numbers 5 and greater.

Q03.48 Create a list with the numbers 1 to 10 (counting by ones). Use slicing to pull out all of the even numbers from the list.

Q03.49 Create a list with the numbers 1 to 10 (counting by ones). Use slicing to pull out every odd number from the list.

Q03.50 Create a list with the numbers 1 to 10 (counting by ones). Use slicing to return the list in reverse order (the returned list starts with 10 and ends with 1),

#### Dictionaries

Q03.60 Create a dictionary called `capitals`

that contains the states and state capitals. Include `Washington`

, capital `Olympia`

and `Oregon`

, capital `Salem`

.

Q03.61 Create a dictionary called `capitals`

that contains the states and state capitals. Include `Washington`

, capital `Olympia`

and `Oregon`

, capital `Salem`

. In the line after the dictionary is created add the state `New York`

, capital `Albany`

.

Q03.62 Create a dictionary `numbers = {'one':1, 'two':2, 'three':3}`

. Pull out the number `'2'`

by calling the key `'two'`

.

Q03.63 Create a dictionary `colors = {'red':' #FF0000', 'green':'#008000', 'blue':'#0000FF'}`

. Pull out all the keys and add them to a list called `colors_list`

with the `.keys()`

method.

Q03.64 Create a dictionary `colors = {'red':' #FF0000', 'green':'#008000', 'blue':'#0000FF'}`

. Pull out all the values and add them to a list called `colors_hex`

with the `.values()`

method.

Q03.65 Create a dictionary `colors = {'red':' #FF0000', 'green':'#008000', 'blue':'#0000FF'}`

. Pull out all the items from the dictionary and add them to a list called `color_items`

with the `.items()`

method.

Q03.66 Create a dictionary `groups = {'solo':1, 'duo':'2'}`

. Add the key trio and the corresponding value 3.

Q03.67 Create a dictionary `groups = {'solo':1, 'duo':'2'}`

. Then remove the key `'duo'`

and the value `'2'`

so that only `'solo':1`

remains.

Q03.68 Create a dictionary `college = {'name': 'University of Oregon'}`

. Add the following two keys: abbreviation, mascot and the corresponding two values: UofO, ducks.

#### Tuples

Q03.70 Create a tuple with the numbers 8, 9, and 10.

Q03.71 Create a tuple that has a single entry, the number 10.

Q03.72 Create a list and a tuple that both contains the values: one, two and three. Pull the word two out of both the list and the tuple.

Q03.73 Create a list and a tuple that both contains the values: one, two and three. Try to substitute the number 2 for the word two in both the list and tuple using indexing (square brackets).

Q03.74 Code in the following lines:

```
t1 = (9)
t2 = (9,)
t3 = ('9')
```

Use Python's `type()`

function to find the object type of each variable.

Q03.75 Create a tuple that returns `True`

when converted to a boolean. Use the `bool()`

function to demonstrate your tuple converts to `True`

.

Q03.76 Create a tuple that returns `False`

when converted to a boolean. Use the `bool()`

function to demonstrate your tuple converts to `False`

.

#### Errors, Explanations, and Solutions

Q03.80 Run the following lines of code and explain the error in your own words, then write the lines of code to run without an error:

```
n = 503
n[2]
```

Q03.81 Run the following lines of code and explain the error in your own words, then write the lines of code to run without an error:

```
a = 321
b = 'go!'
c = a + b
```

Q03.82 Run the following lines of code and explain the error in your own words, then write the lines of code to run without an error:

```
d = {one:1, two:2, three:3}
d[one]
```

Q03.83 Run the following lines of code and explain the error in your own words, then write the lines of code to run without an error:

```
f = false
not f
```

Q03.84 Run the following lines of code and explain the error in your own words, then write the lines of code to run without an error:

```
comp = 0.1 - 4.3i
comp + 5
```

Q03.85 Run the following lines of code and explain the error in your own words, then write the lines of code to run without an error:

```
empty = ''
bool(empty)
```

Q03.86 Run the following lines of code and explain the error in your own words, then write the lines of code to run without an error:

```
lst = [1,3,5]
lst[3]
```

Q03.87 Run the following lines of code and explain the error in your own words, then write the lines of code to run without an error:

```
dict = ['key': 8, 'pair': 9]
dict['key']
```

Q03.88 Run the following lines of code and explain the error in your own words, then write the lines of code to run without an error:

```
s = ['Problem Solving']
s[8:]
```