You will be working on a series of exercises for the material covered in lecture 2a.
We covered:
Remember, programming variables are NOT mathematical variables!
We will start with simple variable exercises. In order to see the value, make sure to put the variable at the end
Assign the following values to x
, one per cell:
523798
True
"this is a string"
And make sure it's visible in Jupyter!
We will now focus on variable reassignment. For each of the following code blocks, guess what the output will be. Then, check your answer in Jupyter.
x = 7
x = 3
x
x = 7
y = x
y
x = 7
y = x
x
As with exercise 2, for each of the following code blocks, guess what the output will be. Write your guesses as comments in the code cells below! Then, check your answer in Jupyter.
x = 3
y = x
x = 4
y
x = 3
y = x
x = 4
x
# My guess for y:
# My guess for x:
As with exercises 2 and 3, for each of the following code blocks, guess what the output will be. Write your guesses as comments in the code cells below! Then, check your answer in Jupyter.
x = "hello"
y = " world"
x + y
x = "hello"
y = " world"
z = x + y
x = "bye"
z
x = True
y = 3
z = x + y
z
What happens when you try to do a computation with a variable which you have not yet given a value? Run the following code:
a = 5
x = a + b
x
What do you need to add to the code so that it does not give an error?
Given this code:
x = 12
y = 5
z = y
y = 12
x = 1 + z
z = x + y + z
What is the value of x, y and z? Write your guesses as comments in the code cells below!
Then verify your solution by running the code above and adding a print statement for each of the variables at the end so that you can see the values of the variables.
Define a variable x
, that has a value of 5
.
Define a variable y
, that has a value of x
.
What is the value of y
?
Change x
to 4
. What is the value of y
?
Define a variable z
, that has a value of the sum of x
and y
Assign x
another value, 12
. What are the values of y
and z
?
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.
Given the following lines of code, what is the value of the variable seven
?
seven = 8
Given the following lines of code, are the variables shiro
and Shiro
equivalent?
shiro = 10
Shiro = 12
How can you look at the values of these variables? Use the print
function to print the values of both!
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 and write your guesses as comments in the cell below. Only then try running it with code (use print statements to print the values of x
and y
).
Define a variable x
that has the value 10
times 20
.
Now increment x
by 3.
Can you increment x
by 3 in a different way (using a different operator)?
Find the remainder of 78
divided by 14
using one operator from lecture. For example -
$16 = 5 * 3 + 1$ (1 is the remainder).
Given x
, make x
10 times smaller using one operation. Return a value that has the type int
.
Write one line of code each that modifies the variable containing "Shiro"
so that it contains the new value
a. "ShiroShiroShiro"
b. "Shiro3"
shiro = "Shiro"
# Add one line of code here that modifies shiro
shiro = "Shiro"
# Add one line of code here that modifies shiro
The following code computes the area of the shape below.
However, it is not very easy to understand what happens in each line. Add a comment at the end of each line explaining what is going on!
a = 3
s = 3**2
d = 0.5*3.14*(a/2)**2
t = 3**0.5 / 4 * a**2
aa = s + d + t
aa
Adding comments to complicated code is generally good practice, because it helps other people (and also yourself if you read your code in the future) to understand your code more easily.
One other thing that helps make code easier to understand is to use variable names that represent what is stored in the variable. For example, instead of aa
, you could use total_area
. Copy the code from above and rename the variables that are used in the code to make it easier to understand!
Conditionals refers to if
, elif
, else
statements, which the next few exercises are about.
Define a list l
. Write code that prints "This list is empty" if the list is empty (has no elements), and "This list has N elements" (where N is the number of elements) otherwise. Make sure your code works even if you change l
.
Define a positive floating point number x
.
Write code that computes and prints the rounded version of that x
. Make sure it works for all values of x
.
Example:
x = 1.2
--> 1x = 12.6
--> 13x = 0.5
--> 1Hint: You can round down a number by converting it to an integer. Could that be useful here?
Different ages correspond to different school in Ethiopia. This shows these categories:
0 - 5 No School
6 - 13 Elementary School
14 - 18 High School
19 - 22 University
23 - 100 No School
Write a variable for Daniel that stores his age (an integer between 0 and 100).
Now using this variable, how would you write a conditional to check if Daniel is in elementary school? If this is True
, print the string "Elementary School"
.
# Write code here to verify if Daniel is in "Elementary School" using conditionals (`if`, `elif`, `else`)
Similar to above, how would you write a conditional to check if Daniel is in the "No School"
category? Make sure to verify both cases ("Elementary School"
and "No School"
) and if the age is in those ranges, print the string "No School"
and "Elementary School"
respectively.
# Write code here to verify if Daniel is in "Elementary School" or "No School" using conditionals (`if`, `elif`, `else`)
Write a conditional that prints "No School"
if the person is not in school or "School"
if the person is in school.
Now using the code pieces you just wrote and your knowledge of conditionals, write a conditional that prints the category of school they are in (string). Test it for different ages to make sure that it works as intended.
The sign of a number is +1 if the number is positive, -1 if the number is negative, and 0 if it is 0.
Define a variable n1
, and write code that computes and prints its sign
Define another variable n2
, and write code that computes and prints the absolute value of that number.
Using the code from above, write code that takes two variables n1
and n2
and computes and prints the number n2
but with the sign of n1
.
Example:
n1 = 16
and n2 = -2
--> 2n1 = 16
and n2 = -14
--> 14n1 = -16
and n2 = -2
--> -2n1 = -16
and n2 = 2
--> -2n1 = 0
and n2 = 2
--> 0