Skip to content

Instantly share code, notes, and snippets.

@nikokozak
Created February 17, 2026 23:40
Show Gist options
  • Select an option

  • Save nikokozak/ffa362aed8d4404bb3e345e5b383821f to your computer and use it in GitHub Desktop.

Select an option

Save nikokozak/ffa362aed8d4404bb3e345e5b383821f to your computer and use it in GitHub Desktop.
Programming Data, Exercise #1
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "59c5ac0d",
"metadata": {},
"source": [
"# Exercise #1: Python basics\n",
"\n",
"To do this exercise, download this notebook and put it in the same folder as your other notebooks. You should see the notebook appear in your Jupyter Notebook home page.\n",
"\n",
"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. Try to make the **smallest change possible**. Many exercises can be solved by changing a single value or variable name.\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": 2,
"id": "62a04c8c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"12.0"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"10 + (4 / 2)"
]
},
{
"cell_type": "markdown",
"id": "23480505",
"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": 3,
"id": "22e8589d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"14 < 15"
]
},
{
"cell_type": "markdown",
"id": "5bb8390d",
"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": 4,
"id": "68298b71",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"32"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_number = 32\n",
"my_number"
]
},
{
"cell_type": "markdown",
"id": "3dc69bd2",
"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": 5,
"id": "d9ce2da5",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"str"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = 14\n",
"y = 17.4\n",
"z = \"mother said there'd be days like these\"\n",
"type(z)"
]
},
{
"cell_type": "markdown",
"id": "2e69b9fc",
"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": 6,
"id": "00d51090",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"We aren't friends now.\n"
]
}
],
"source": [
"print('We aren\\'t friends now.')"
]
},
{
"cell_type": "markdown",
"id": "72a84c11",
"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": 1,
"id": "f305d467",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"51"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"first_line = \"It was the best of times.\"\n",
"second_line = \"It was the worst of times.\"\n",
"len(first_line) + len(second_line)"
]
},
{
"cell_type": "markdown",
"id": "9fec3bc0",
"metadata": {},
"source": [
"## Task 7: Questions about strings, part 2\n",
"\n",
"Write an expression at the bottom of the following cell that evaluates to the position of the word `window` in the string defined in the variable called `romeo.` (Hint: Use the [`.find()` method](https://docs.python.org/3/library/stdtypes.html#str.find).)\n",
"\n",
"Expected output: `37`"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "3f59bfeb",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"37"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"romeo = \"But, soft! what light through yonder window breaks?\"\n",
"romeo.find(\"window\")"
]
},
{
"cell_type": "markdown",
"id": "99c9dc8a",
"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. (Hint: Use the [`.strip()` method](https://docs.python.org/3/library/stdtypes.html#str.strip)).\n",
"\n",
"Expected output: `and the horse you rode in on`"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "fbb91462",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"and the horse you rode in on\n"
]
}
],
"source": [
"benediction = \" and the horse you rode in on \\n\"\n",
"print(benediction.strip())"
]
},
{
"cell_type": "markdown",
"id": "9061fceb",
"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. (Hint: [Use the `.upper()` method](https://docs.python.org/3/library/stdtypes.html#str.upper)]).\n",
"\n",
"Expected output: `AND THE HORSE YOU RODE IN ON`"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "38e7fc3b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"AND THE HORSE YOU RODE IN ON\n"
]
}
],
"source": [
"print(benediction.strip().upper()) # your code here!"
]
},
{
"cell_type": "markdown",
"id": "c4b42eeb",
"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": 16,
"id": "793e1fc2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'p'"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"offset = 2\n",
"\"apple\"[offset]"
]
},
{
"cell_type": "markdown",
"id": "5fc46bc6",
"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": 19,
"id": "02049d7e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'yonder'"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"start = 30\n",
"end = 36\n",
"romeo = \"But, soft! what light through yonder window breaks?\"\n",
"romeo[start:end]"
]
},
{
"cell_type": "markdown",
"id": "2e52f5fd",
"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": 21,
"id": "1b70cb0b",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"100\n"
]
}
],
"source": [
"print(int(\"19\") + int(\"81\"))"
]
},
{
"cell_type": "markdown",
"id": "756fc85a",
"metadata": {},
"source": [
"## Task 13: Simple indexing and list functions\n",
"\n",
"There is a list of numbers assigned to a variable `number_list` in the code below that has been assigned a list of numbers. Run this cell before filling in the answers below."
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "4621bf85",
"metadata": {},
"outputs": [],
"source": [
"number_list = [-2, -58, 4, 36, -6, 60, -57]"
]
},
{
"cell_type": "markdown",
"id": "58afb7c3",
"metadata": {},
"source": [
"Write another expression in the cell below, using square bracket index notation, that causes the 4th element of `number_list` (i.e., the number `36`) to be displayed when running the code in the cell.\n",
"\n",
"Expected output: `36`"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "4507ff6f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"36"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"number_list[3]"
]
},
{
"cell_type": "markdown",
"id": "a23dd942",
"metadata": {},
"source": [
"Now write an expression that evaluates to the number of items in the list (i.e., `7`), using the `len()` function.\n",
"\n",
"Expected output: `7`"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "945fe37b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(number_list)"
]
},
{
"cell_type": "markdown",
"id": "0be74470",
"metadata": {},
"source": [
"The following expression:\n",
"\n",
" max(number_list)\n",
" \n",
"... will evaluate to the largest value in `number_list` (i.e., `60`). Change the variable `x` in the code below so that the expression\n",
"\n",
" sorted(number_list)[x]\n",
" \n",
"... does the same thing. (i.e., when you run the cell, it should display `60`.)\n",
"\n",
"Expected output: `60`"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "2e1ed0ec",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"60"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = -1\n",
"sorted(number_list)[x]"
]
},
{
"cell_type": "markdown",
"id": "31cab481",
"metadata": {},
"source": [
"## Task 14: List slices\n",
"\n",
"In the cell below, insert integer values before and after the colon in the list index so that the expression evaluates to a slice of `number_list` starting with its second element and ending with its fifth element (exclusive).\n",
"\n",
"Expected output: `[-58, 4, 36]`"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "9e5d6957",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[-58, 4, 36]"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"number_list[1:4]"
]
},
{
"cell_type": "markdown",
"id": "e80377da",
"metadata": {},
"source": [
"Now, insert integer values before and after the colon in the expression below so that the expression evaluates to a slice of `number_list` starting with its third element and ending at the end of the list.\n",
"\n",
"Expected output: `[4, 36, -6, 60, -57]`"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "864ae597",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[4, 36, -6, 60, -57]"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"number_list[2:]"
]
},
{
"cell_type": "markdown",
"id": "397f6b7f",
"metadata": {},
"source": [
"Finally, fill in a value for the variable `x` below so that the expression evaluates to a slice of `number_list` starting at the second-to-last element of the list and ending at the end of the list. (Hint: `x` should be a negative integer.)\n",
"\n",
"Expected output: `[60, -57]`"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "5521b5cf",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[60, -57]"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = -2\n",
"number_list[x:]"
]
},
{
"cell_type": "markdown",
"id": "ffdf6c83",
"metadata": {},
"source": [
"## Task 15: List comprehensions\n",
"\n",
"For this problem set, I'm introducing a new Python operator: the modulo operator, `%`. This operator returns the *remainder* of dividing one integer by another. For example:"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "a736efa9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"22 % 3"
]
},
{
"cell_type": "markdown",
"id": "45839d8c",
"metadata": {},
"source": [
"This expression evaluates to `1` because the remainder of dividing `22` by `3` is `1`. We can use the modulo operator to test whether or not a number is even (i.e., divisible by 2), by using the number `2` on the right side of the operator:"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "ee2fdff3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"100 % 2"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "e720b8a0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"101 % 2"
]
},
{
"cell_type": "markdown",
"id": "f6c5c298",
"metadata": {},
"source": [
"Given the above information, modify the list comprehension below so that it evaluates to a list containing *only* the members of `number_list` that are *divisible by three*. (Hint: Use the modulo operator in the membership expression of the list comprehension.)\n",
"\n",
"Expected output: `[36, -6, 60, -57]`"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "72e99738",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[36, -6, 60, -57]"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[item for item in number_list if not item % 3]"
]
},
{
"cell_type": "markdown",
"id": "ffa9cade",
"metadata": {},
"source": [
"## Task 16: Splitting strings\n",
"\n",
"In the cell below, a variable `float_str` is set to a string containing a list of floating-point numbers, separated by semicolons (`;`). (Make sure to run this cell before you proceed, so that the variable will be available in subsequent cells.)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "69263b6d",
"metadata": {},
"outputs": [],
"source": [
"float_str = \"5.8;6.9;3.1;5.9;6.6;6.5;6.5;5.6;6;6.4;3.32;6.0;6.0;6.3;6.6;6.6\""
]
},
{
"cell_type": "markdown",
"id": "410a4d3a",
"metadata": {},
"source": [
"Modify the list comprehension below so that it evaluates to a list of floating-point numbers. The type of the expression should be `list` and the type of individual elements in the list should be `float`. (Hint: You'll need to use the `float()` function in the list comprehension's predicate expression, and call the `.split()` method on the string.)\n",
"\n",
"Expected output:\n",
"\n",
" [5.8, 6.9, 3.1, 5.9, 6.6, 6.5, 6.5, 5.6, 6.0, 6.4, 3.32, 6.0, 6.0, 6.3, 6.6, 6.6]"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "ee70bf4b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[5.8,\n",
" 6.9,\n",
" 3.1,\n",
" 5.9,\n",
" 6.6,\n",
" 6.5,\n",
" 6.5,\n",
" 5.6,\n",
" 6.0,\n",
" 6.4,\n",
" 3.32,\n",
" 6.0,\n",
" 6.0,\n",
" 6.3,\n",
" 6.6,\n",
" 6.6]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"float_list = [float(item) for item in float_str.split(';')]\n",
"float_list"
]
},
{
"cell_type": "markdown",
"id": "ff3586c2",
"metadata": {},
"source": [
"Using the expression you wrote above as a starting point, write an expression below that evaluates to the *sum* of the numbers in the list.\n",
"\n",
"Expected output: `94.11999999999999` (or close to that)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"id": "7ac39a33",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"94.11999999999999"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum(float_list)"
]
},
{
"cell_type": "markdown",
"id": "2d6f5df1",
"metadata": {},
"source": [
"## Task 17: Strings in list comprehensions\n",
"\n",
"In the cell below, I've defined a list of strings and assigned it to a variable called `greek`. Make sure to run this cell before you continue."
]
},
{
"cell_type": "code",
"execution_count": 47,
"id": "a68a5f89",
"metadata": {},
"outputs": [],
"source": [
"greek = [\"alpha\", \"beta\", \"gamma\", \"delta\", \"epsilon\"]"
]
},
{
"cell_type": "markdown",
"id": "44e50cf0",
"metadata": {},
"source": [
"Okay. Your job in the next cell is to modify the list comprehension so that it evaluates to a list of strings in `greek` that contain *exactly five letters*. (Hint: You'll need to add a predicate expression.)\n",
"\n",
"Expected output: `[\"alpha\", \"gamma\", \"delta\"]`"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "81e5029b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['alpha', 'gamma', 'delta']"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[item for item in greek if len(item) == 5]"
]
},
{
"cell_type": "markdown",
"id": "176bcb3b",
"metadata": {},
"source": [
"In the cell below, write an expression that evaluates to a string containing each of the *first letters* of each string in `greek`. This one is tricky—take your time and think it through. Hint: You'll need to use a list comprehension and the `.join()` method.\n",
"\n",
"Expected output: `'abgde'`"
]
},
{
"cell_type": "code",
"execution_count": 54,
"id": "0b576325",
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"'abgde'"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"\".join([item[0] for item in greek])"
]
},
{
"cell_type": "markdown",
"id": "779132eb",
"metadata": {},
"source": [
"## Task 18: For loops\n",
"\n",
"The cell below has the skeleton of the `for` loop written for you. Replace the string `\"blip\"` with an expression such that the cell, when executed, outputs the first ten multiples of five.\n",
"\n",
"Expected output:\n",
"\n",
" 0\n",
" 5\n",
" 10\n",
" 15\n",
" 20\n",
" 25\n",
" 30\n",
" 35\n",
" 40\n",
" 45\n"
]
},
{
"cell_type": "code",
"execution_count": 56,
"id": "945c3d96",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"5\n",
"10\n",
"15\n",
"20\n",
"25\n",
"30\n",
"35\n",
"40\n",
"45\n"
]
}
],
"source": [
"for i in range(10):\n",
" print(i*5)"
]
},
{
"cell_type": "markdown",
"id": "69c6028a",
"metadata": {},
"source": [
"Using the variable `greek` that was defined earlier, write a `for` loop in the cell below so that when the cell is executed it outputs, on separate lines, each of the elements of the list in upper case.\n",
"\n",
"Expected output:\n",
"\n",
" ALPHA\n",
" BETA\n",
" GAMMA\n",
" DELTA\n",
" EPSILON"
]
},
{
"cell_type": "code",
"execution_count": 60,
"id": "44bed222",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ALPHA\n",
"BETA\n",
"GAMMA\n",
"DELTA\n",
"EPSILON\n"
]
}
],
"source": [
"for i in range(len(greek)):\n",
" print(greek[i].upper())"
]
},
{
"cell_type": "markdown",
"id": "a23cbc54",
"metadata": {},
"source": [
"You're done! Good job."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "05ef2305-364d-4b00-8101-2e8318ad6cce",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.14.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment