Skip to content

Instantly share code, notes, and snippets.

@drvinceknight
Created March 5, 2024 08:56
Show Gist options
  • Save drvinceknight/777d529bba56e61a0fb63ae01ac8eda4 to your computer and use it in GitHub Desktop.
Save drvinceknight/777d529bba56e61a0fb63ae01ac8eda4 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "b45aea81-6f65-4d59-af86-976fec11201e",
"metadata": {},
"source": [
"# Python for Mathematics"
]
},
{
"cell_type": "markdown",
"id": "da586acd-2d7c-487f-88d4-b85740eea07e",
"metadata": {},
"source": [
"In this workshop we will solve mathematical problems. We will do this by hand but also using tools available at our fingerprints in the programming language Python.\n",
"\n",
"This is based on the Namibian curriculum for Mathematics for grades 4 to 7. Which includes topics:\n",
"\n",
"1. Data Handling\n",
"2. Whole numbers\n",
"3. Computation"
]
},
{
"cell_type": "markdown",
"id": "85dce803-5f63-4186-b3db-dd2cda2892d1",
"metadata": {},
"source": [
"## 1 Whole numbers\n",
"\n",
"### Counting\n",
"\n",
"- Grade 4: \"_Understand the principle of counting up to 10,000 using a variety of counting strategies_\"\n",
"- Grade 5: \"_Understand the principle of counting up to 100,000 using a variety of counting strategies and develop a sense for the base ten number system_\"\n",
"- Grade 6: \"_Understand the principle of counting up to 100,000 using a variety of counting strategies and develop a sense for the base ten number system_\"\n",
"- Grade 7: \"_Know how to compare and order whole numbers and round number within the number range to 1,000,000_\""
]
},
{
"cell_type": "markdown",
"id": "cc954521-3141-4529-8d80-6b76a461ebfe",
"metadata": {},
"source": [
"#### Exercise\n",
"\n",
"---\n",
"\n",
"Count from 27 to 59 in 3s.\n",
"\n",
"---\n",
"\n",
"**Do this by hand and write down your count.**\n",
"\n",
"Let us use `range` to do this."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "903ccb6c-dcfe-478c-89b4-2699b641966b",
"metadata": {},
"outputs": [],
"source": [
"instructions = range(27, 59, 3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d362e777-1da2-4bc7-b4d4-ed2f24c1eb53",
"metadata": {},
"outputs": [],
"source": [
"for number in instructions:\n",
" print(number)"
]
},
{
"cell_type": "markdown",
"id": "bcf5b2eb-a1ff-4dc9-9da1-2b8a52927b79",
"metadata": {},
"source": [
"Questions:\n",
"\n",
"- What do you think `range(27, 59, 3)` does? What are the roles of the `27`, `59` and `3`?\n",
"- Is the above right? What about the start and the end?"
]
},
{
"cell_type": "markdown",
"id": "d99e659d-2df7-4c4a-b381-c2870412b0d8",
"metadata": {
"jp-MarkdownHeadingCollapsed": true
},
"source": [
"#### Self work questions:\n",
"\n",
"Either by hand or using `range` (or both!) answer the following questions:\n",
"\n",
"1. Count from 27 to 62 in 4s.\n",
"2. Count from 2 to 5 in 2s.\n",
"3. Count from 1 to 550 in 10s.\n",
"4. Count backwards from 50 to 4 in 7s."
]
},
{
"cell_type": "markdown",
"id": "ab446a6c-5344-4405-bdcf-3db48eb81584",
"metadata": {},
"source": [
"### Rounding\n",
"\n",
"- Grade 4: \"_Develop the skill to use approximation and of rounding numbers to facilitate estimation of answers to calculations_\"\n",
"- Grade 5: \"_Develop the skill to use approximation to reduce the complexity of large numbers and to facilitate controlled estimation_\"\n",
"- Grade 6: \"_Develop the skill to use approximation to reduce the complexity of large numbers and to facilitate controlled estimation_\"\n",
"- Grade 7: \"_Know how to compare and round numbers within the range 1 to 1,000,000_\""
]
},
{
"cell_type": "markdown",
"id": "6cc478c1-616b-4193-8c9b-05cd508811bb",
"metadata": {},
"source": [
"#### Exercise\n",
"\n",
"---\n",
"\n",
"Round 534 to the nearest 10, 20, 100 and the nearest 1,000.\n",
"\n",
"---\n",
"\n",
"**Do this by hand and write down your count.**\n",
"\n",
"Let us use `round` do this."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8dbbcee9-32b3-4ab8-881c-a427e1547b5a",
"metadata": {},
"outputs": [],
"source": [
"round(534 / 10) * 10"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "29fea90c-d8b6-4e97-86fb-e04dace9075e",
"metadata": {},
"outputs": [],
"source": [
"round(534 / 20) * 20"
]
},
{
"cell_type": "markdown",
"id": "728b78b0-9ba8-4f1a-8734-18f94215406c",
"metadata": {},
"source": [
"We can write a python `function` to repeat this bit of code"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1e79c300-3831-46c9-94c7-22ae7ecac915",
"metadata": {},
"outputs": [],
"source": [
"def round_to_nearest_value(number, value):\n",
" return round(number / value) * value"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9385d6a3-a2b8-4173-8ab3-e6e271516842",
"metadata": {},
"outputs": [],
"source": [
"round_to_nearest_value(534, 20)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "17950e54-b73e-4d4a-8d24-7d4758021bc0",
"metadata": {},
"outputs": [],
"source": [
"round_to_nearest_value(534, 100)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f4c643fd-5acc-440b-91ba-1fdae0df693e",
"metadata": {},
"outputs": [],
"source": [
"round_to_nearest_value(534, 1000)"
]
},
{
"cell_type": "markdown",
"id": "0d423e57-c2cb-4aaf-923f-32e95123bea2",
"metadata": {},
"source": [
"Questions:\n",
"\n",
"- What do you think `round(534 / 10)` does? \n",
"- What do you think `round(534 / 10) * 10` does?\n",
"- Can you explain why this works?"
]
},
{
"cell_type": "markdown",
"id": "0bac418d-e8e5-41e5-9303-5db2d937c299",
"metadata": {},
"source": [
"#### Self work questions:\n",
"\n",
"Either by hand or using `round_to_nearest_value` (or both!) answer the following questions:\n",
"\n",
"1. Round 15 to the nearest 2.\n",
"2. Round 640 to the nearest 5.\n",
"3. Round 4,002,351 to the nearest 1,000.\n",
"4. Round 321 to the nearest $\\pi$."
]
},
{
"cell_type": "markdown",
"id": "e774aa27-cfe8-4e3a-93c1-7b1c8f9a9f0f",
"metadata": {},
"source": [
"### Comparing and ordering\n",
"\n",
"- Grade 4: \"_know how to compare and order whole numbers and develop an understanding of relative value of numbers_\"\n",
"- Grade 5: \"_know how to compare and order whole numbers and develop an understanding of relative value of numbers_\"\n",
"- Grade 6: \"_know how to compare and order whole numbers and develop an understanding of relative value of numbers_\"\n",
"- Grade 7: \"_Know how to compare and round numbers within the range 1 to 1,000,000_\""
]
},
{
"cell_type": "markdown",
"id": "71096f1f-4a85-4c64-bd7d-35bb571b27c6",
"metadata": {},
"source": [
"#### Exercise\n",
"\n",
"---\n",
"\n",
"Arrange the following numbers 5, 6, 3, 2, 10, 12, 1 in ascending and then descending order.\n",
"\n",
"---\n",
"\n",
"**Do this by hand and write down your count.**\n",
"\n",
"Let us use `sorted` do this."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4f6a9027-34ad-47da-b443-25a42c13ef31",
"metadata": {},
"outputs": [],
"source": [
"numbers = 5, 6, 3, 2, 10, 12, 1\n",
"sorted(numbers)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2a1e2772-49a0-492a-b7fd-2c6a34762597",
"metadata": {},
"outputs": [],
"source": [
"sorted(numbers, reverse=True)"
]
},
{
"cell_type": "markdown",
"id": "8ec8ffa6-6c8d-40e1-be1a-d53b6d1dcd9c",
"metadata": {},
"source": [
"Questions:\n",
"\n",
"- What do you think the role of the brackets `[` and `]` are?\n",
"- What do you think `reverse=True` does?"
]
},
{
"cell_type": "markdown",
"id": "1c74baed-ca7a-4f80-8eea-dede10d52cfd",
"metadata": {},
"source": [
"#### Self work questions:\n",
"\n",
"Either by hand or using `sorted` (or both!) answer the following questions:\n",
"\n",
"Sort the following numbers in ascending and then in descending order:\n",
" 1. 4, 5, 8, 10, 900102, 201902, 1782, 19204\n",
" 2. 49, 85, 12, 80, 46\n",
" 3. 78, 31, 35, 79, 8, 38, 7, 68, 65, 63, 81, 58, 73, 70, 65, 33, 91\n",
" 4. 5242, 4510, 8088, 2359, 6524, 452, 1185, 8543, 4234, 4529\n",
" 5. 40974, 60417, 43715, 5354, 61705, 32294, 35460, 89366, 7067"
]
},
{
"cell_type": "markdown",
"id": "906d7a32-de97-476c-a7ea-8bcd9fc59017",
"metadata": {},
"source": [
"### Number patterns\n",
"\n",
"- Grade 4: \"_will know how to create, continue and describe number patterns_\"\n",
"- Grade 5: \"_understand that number patterns are built on constant relationships between the terms in a sequence_\"\n",
"\n",
"#### Exercise\n",
"\n",
"---\n",
"\n",
"Obtain the next 3 terms of the following sequence 1, 10, 19, 28, 37.\n",
"\n",
"---\n",
"\n",
"**Do this by hand and write down your count.**\n",
"\n",
"Let us use `yield` to do this. Note that we still have to understand the sequence ourselves!"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2c96e671-d00f-4538-8a63-c16c068d9413",
"metadata": {},
"outputs": [],
"source": [
"def instructions():\n",
" number = 37\n",
" while True:\n",
" number = number + 9\n",
" yield number\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e0a6e9f9-e6a9-4f1c-8db3-3706129f8227",
"metadata": {},
"outputs": [],
"source": [
"numbers = instructions()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "dd079de7-154a-4e01-808b-e4d3423680d9",
"metadata": {},
"outputs": [],
"source": [
"next(numbers)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6496a7e0-437d-4ac8-b394-f17e0e443779",
"metadata": {},
"outputs": [],
"source": [
"next(numbers)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7cee3e20-bf82-42b0-8865-a6a00a821fc2",
"metadata": {},
"outputs": [],
"source": [
"next(numbers)"
]
},
{
"cell_type": "markdown",
"id": "9090209d-8ebd-4a3b-8f19-45147c5edbc5",
"metadata": {},
"source": [
"### 2 Computation\n",
"\n",
"#### Fraction representation, conversion and calculation\n",
"\n",
"- Grade 4: \"_model and represent fractions using fraction terminology and notation_\"\n",
"- Grade 5: \"_perform basic calculations with simple fractions_\"\n",
"- Grade 6: \"_determine suitable operations and strategies to solve context word problems_\"\n",
"- Grade 7: \"_apply their knowledge of fractions to solve context word problems_\"\n",
"\n",
"#### Exercise\n",
"\n",
"---\n",
"\n",
"Two classes of learners both have a cake. In one class the cake is cut in to 18 pieces and 8 of them are eaten. In another class the cake is cut in to 24 pieces and 12 of them are eaten.\n",
"\n",
"- Which class has eaten the most cake?\n",
"- How much of the total cake has been eaten?\n",
"\n",
"---\n",
"\n",
"**Do this by hand and write down your answer.**\n",
"\n",
"Let us use `fractions` to do this."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9b8c4021-1ae3-40d5-9b1f-2a692695edb7",
"metadata": {},
"outputs": [],
"source": [
"import fractions\n",
"cake_eaten_in_first_class = fractions.Fraction(8, 18)\n",
"print(cake_eaten_in_first_class)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "68425bf5-2abf-4fa3-8860-14dd3bea2e45",
"metadata": {},
"outputs": [],
"source": [
"cake_eaten_in_second_class = fractions.Fraction(12, 24)\n",
"print(cake_eaten_in_second_class)"
]
},
{
"cell_type": "markdown",
"id": "c6ce56c9-b85f-46a0-9b23-a713756a4220",
"metadata": {},
"source": [
"We can use `sorted` to order these amounts in ascending order:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5c76d425-5806-4f90-9597-72c7d8074876",
"metadata": {},
"outputs": [],
"source": [
"sorted((cake_eaten_in_first_class, cake_eaten_in_second_class))"
]
},
{
"cell_type": "markdown",
"id": "1b447a9a-3ebb-46bd-bd57-49a4d15b648d",
"metadata": {},
"source": [
"To obtain the total amount of cake we can add these fractions together:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a5aab978-8f25-44ce-920b-71dcd869ac25",
"metadata": {},
"outputs": [],
"source": [
"print(cake_eaten_in_first_class + cake_eaten_in_second_class)"
]
},
{
"cell_type": "markdown",
"id": "8dd2fd5c-6bd2-43d5-bc96-7ccd672fb433",
"metadata": {},
"source": [
"Questions:\n",
"\n",
"- What `fractions.Fraction(12, 24)` do?\n",
"\n",
"#### Self work questions:\n",
"\n",
"Either by hand or using `fractions.Fraction` (or both!) answer the following questions:\n",
"\n",
"1. What is bigger $\\frac{3}{5}$ or $\\frac{15}{27}$.\n",
"2. What is $\\frac{3}{5} + \\frac{15}{27}$.\n",
"3. If a farmer feeds $\\frac{12}{40}$ of their animals on one day and $\\frac{25}{40}$ of their animals on the next day, how many of their animals have been fed?"
]
},
{
"cell_type": "markdown",
"id": "744a05ea-82a1-4195-91c0-577551ca31e5",
"metadata": {},
"source": [
"## 3 Data Handling\n",
"\n",
"### Collect and represent data\n",
"\n",
"- Grade 4: \"_develop an understanding of collecting, organizing, representing and interpreting data_\"\n",
"- Grade 5: \"_know simple data collection and recording strategies_\"\n",
"- Grade 6: \"_know how to represent data on a variety of bar graphs_\"\n",
"- Grade 7: \"_ know how to design and use data collection instruments and data recording and representation strategies_\"\n",
"\n",
"#### Exercise\n",
"\n",
"---\n",
"\n",
"Some students have collected the following information asking students where home was for them. At each stage they wrote down the place:\n",
"\n",
"```\n",
"Omaruru\n",
"Swakopmund\n",
"Omaruru\n",
"Outjo\n",
"Omaruru\n",
"Windhoek\n",
"Walvis Bay\n",
"Swakopmund\n",
"Windhoek\n",
"Henties Bay\n",
"Omaruru\n",
"Otjiwarongo\n",
"Outjo\n",
"Windhoek\n",
"Swakopmund\n",
"Grootfontein\n",
"Swakopmund\n",
"Windhoek\n",
"Windhoek\n",
"Okahandja\n",
"Mariental\n",
"Mariental\n",
"Grootfontein\n",
"Mariental\n",
"Swakopmund\n",
"Outjo\n",
"Gobabis\n",
"Tsumeb\n",
"Mariental\n",
"Walvis Bay\n",
"Otjiwarongo\n",
"Otjiwarongo\n",
"```\n",
"\n",
"Represent this data.\n",
"\n",
"---\n",
"\n",
"**Do this by hand and write down your answer.**\n",
"\n",
"Let us use `collections.Counter` to do this."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6e3bd06e-4a2e-49e3-b24c-065027bbddc5",
"metadata": {},
"outputs": [],
"source": [
"import collections\n",
"\n",
"places = (\n",
" \"Omaruru\",\n",
" \"Swakopmund\",\n",
" \"Omaruru\",\n",
" \"Outjo\",\n",
" \"Omaruru\",\n",
" \"Windhoek\",\n",
" \"Walvis Bay\",\n",
" \"Swakopmund\",\n",
" \"Windhoek\",\n",
" \"Henties Bay\",\n",
" \"Omaruru\",\n",
" \"Otjiwarongo\",\n",
" \"Outjo\",\n",
" \"Windhoek\",\n",
" \"Swakopmund\",\n",
" \"Grootfontein\",\n",
" \"Swakopmund\",\n",
" \"Windhoek\",\n",
" \"Windhoek\",\n",
" \"Okahandja\",\n",
" \"Mariental\",\n",
" \"Mariental\",\n",
" \"Grootfontein\",\n",
" \"Mariental\",\n",
" \"Swakopmund\",\n",
" \"Outjo\",\n",
" \"Gobabis\",\n",
" \"Tsumeb\",\n",
" \"Mariental\",\n",
" \"Walvis Bay\",\n",
" \"Otjiwarongo\",\n",
" \"Otjiwarongo\",\n",
")\n",
"\n",
"counts = collections.Counter(places)\n",
"counts"
]
},
{
"cell_type": "markdown",
"id": "0513980b-ae50-4663-93a5-a19224a8823b",
"metadata": {},
"source": [
"with `matplotlib` we can also plot this:"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2541498b-5526-4d02-afb9-e9306b5f53ba",
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"\n",
"plt.figure()\n",
"plt.bar(counts.keys(), counts.values())\n",
"plt.xticks(rotation=90);"
]
},
{
"cell_type": "markdown",
"id": "0fbd8a55-14d9-47d1-b286-20753a79ff56",
"metadata": {},
"source": [
"#### Project:\n",
"\n",
"Reproduce this work by asking everyone at the conference where they are from."
]
}
],
"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.11.4"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment