Lecture 1, Part 1 Exercises

Before starting, please run the following cell

In [ ]:
from __future__ import division, print_function 

Question 1

Use code to determine the answers to the question 1.1-1.4

1.1

What is the type of 1? Use the function type to determine.

1.2

What about the type of 1.0?

1.3

What is the resulting type of adding an 1 and 1?

1.4

What is different about the resulting type of adding 1 and 1.0?

1.5

So, what can you say about the resulting type of adding an int and a float?

Question 2

Use code to determine the answers to the question 2.1-2.4

2.1

What is the type of "Daniel"?

2.2

What is the type of 'Computer'?

2.3

What happens when you add "Daniel" and 'Computer'?

2.4

What is the resulting type of adding "Daniel" and 'Computer'?

2.5

Is there a difference between single quotes and double quotes?

2.6

What happens when you add two strings?

Question 3

Use code to determine the answers to the question 3.1

3.1

What is the resulting type of adding 3 and '3'?

3.2

Can you explain why the error occured?

Question 4

Use code to determine the answers to the question 4.1-4.4

4.1

Make a list that contains 4 and 5.0.

4.2

Can you make a list that has 1, 2 and "tree"in it?

4.3

Can you make a list that contains 1, 2, "tree", and [4, 5.0]?

4.4

Think about another type we haven't used in Question 4. Can you put it in the list of [1, 2.0]?

4.5

What types can lists contain?

4.6

Add lists [5, 6, 'apple'] and [True, 12, 5.0] together. What happens?

Question 5

Use code to determine the answers to the question 5.1-5.4

5.1

Multiply 5 and 4.

5.2

Multiply 'Shiro' by 3.

5.3

Multiply 5.0 and 4.

5.4

Multiply ['Shiro', 5] by 4.

5.5

What patterns emerge when you multiply different types with an integer?

Question 6

The upper left corner that's colored represents the operator. For example, in figure 1, B represents the result of False and True.

Figure 1.

title

Figure 2.

title

6.1

What are the boolean values of A-H? Use your intuition.

A.

B.

C.

D.

E.

F.

G.

H.

6.2

Use code to check your above answers.

In [ ]:
# Explore here

6.3

Are 3 and '3' equivalent? Use code to confirm.

6.4

Are "Daniel" and "daniel" equivalent? Use code to confirm.

6.5

What is the result of (not False) == True?

6.6

Given x and without using False, make x have the value of False with one operator.

In [ ]:
x = True

Question 7

7.1

Define a variable x, that has a value of 5.

7.2

Define a variable y, that has a value of x.

7.3

What is the value of y?

7.4

Change x to 4. What is the value of y?

7.5

Define a variable z, that has a value of the sum of x and y

7.6

Assign x another value, 12. What are the values of y and z?

Question 8

8.1

Given the following lines of code, what is the value of the variable seven?

seven = 8

8.2

Given the following lines of code, are the variables shiro and Shiro equivalent?

shiro = 10
Shiro = 12

Question 9

9.1

Define a variable x that has the value 10 times 20.

9.2

Given x increment it by 3 by using two different operators.

9.3

Find the remainder of 78 divided by 14 using one operator from lecture. For example -

$16 = 5 * 3 + 1$ (1 is the remainder).

9.4

Given x, make x 10 times smaller using one operation. Return a value that has the type int.

In [ ]:
x = 200

Question 10

10.1

What type(s) can you multiply lists with, without creating an error?

Question 11

11.1

Write code to produce

a. 'HelloHelloHello'

b. 'Hello3'

using the 'Hello' and 3 (any type) exactly once each.

Question 12

12.1

Look at the following code

x = 99
x /= 10
x *= 50
y = x
y += 5
x -= y
y %= x

What is x and y?