2.18. Chapter 2 Assessment¶
Check your understanding
There is a function we are providing in for you in this problem called square
. It takes one integer and returns the square of that integer value. Write code to assign a variable called xyz
the value 5*5
(five squared). Use the square function, rather than just multiplying with *
.
Write code to assign the number of characters in the string rv
to a variable num_chars
.
- a = len("hello worldwelcome!")
- Though we are using the len function here, we are hardcoding what len should return the length of. We are not referencing z or y.
- a = 11 + 8
- This is hardcoding, we are writing in the value without referencing z or y.
- a = len(z) + len(y)
- This is not considered hard coding. We are using the function len to determine the length of what is stored in z and y, which is a correct way to approach this problem.
- a = len("hello world") + len("welcome!")
- Though we are using the len function here, we are hardcoding what len should return the length of each time we call len. We are not referencing z or y.
- none of the above are hardcoding.
- At least one of these solutions is considered hardcoding. Take another look.
The code below initializes two variables, z
and y
. We want to assign the total number of characters in z
and in y
to the variable a
. Which of the following solutions, if any, would be considered hard coding?
z = "hello world"
y = "welcome!"
(This is not an assessment question) The code below defines functions used by one of the questions above. Do not modify the code, but feel free to take a look.
The image above shows a program that just completed executing (we know this because the green arrow is on the last line and there is no red arrow in the image). At the end of the program, which variables changed from their original value during the program’s execution? List each variable in the box below, separated by a white space.
- an error
- This is a correct. When using + to stick strings together, all variables need to be a string. This throws a type error.
- sara 11
- Hmm, this would be true if 11 was a string but it isn't
- sara11
- Hmm, not quite, we stick a space on our string in line 3
- "sara11"
- Hmm, not quite, we stick a space on our string in line 3 and we don't have quotes
- "sara 11"
- Hmm, we don't have quotes in our string
:

What does the following code shown in the codelens image print when it finishes executing?
- 1
- This is a variable being asssigned a string value
- 2
- This is a variable being assigned an integer value
- 3
- Correct, "sara" + " " + age is an expression that we want to evaluate to "sara 11" and store the string in the info variable
- 4
- This prints the string value stored the info variable
:

Here is that same image of code again. Which line number shows a line of code we would define as an expression?