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:
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
1.2
You can do math using symbols like +
(add), -
(subtract), *
(multiply), and \
(divide).
Write 2000 + 22
below:
1.3
You can print using print()
.
Print the your favorite number below:
1.4
Keep in mind that Python does have an order of operations.
Write two lines of code.
335 + 2 x 6
(335 + 2) x 6
1.5
Print any math expression of your choice, and comment your code.
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.
2.2
What about the type of 1.0
?
2.3
What is the resulting type of adding an 1
and 1
?
2.4
What is different about the resulting type of adding 1
and 1.0
?
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
Use code to answer 3.1-3.4
3.1
What is the type of "BoazBarak"
?
3.2
What is the type of 'Obama'
?
3.3
What happens when you add "BoazBarak"
and 'Obama'
(with the +
operator)?
3.4
What is the resulting type of adding "BoazBarak"
and 'Obama'
?
3.5
Is there a difference between single quotes and double quotes?
Write the answer in a comment below
3.6
What happens when you add two strings (with the +
operator)?
Write the answer in a comment below
4.1
What is the resulting type of adding 3
and '3'
?
4.2
Can you explain why the error occured (in a comment)?
5.1
Make a list that contains 4
and 5.0
.
5.2
Can you make a list that has 1
, 2
and "Boaz"
in it?
5.3
Can you make a list that contains 1
, 2
, "Boaz"
, and [4, 5.0]
?
5.4
Think about another type we haven't used in Question 5. Can you put it inside the list [1, 2.0]
?
5.5
What types can lists contain?
Write the answer in a comment below
5.6
Add lists ['I', 'love']
and ['to c', 0, 'de']
together. What happens?
5.7
In a comment below, what does adding two lists seem to do?
6.1
Multiply 5
and 4
.
6.2
Multiply 'Barak'
by 3
.
6.3
Multiply 5.0
and 4
.
6.4
Multiply ['Barak', 5]
by 4
.
6.5
In a comment below, what patterns emerge when you multiply different types with an integer?
6.6
What type(s) can you multiply lists with, without creating an error?
# Explore here!
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.
# Explore here (using keywords like: True, False, and, or)
7.3
Are 3
and '3'
equivalent? Use code to confirm.
7.4
Are "Boaz"
and "Boaz"
equivalent? Use code to confirm.
7.5
What is the result of (not False) == True
?
8.1
Define a variable x
, that has a value of 5
.
8.2
Define a variable y
, that has a value of x
.
8.3
What is the value of y
?
8.4
Change x
to 4
. What is the value of y
?
8.5
Define a variable z
, that has a value of the sum of x
and y
8.6
Assign x
another value, 12
. What are the values of y
and z
?
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.
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
?
9.2
What is the value of the variable ten
minus the variable four
?
9.3
Is the value of the variable boaz
the same as the value of the variable Boaz
?
9.4
Is the value of the string 'boaz'
the same as the value of the string 'Boaz'
?
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)
10.2
Now set x
to be five times its original value
10.3
Now increase (or increment) x
by 3 using the +=
operator
10.4
Multiply x
by 2 using the *=
operator
10.5
Increment x
by the second number of your age
(For example, if you are 16, then then increment x
by 6)
10.6
Decrement x
by 6
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)
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$)
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
.
11.1
Write code to produce
a. 'BoazBoazBoaz'
b. 'Boaz3'
You should only type 'Boaz'
once and 3
once for each case.
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.