{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# RWET Programming Exercise A: Expressions and strings\n", "\n", "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.\n", "\n", "## Task 1: Arithmetic expressions\n", "\n", "Add parentheses to the Python statement below so that it prints out the value 7.0.\n", "\n", "Expected output: `7.0`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "10 + 4 / 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 2: Expressions of inequality\n", "\n", "Change the operator in the statement below so that it displays `True` instead of `False`.\n", "\n", "Expected output: `True`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "14 > 15" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 3: Variable assignment\n", "\n", "Change the variable assignment below so that the cell evaluates to\n", "the number 32.\n", "\n", "Expected output: `32`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "my_number = 17\n", "my_number" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 4: Types\n", "\n", "Three variables are assigned below, all with different types.\n", "Replace the word `None` inside the parentheses of type() in the print\n", "statement below so that it prints `str`.\n", "\n", "Expected output: `str`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = 14\n", "y = 17.4\n", "z = \"mother said there'd be days like these\"\n", "type(None)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 5: String literals\n", "\n", "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.\n", "\n", "Expected output: `We aren't friends now.`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print('We are friends now.')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 6: Questions about strings\n", "\n", "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.\n", "\n", "Expected output: `51`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "first_line = \"It was the best of times.\"\n", "second_line = \"It was the worst of times.\"\n", "# your code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 7: Questions about strings, part 2\n", "\n", "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.\n", "\n", "Expected output: `37`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "romeo = \"But, soft! what light through yonder window breaks?\"\n", "# your code here!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 8: String transformations\"\n", "\n", "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.\n", "\n", "Expected output: `and the horse you rode in on`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "benediction = \" and the horse you rode in on \\n\"\n", "print(benediction)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 9: String transformations, part 2\n", "\n", "Using the previously defined `benediction` variable, write an\n", "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.\n", "\n", "Expected output: `AND THE HORSE YOU RODE IN ON`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(benediction) # your code here!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 10: String indexing\n", "\n", "Modify the value assigned to variable `offset` below so that the expression at the bottom of the cell evaluates to `'p'`.\n", "\n", "Expected output: `'p'`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\"apple\"[offset]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 11: String slices\n", "\n", "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'`.\n", "\n", "Expected output: `'yonder'`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "start = 0\n", "end = 10\n", "romeo = \"But, soft! what light through yonder window breaks?\"\n", "romeo[start:end]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Task 12: Integers and strings\n", "\n", "Modify the statement below so that it displays the number 100. Do this using the int() function (hint: you need to use it twice).\n", "\n", "Expected output: `100`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(\"19\" + \"81\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }