JamCoders

💾 Download

Lecture 1, Part 1 Exercises

Instructions

Welcome to Jamcoders! This is a Jupyter Notebook with some exercises based on lecture. Jupyter Notebook allows you to write Python code in boxes called cells which you can run individually (by pressing CTRL+ENTER or SHIFT+ENTER).

The goal of this notebook is to review the following topics:

  • Comments
  • Math in Python
  • Printing
  • Types
  • Variables

Question 1: Comments, Math, Printing

Use code to answer 1.1-1.5

1.1

Comments do not change how code runs, but are important for explaining the code in words.

Write any comment (starting with #) in the cell below

In [ ]:

1.2

You can do math using symbols like + (add), - (subtract), * (multiply), and \ (divide).

Write 2000 + 22 below:

In [ ]:

1.3

You can print using print().

Print the your favorite number below:

In [ ]:

1.4

Keep in mind that Python does have an order of operations.

Write two lines of code.

  1. In the first, print 335 + 2 x 6
  2. In the second, print (335 + 2) x 6
In [2]:

Out[2]:
<function print>

1.5

Print any math expression of your choice, and comment your code.

In [ ]:

Question 2: Integer and Float Types

So far, we have been doing math with Integers, but there are other types!

Use code answer 2.1-2.4

2.1

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

In [ ]:

2.2

What about the type of 1.0?

In [ ]:

2.3

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

In [ ]:

2.4

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

In [ ]:

2.5

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

Write the answer in a comment below

In [ ]:

Question 3: String Types

Use code to answer 3.1-3.4

3.1

What is the type of "BoazBarak"?

In [ ]:

3.2

What is the type of 'Obama'?

In [ ]:

3.3

What happens when you add "BoazBarak" and 'Obama' (with the + operator)?

In [ ]:

3.4

What is the resulting type of adding "BoazBarak" and 'Obama'?

In [ ]:

3.5

Is there a difference between single quotes and double quotes?

Write the answer in a comment below

In [ ]:

3.6

What happens when you add two strings (with the + operator)?

Write the answer in a comment below

In [ ]:

Question 4: Type Error

4.1

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

In [ ]:

4.2

Can you explain why the error occured (in a comment)?

In [ ]:

Question 5: Lists

5.1

Make a list that contains 4 and 5.0.

In [ ]:

5.2

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

In [ ]:

5.3

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

In [ ]:

5.4

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

In [ ]:

5.5

What types can lists contain?

Write the answer in a comment below

In [ ]:

5.6

Add lists ['I', 'love'] and ['to c', 0, 'de'] together. What happens?

In [ ]:

5.7

In a comment below, what does adding two lists seem to do?

In [ ]:

Question 6: Multiplication Operator

6.1

Multiply 5 and 4.

In [ ]:

6.2

Multiply 'Barak' by 3.

In [ ]:

6.3

Multiply 5.0 and 4.

In [ ]:

6.4

Multiply ['Barak', 5] by 4.

In [ ]:

6.5

In a comment below, what patterns emerge when you multiply different types with an integer?

In [ ]:

6.6

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

In [ ]:
# Explore here!

Question 7: Boolean Operators

Figure 1

AND True False
True A B
False C D

Figure 2

OR True False
True E F
False G H

7.1

What are the boolean values of A through H? Use your intuition to fill out the empty list in the text cell below

Fill out this list.

A.

B.

C.

D.

E.

F.

G.

H.

7.2

Use code to check your above answers.

In [ ]:
# Explore here (using keywords like: True, False, and, or)

7.3

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

In [ ]:

7.4

Are "Boaz" and "Boaz" equivalent? Use code to confirm.

In [ ]:

7.5

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

In [ ]:

Question 8: Variables

8.1

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

In [ ]:

8.2

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

In [ ]:

8.3

What is the value of y?

In [ ]:

8.4

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

In [ ]:

8.5

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

In [ ]:

8.6

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

In [ ]:

8.7

Define a variable x and assign it the value of False, but without using the keyword False.

Hint: You can do it using one special operator.

In [ ]:

Question 9: Tricky Variable Names

Suppose you are reading the following code:

seven = 8
boaz = 'boaz'
Boaz = 'boaz'
four = 4
ten = 16

Answer the following either in words in a comment, or with code

9.1

What is the value of the variable seven?

In [ ]:

9.2

What is the value of the variable ten minus the variable four?

In [ ]:

9.3

Is the value of the variable boaz the same as the value of the variable Boaz?

In [ ]:

9.4

Is the value of the string 'boaz' the same as the value of the string 'Boaz'?

In [ ]:

Question 10: Changing Variables

10.1

Define a variable x to have the value of the first number of your age.

(For example, if you are 16, then the first number is 1)

In [ ]:

10.2

Now set x to be five times its original value

In [ ]:

10.3

Now increase (or increment) x by 3 using the += operator

In [ ]:

10.4

Multiply x by 2 using the *= operator

In [ ]:

10.5

Increment x by the second number of your age

(For example, if you are 16, then then increment x by 6)

In [ ]:

10.6

Decrement x by 6

In [ ]:

10.7

Now I'll guess your age! Print the value of x

(You may have to re-run run cells 10.1-10.7 each once for this to work)

In [ ]:

10.8

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

(For example, the remainder of 16 divided by 5 is 1, because $16 = 5 * 3 + 1$)

In [ ]:

10.9

Given some value for y, make y 10 times smaller (ignoring the remainder) using one operation. After you are done, y should have type int.

In [ ]:

Question 11: Putting it all together

11.1

Write code to produce

a. 'BoazBoazBoaz'

b. 'Boaz3'

You should only type 'Boaz' once and 3 once for each case.

In [ ]:

11.2

Look at the following code

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

What is x and y? Try to do it in your head (or on paper) first before trying it with code.

In [ ]: