RWET Programming Exercise A: Expressions and strings

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.

Task 1: Arithmetic expressions

Add parentheses to the Python statement below so that it prints out the value 7.0.

Expected output: 7.0

In [ ]:
10 + 4 / 2

Task 2: Expressions of inequality

Change the operator in the statement below so that it displays True instead of False.

Expected output: True

In [ ]:
14 > 15

Task 3: Variable assignment

Change the variable assignment below so that the cell evaluates to the number 32.

Expected output: 32

In [ ]:
my_number = 17
my_number

Task 4: Types

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

In [ ]:
x = 14
y = 17.4
z = "mother said there'd be days like these"
type(None)

Task 5: String literals

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.

In [ ]:
print('We are friends now.')

Task 6: Questions about strings

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

In [ ]:
first_line = "It was the best of times."
second_line = "It was the worst of times."
# your code here

Task 7: Questions about strings, part 2

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

In [ ]:
romeo = "But, soft! what light through yonder window breaks?"
# your code here!

Task 8: String transformations"

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

In [ ]:
benediction = "     and the horse you rode in on    \n"
print(benediction)

Task 9: String transformations, part 2

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

In [ ]:
print(benediction) # your code here!

Task 10: String indexing

Modify the value assigned to variable offset below so that the expression at the bottom of the cell evaluates to 'p'.

Expected output: 'p'

In [ ]:
"apple"[offset]

Task 11: String slices

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'

In [ ]:
start = 0
end = 10
romeo = "But, soft! what light through yonder window breaks?"
romeo[start:end]

Task 12: Integers and strings

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

In [ ]:
print("19" + "81")