To do this exercise, download this notebook and open it on your own computer. There are several tasks described below. Your job is to change the code in the cells so that the output from running the cell matches the expected output indicated above the cell.
Add parentheses to the Python statement below so that it prints out the value 7.0.
Expected output: 7.0
10 + 4 / 2
Change the operator in the statement below so that it displays True
instead of False
.
Expected output: True
14 > 15
Change the variable assignment below so that the cell evaluates to the number 32.
Expected output: 32
my_number = 17
my_number
Three variables are assigned below, all with different types.
Replace the word None
inside the parentheses of type() in the print
statement below so that it prints str
.
Expected output: str
x = 14
y = 17.4
z = "mother said there'd be days like these"
type(None)
Modify the statement below so that it displays the string "We aren't friends now." (i.e., change "are" to "aren't".) Use a single quoted string---don't change it to double quotes.
Expected output: We aren't friends now.
print('We are friends now.')
In the cell below, on a line directly following the two variable assignments, write an expression that evaluates to the sum of the lengths of the two string variables defined in the cell (first_line
and second_line
). Use the len()
function.
Expected output: 51
first_line = "It was the best of times."
second_line = "It was the worst of times."
# your code here
Inside the parentheses of the print
function below, write an expression that evaluates to the position of the word window
in the string defined in the variable called romeo.
Use the .find()
method.
Expected output: 37
romeo = "But, soft! what light through yonder window breaks?"
# your code here!
Modify the expression inside the parentheses of the print
function below so that it displays the contents of the variable "benediction", but with all white space removed from the beginning and end of the string. Use the .strip()
method.
Expected output: and the horse you rode in on
benediction = " and the horse you rode in on \n"
print(benediction)
Using the previously defined benediction
variable, write an
expression in the parentheses of the print
function below so that running the cell displays to the content of the string, with all whitespace removed, and with all letters converted to uppercase. Use the .upper()
method.
Expected output: AND THE HORSE YOU RODE IN ON
print(benediction) # your code here!
Modify the value assigned to variable offset
below so that the expression at the bottom of the cell evaluates to 'p'
.
Expected output: 'p'
"apple"[offset]
Modify the values assigned to variables start
and end
below so that the expression at the bottom of the cell evaluates to the string 'yonder'
.
Expected output: 'yonder'
start = 0
end = 10
romeo = "But, soft! what light through yonder window breaks?"
romeo[start:end]
Modify the statement below so that it displays the number 100. Do this using the int() function (hint: you need to use it twice).
Expected output: 100
print("19" + "81")