From 57a5c0bc19d4c582f55e381171226b4b9eb81d1f Mon Sep 17 00:00:00 2001 From: Cyy Date: Tue, 4 Nov 2025 15:19:28 -0500 Subject: [PATCH] wrong --- .../assignments/assignment_1_20251017.ipynb | 225 +++ 02_activities/assignments/assignment_2.ipynb | 186 +- .../my_activities/Class_Object_Method.ipynb | 130 ++ .../my_activities/List Examples.ipynb | 1718 +++++++++++++++++ .../my_activities/Loops examples.ipynb | 426 ++++ .../my_activities/Python_Lesson_04.ipynb | 190 ++ .../functions_with_multiple_parameters.ipynb | 307 +++ 02_activities/my_activities/numpy.ipynb | 525 +++++ 02_activities/my_activities/open file.ipynb | 119 ++ 02_activities/my_activities/provinces.txt | 1 + .../strings related examples.ipynb | 833 ++++++++ 11 files changed, 4641 insertions(+), 19 deletions(-) create mode 100644 02_activities/assignments/assignment_1_20251017.ipynb create mode 100644 02_activities/my_activities/Class_Object_Method.ipynb create mode 100644 02_activities/my_activities/List Examples.ipynb create mode 100644 02_activities/my_activities/Loops examples.ipynb create mode 100644 02_activities/my_activities/Python_Lesson_04.ipynb create mode 100644 02_activities/my_activities/functions_with_multiple_parameters.ipynb create mode 100644 02_activities/my_activities/numpy.ipynb create mode 100644 02_activities/my_activities/open file.ipynb create mode 100644 02_activities/my_activities/provinces.txt create mode 100644 02_activities/my_activities/strings related examples.ipynb diff --git a/02_activities/assignments/assignment_1_20251017.ipynb b/02_activities/assignments/assignment_1_20251017.ipynb new file mode 100644 index 000000000..01d6a2a9d --- /dev/null +++ b/02_activities/assignments/assignment_1_20251017.ipynb @@ -0,0 +1,225 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Assignment #1: Anagram Checker\n", + "\n", + "**Background**: Anagram Checker is a program that takes two words and determines if an anagram can be made from it. If so, the program will return `true`, otherwise `false`." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Submission Information\n", + "\n", + "🚨 **Please review our [Assignment Submission Guide](https://github.com/UofT-DSI/onboarding/blob/main/onboarding_documents/submissions.md)** 🚨 for detailed instructions on how to format, branch, and submit your work. Following these guidelines is crucial for your submissions to be evaluated correctly.\n", + "\n", + "### Submission Parameters:\n", + "* Submission Due Date: `11:59 PM - October 20, 2025`\n", + "* The branch name for your repo should be: `assignment-1`\n", + "* What to submit for this assignment:\n", + " * This Jupyter Notebook (assignment_1.ipynb) should be populated and should be the only change in your pull request.\n", + "* What the pull request link should look like for this assignment: `https://github.com//python/pull/`\n", + " * Open a private window in your browser. Copy and paste the link to your pull request into the address bar. Make sure you can see your pull request properly. This helps the technical facilitator and learning support staff review your submission easily.\n", + "\n", + "Checklist:\n", + "- [ ] Created a branch with the correct naming convention.\n", + "- [ ] Ensured that the repository is public.\n", + "- [ ] Reviewed the PR description guidelines and adhered to them.\n", + "- [ ] Verify that the link is accessible in a private browser window.\n", + "\n", + "If you encounter any difficulties or have questions, please don't hesitate to reach out to our team via our Slack at `#dc-help`. Our Technical Facilitators and Learning Support staff are here to help you navigate any challenges." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Part 1: Building the base Anagram Checker\n", + "\n", + "Given two valid strings, check to see if they are anagrams of each other. If it is, return `True`, else `False`. For this part, we can assume that uppercase letters are the same as if it was a lowercase character.\n", + "\n", + "Examples of anagrams:\n", + "* Silent and Listen\n", + "* Night and Think\n", + "\n", + "Example outputs:\n", + "```python\n", + "anagram_checker(\"Silent\", \"listen\") # True\n", + "anagram_checker(\"Silent\", \"Night\") # False\n", + "anagram_checker(\"night\", \"Thing\") # True\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# For testing purposes, we will write our code in the function\n", + "def anagram_checker(word_a, word_b):\n", + " # Your code here\n", + " WORD_A = word_a.upper()\n", + " WORD_B = word_b.upper()\n", + " if sorted(WORD_A) == sorted(WORD_B):\n", + " return True\n", + " else:\n", + " return False\n", + "# Run your code to check using the words below:\n", + "anagram_checker(\"Silent\", \"listen\")" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "anagram_checker(\"Silent\", \"Night\")" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "anagram_checker(\"night\", \"Thing\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Part 2: Expanding the functionality of the Anagram Checker\n", + "\n", + "Using your existing and functional anagram checker, let's add a boolean option called `is_case_sensitive`, which will return `True` or `False` based on if the two compared words are anagrams and if we are checking for case sensitivity." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def anagram_checker(word_a, word_b, is_case_sensitive):\n", + " # Modify your existing code here\n", + " result = sorted(word_a) == sorted(word_b)\n", + "\n", + " if result == is_case_sensitive:\n", + " return True\n", + " else: \n", + " return False\n", + "# Run your code to check using the words below:\n", + "anagram_checker(\"Silent\", \"listen\", False) # True" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "I can't store the True/False return in a variable. (Like the if in Part 1)\n", + "However, I can store the output of the comparison (sorted(word_a) == sorted(word_b)) as a variable, and compare the variable with another one" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "anagram_checker(\"Silent\", \"Listen\", True) # False" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "|Criteria|Pass|Fail|\n", + "|---|---|---|\n", + "|Code Execution|All code cells execute without errors.|Any code cell produces an error upon execution.|\n", + "|Code Quality|Code is well-organized, concise, and includes necessary comments for clarity. E.g. Great use of variable names.|Code is unorganized, verbose, or lacks necessary comments. E.g. Single character variable names outside of loops.|" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "python-env", + "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": 2 +} diff --git a/02_activities/assignments/assignment_2.ipynb b/02_activities/assignments/assignment_2.ipynb index 36a3e2bb7..446af4c5d 100644 --- a/02_activities/assignments/assignment_2.ipynb +++ b/02_activities/assignments/assignment_2.ipynb @@ -72,11 +72,138 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 13, "metadata": { "id": "n0m48JsS-nMC" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0,0,1,3,1,2,4,7,8,3,3,3,10,5,7,4,7,7,12,18,6,13,11,11,7,7,4,6,8,8,4,4,5,7,3,4,2,3,0,0\n", + "\n", + "0,1,2,1,2,1,3,2,2,6,10,11,5,9,4,4,7,16,8,6,18,4,12,5,12,7,11,5,11,3,3,5,4,4,5,5,1,1,0,1\n", + "\n", + "0,1,1,3,3,2,6,2,5,9,5,7,4,5,4,15,5,11,9,10,19,14,12,17,7,12,11,7,4,2,10,5,4,2,2,3,2,2,1,1\n", + "\n", + "0,0,2,0,4,2,2,1,6,7,10,7,9,13,8,8,15,10,10,7,17,4,4,7,6,15,6,4,9,11,3,5,6,3,3,4,2,3,2,1\n", + "\n", + "0,1,1,3,3,1,3,5,2,4,4,7,6,5,3,10,8,10,6,17,9,14,9,7,13,9,12,6,7,7,9,6,3,2,2,4,2,0,1,1\n", + "\n", + "0,0,1,2,2,4,2,1,6,4,7,6,6,9,9,15,4,16,18,12,12,5,18,9,5,3,10,3,12,7,8,4,7,3,5,4,4,3,2,1\n", + "\n", + "0,0,2,2,4,2,2,5,5,8,6,5,11,9,4,13,5,12,10,6,9,17,15,8,9,3,13,7,8,2,8,8,4,2,3,5,4,1,1,1\n", + "\n", + "0,0,1,2,3,1,2,3,5,3,7,8,8,5,10,9,15,11,18,19,20,8,5,13,15,10,6,10,6,7,4,9,3,5,2,5,3,2,2,1\n", + "\n", + "0,0,0,3,1,5,6,5,5,8,2,4,11,12,10,11,9,10,17,11,6,16,12,6,8,14,6,13,10,11,4,6,4,7,6,3,2,1,0,0\n", + "\n", + "0,1,1,2,1,3,5,3,5,8,6,8,12,5,13,6,13,8,16,8,18,15,16,14,12,7,3,8,9,11,2,5,4,5,1,4,1,2,0,0\n", + "\n", + "0,1,0,0,4,3,3,5,5,4,5,8,7,10,13,3,7,13,15,18,8,15,15,16,11,14,12,4,10,10,4,3,4,5,5,3,3,2,2,1\n", + "\n", + "0,1,0,0,3,4,2,7,8,5,2,8,11,5,5,8,14,11,6,11,9,16,18,6,12,5,4,3,5,7,8,3,5,4,5,5,4,0,1,1\n", + "\n", + "0,0,2,1,4,3,6,4,6,7,9,9,3,11,6,12,4,17,13,15,13,12,8,7,4,7,12,9,5,6,5,4,7,3,5,4,2,3,0,1\n", + "\n", + "0,0,0,0,1,3,1,6,6,5,5,6,3,6,13,3,10,13,9,16,15,9,11,4,6,4,11,11,12,3,5,8,7,4,6,4,1,3,0,0\n", + "\n", + "0,1,2,1,1,1,4,1,5,2,3,3,10,7,13,5,7,17,6,9,12,13,10,4,12,4,6,7,6,10,8,2,5,1,3,4,2,0,2,0\n", + "\n", + "0,1,1,0,1,2,4,3,6,4,7,5,5,7,5,10,7,8,18,17,9,8,12,11,11,11,14,6,11,2,10,9,5,6,5,3,4,2,2,0\n", + "\n", + "0,0,0,0,2,3,6,5,7,4,3,2,10,7,9,11,12,5,12,9,13,19,14,17,5,13,8,11,5,10,9,8,7,5,3,1,4,0,2,1\n", + "\n", + "0,0,0,1,2,1,4,3,6,7,4,2,12,6,12,4,14,7,8,14,13,19,6,9,12,6,4,13,6,7,2,3,6,5,4,2,3,0,1,0\n", + "\n", + "0,0,2,1,2,5,4,2,7,8,4,7,11,9,8,11,15,17,11,12,7,12,7,6,7,4,13,5,7,6,6,9,2,1,1,2,2,0,1,0\n", + "\n", + "0,1,2,0,1,4,3,2,2,7,3,3,12,13,11,13,6,5,9,16,9,19,16,11,8,9,14,12,11,9,6,6,6,1,1,2,4,3,1,1\n", + "\n", + "0,1,1,3,1,4,4,1,8,2,2,3,12,12,10,15,13,6,5,5,18,19,9,6,11,12,7,6,3,6,3,2,4,3,1,5,4,2,2,0\n", + "\n", + "0,0,2,3,2,3,2,6,3,8,7,4,6,6,9,5,12,12,8,5,12,10,16,7,14,12,5,4,6,9,8,5,6,6,1,4,3,0,2,0\n", + "\n", + "0,0,0,3,4,5,1,7,7,8,2,5,12,4,10,14,5,5,17,13,16,15,13,6,12,9,10,3,3,7,4,4,8,2,6,5,1,0,1,0\n", + "\n", + "0,1,1,1,1,3,3,2,6,3,9,7,8,8,4,13,7,14,11,15,14,13,5,13,7,14,9,10,5,11,5,3,5,1,1,4,4,1,2,0\n", + "\n", + "0,1,1,1,2,3,5,3,6,3,7,10,3,8,12,4,12,9,15,5,17,16,5,10,10,15,7,5,3,11,5,5,6,1,1,1,1,0,2,1\n", + "\n", + "0,0,2,1,3,3,2,7,4,4,3,8,12,9,12,9,5,16,8,17,7,11,14,7,13,11,7,12,12,7,8,5,7,2,2,4,1,1,1,0\n", + "\n", + "0,0,1,2,4,2,2,3,5,7,10,5,5,12,3,13,4,13,7,15,9,12,18,14,16,12,3,11,3,2,7,4,8,2,2,1,3,0,1,1\n", + "\n", + "0,0,1,1,1,5,1,5,2,2,4,10,4,8,14,6,15,6,12,15,15,13,7,17,4,5,11,4,8,7,9,4,5,3,2,5,4,3,2,1\n", + "\n", + "0,0,2,2,3,4,6,3,7,6,4,5,8,4,7,7,6,11,12,19,20,18,9,5,4,7,14,8,4,3,7,7,8,3,5,4,1,3,1,0\n", + "\n", + "0,0,0,1,4,4,6,3,8,6,4,10,12,3,3,6,8,7,17,16,14,15,17,4,14,13,4,4,12,11,6,9,5,5,2,5,2,1,0,1\n", + "\n", + "0,1,1,0,3,2,4,6,8,6,2,3,11,3,14,14,12,8,8,16,13,7,6,9,15,7,6,4,10,8,10,4,2,6,5,5,2,3,2,1\n", + "\n", + "0,0,2,3,3,4,5,3,6,7,10,5,10,13,14,3,8,10,9,9,19,15,15,6,8,8,11,5,5,7,3,6,6,4,5,2,2,3,0,0\n", + "\n", + "0,1,2,2,2,3,6,6,6,7,6,3,11,12,13,15,15,10,14,11,11,8,6,12,10,5,12,7,7,11,5,8,5,2,5,5,2,0,2,1\n", + "\n", + "0,0,2,1,3,5,6,7,5,8,9,3,12,10,12,4,12,9,13,10,10,6,10,11,4,15,13,7,3,4,2,9,7,2,4,2,1,2,1,1\n", + "\n", + "0,0,1,2,4,1,5,5,2,3,4,8,8,12,5,15,9,17,7,19,14,18,12,17,14,4,13,13,8,11,5,6,6,2,3,5,2,1,1,1\n", + "\n", + "0,0,0,3,1,3,6,4,3,4,8,3,4,8,3,11,5,7,10,5,15,9,16,17,16,3,8,9,8,3,3,9,5,1,6,5,4,2,2,0\n", + "\n", + "0,1,2,2,2,5,5,1,4,6,3,6,5,9,6,7,4,7,16,7,16,13,9,16,12,6,7,9,10,3,6,4,5,4,6,3,4,3,2,1\n", + "\n", + "0,1,1,2,3,1,5,1,2,2,5,7,6,6,5,10,6,7,17,13,15,16,17,14,4,4,10,10,10,11,9,9,5,4,4,2,1,0,1,0\n", + "\n", + "0,1,0,3,2,4,1,1,5,9,10,7,12,10,9,15,12,13,13,6,19,9,10,6,13,5,13,6,7,2,5,5,2,1,1,1,1,3,0,1\n", + "\n", + "0,1,1,3,1,1,5,5,3,7,2,2,3,12,4,6,8,15,16,16,15,4,14,5,13,10,7,10,6,3,2,3,6,3,3,5,4,3,2,1\n", + "\n", + "0,0,0,2,2,1,3,4,5,5,6,5,5,12,13,5,7,5,11,15,18,7,9,10,14,12,11,9,10,3,2,9,6,2,2,5,3,0,0,1\n", + "\n", + "0,0,1,3,3,1,2,1,8,9,2,8,10,3,8,6,10,13,11,17,19,6,4,11,6,12,7,5,5,4,4,8,2,6,6,4,2,2,0,0\n", + "\n", + "0,1,1,3,4,5,2,1,3,7,9,6,10,5,8,15,11,12,15,6,12,16,6,4,14,3,12,9,6,11,5,8,5,5,6,1,2,1,2,0\n", + "\n", + "0,0,1,3,1,4,3,6,7,8,5,7,11,3,6,11,6,10,6,19,18,14,6,10,7,9,8,5,8,3,10,2,5,1,5,4,2,1,0,1\n", + "\n", + "0,1,1,3,3,4,4,6,3,4,9,9,7,6,8,15,12,15,6,11,6,18,5,14,15,12,9,8,3,6,10,6,8,7,2,5,4,3,1,1\n", + "\n", + "0,1,2,2,4,3,1,4,8,9,5,10,10,3,4,6,7,11,16,6,14,9,11,10,10,7,10,8,8,4,5,8,4,4,5,2,4,1,1,0\n", + "\n", + "0,0,2,3,4,5,4,6,2,9,7,4,9,10,8,11,16,12,15,17,19,10,18,13,15,11,8,4,7,11,6,7,6,5,1,3,1,0,0,0\n", + "\n", + "0,1,1,3,1,4,6,2,8,2,10,3,11,9,13,15,5,15,6,10,10,5,14,15,12,7,4,5,11,4,6,9,5,6,1,1,2,1,2,1\n", + "\n", + "0,0,1,3,2,5,1,2,7,6,6,3,12,9,4,14,4,6,12,9,12,7,11,7,16,8,13,6,7,6,10,7,6,3,1,5,4,3,0,0\n", + "\n", + "0,0,1,2,3,4,5,7,5,4,10,5,12,12,5,4,7,9,18,16,16,10,15,15,10,4,3,7,5,9,4,6,2,4,1,4,2,2,2,1\n", + "\n", + "0,1,2,1,1,3,5,3,6,3,10,10,11,10,13,10,13,6,6,14,5,4,5,5,9,4,12,7,7,4,7,9,3,3,6,3,4,1,2,0\n", + "\n", + "0,1,2,2,3,5,2,4,5,6,8,3,5,4,3,15,15,12,16,7,20,15,12,8,9,6,12,5,8,3,8,5,4,1,3,2,1,3,1,0\n", + "\n", + "0,0,0,2,4,4,5,3,3,3,10,4,4,4,14,11,15,13,10,14,11,17,9,11,11,7,10,12,10,10,10,8,7,5,2,2,4,1,2,1\n", + "\n", + "0,0,2,1,1,4,4,7,2,9,4,10,12,7,6,6,11,12,9,15,15,6,6,13,5,12,9,6,4,7,7,6,5,4,1,4,2,2,2,1\n", + "\n", + "0,1,2,1,1,4,5,4,4,5,9,7,10,3,13,13,8,9,17,16,16,15,12,13,5,12,10,9,11,9,4,5,5,2,2,5,1,0,0,1\n", + "\n", + "0,0,1,3,2,3,6,4,5,7,2,4,11,11,3,8,8,16,5,13,16,5,8,8,6,9,10,10,9,3,3,5,3,5,4,5,3,3,0,1\n", + "\n", + "0,1,1,2,2,5,1,7,4,2,5,5,4,6,6,4,16,11,14,16,14,14,8,17,4,14,13,7,6,3,7,7,5,6,3,4,2,2,1,1\n", + "\n", + "0,1,1,1,4,1,6,4,6,3,6,5,6,4,14,13,13,9,12,19,9,10,15,10,9,10,10,7,5,6,8,6,6,4,3,5,2,1,1,1\n", + "\n", + "0,0,0,1,4,5,6,3,8,7,9,10,8,6,5,12,15,5,10,5,8,13,18,17,14,9,13,4,10,11,10,8,8,6,5,5,2,0,2,0\n", + "\n", + "0,0,1,0,3,2,5,4,8,2,9,3,3,10,12,9,14,11,13,8,6,18,11,9,13,11,8,5,5,2,8,5,3,5,4,1,3,1,1,0\n", + "\n" + ] + } + ], "source": [ "all_paths = [\n", " \"../../05_src/data/assignment_2_data/inflammation_01.csv\",\n", @@ -93,10 +220,12 @@ " \"../../05_src/data/assignment_2_data/inflammation_12.csv\"\n", "]\n", "\n", - "with open(all_paths[0], 'r') as f:\n", + "with open(all_paths[0], 'r') as f: #just the 1st file - inflammation_01\n", " # YOUR CODE HERE: Use the readline() or readlines() method to read the .csv file into a variable\n", - " \n", - " # YOUR CODE HERE: Iterate through the variable using a for loop and print each row for inspection" + " variable = f.readlines()\n", + " # YOUR CODE HERE: Iterate through the variable using a for loop and print each row for inspection\n", + " for i in variable:\n", + " print(i)" ] }, { @@ -130,7 +259,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": { "id": "82-bk4CBB1w4" }, @@ -145,13 +274,13 @@ " # Implement the specific operation based on the 'operation' argument\n", " if operation == 'mean':\n", " # YOUR CODE HERE: Calculate the mean (average) number of flare-ups for each patient\n", - "\n", + " return np.mean(data, axis=ax)\n", " elif operation == 'max':\n", " # YOUR CODE HERE: Calculate the maximum number of flare-ups experienced by each patient\n", - "\n", + " return np.max(data, axis=ax)\n", " elif operation == 'min':\n", " # YOUR CODE HERE: Calculate the minimum number of flare-ups experienced by each patient\n", - "\n", + " return np.min(data, axis=ax)\n", " else:\n", " # If the operation is not one of the expected values, raise an error\n", " raise ValueError(\"Invalid operation. Please choose 'mean', 'max', or 'min'.\")\n", @@ -161,11 +290,19 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 15, "metadata": { "id": "3TYo0-1SDLrd" }, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "60\n" + ] + } + ], "source": [ "# Test it out on the data file we read in and make sure the size is what we expect i.e., 60\n", "# Your output for the first file should be 60\n", @@ -228,7 +365,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "metadata": { "id": "_svDiRkdIwiT" }, @@ -251,7 +388,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 18, "metadata": { "id": "LEYPM5v4JT0i" }, @@ -261,15 +398,25 @@ "\n", "def detect_problems(file_path):\n", " #YOUR CODE HERE: Use patient_summary() to get the means and check_zeros() to check for zeros in the means\n", - "\n", - " return" + " mean_of_each_patient = patient_summary(file_path, 'mean')\n", + " problematic = check_zeros(mean_of_each_patient)\n", + " return problematic\n", + " " ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n" + ] + } + ], "source": [ "# Test out your code here\n", "# Your output for the first file should be False\n", @@ -314,7 +461,8 @@ "provenance": [] }, "kernelspec": { - "display_name": "Python 3", + "display_name": "python-env", + "language": "python", "name": "python3" }, "language_info": { @@ -327,7 +475,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.8" + "version": "3.11.4" } }, "nbformat": 4, diff --git a/02_activities/my_activities/Class_Object_Method.ipynb b/02_activities/my_activities/Class_Object_Method.ipynb new file mode 100644 index 000000000..5d85b2209 --- /dev/null +++ b/02_activities/my_activities/Class_Object_Method.ipynb @@ -0,0 +1,130 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "8a211d16", + "metadata": {}, + "outputs": [], + "source": [ + "class Car:\n", + " def __init__(self, name, make, model, vin): #defining the properties of a car\n", + " self.name = name\n", + " self.make = make\n", + " self.model = model\n", + " self.vin = vin\n", + " \n", + " def honk(self): #defining a method/ something that a car(that class) can do\n", + " print(\"beeeeep!!!\")" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "afae8c72", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'1234587MA'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Cyy_car = Car(\"Goodboy\", \"Dodge\", \"GrandCaravan\", \"1234587MA\")\n", + "Jan_car = Car(\"Goodgirl\", \"Kia\", \"Seltos\", \"KN1297U1234\")\n", + "\n", + "Cyy_car.vin" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "119dffaa", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "beeeeep!!!\n" + ] + } + ], + "source": [ + "Cyy_car.honk()" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "6b468009", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Hello, I am Cyy\n" + ] + } + ], + "source": [ + "#We usually create a class name with title upper case, though not a must\n", + "# __init__ is a special method in Python classes\n", + "# stands for initialize, automatically called when a new object of a class is created\n", + "# to initialize the object's attributes or properties\n", + "\n", + "# self is a reference to the current instance of the class\n", + "# always the first parameter in every method definition in a class\n", + "\n", + "class Person:\n", + " def __init__(self, name, age):\n", + " self.name = name\n", + " self.age = age\n", + " \n", + " def greet(self):\n", + " print(f\"Hello, I am {self.name}\")\n", + "\n", + "id01 = Person(\"Cyy\", 36)\n", + "\n", + "id01.name\n", + "id01.greet()\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "0c19a764", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "python-env", + "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 +} diff --git a/02_activities/my_activities/List Examples.ipynb b/02_activities/my_activities/List Examples.ipynb new file mode 100644 index 000000000..08fa383b0 --- /dev/null +++ b/02_activities/my_activities/List Examples.ipynb @@ -0,0 +1,1718 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 25, + "id": "cadcd1ab", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "We are in the 21st century\n" + ] + } + ], + "source": [ + "year = 2022\n", + "\n", + "if year >= 2000:\n", + " print(\"We are in the 21st century\")" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "3f55b1e2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "SATURADAY\n", + "I am happy\n" + ] + } + ], + "source": [ + "today = \"saturaday\"\n", + "upper_today = today.upper()\n", + "print(upper_today)\n", + "\n", + "if upper_today == \"SUNDAY\" or \"SATURADAY\":\n", + " print (\"I am happy\")\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "75e96002", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "It's too early to have the test again\n" + ] + } + ], + "source": [ + "def eye_exam(age, time_since_last_exam, condition):\n", + " '''age is an integer, qualifying condition is bool -> True (you qualify), time_since_last -> # month'''\n", + " if age <= 19:\n", + " if time_since_last_exam >=12:\n", + " print(\"You are eligible for 1 major eye exam and 2 minor assessments\")\n", + " else:\n", + " print(\"It's too early to have the test again\")\n", + " \n", + " elif 20<= age <= 64:\n", + " if condition == True:\n", + " if time_since_last_exam >=12:\n", + " print(\"You are eligible for 1 major eye exam and 2 minor assessments\")\n", + " else:\n", + " print(\"It's too early to have the test again\")\n", + "\n", + "\n", + "child_01 = eye_exam(21, 12, False)\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "e6cdf790", + "metadata": {}, + "source": [ + "This piece of code is a little bit weird." + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "57d193c2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['a', 'e', 'i', 'o', 'u']" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "vowels = ['a','e','i','o','u']\n", + "vowels" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "55fea22f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[]" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "empty = list()\n", + "empty" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "a02a2b6c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[['dimsum', 'Chinese'], ['pizza', 'cola', 'Local'], 'bubble tea']" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "List_of_food = [\n", + " ['dimsum', 'Chinese'],\n", + " ['pizza','cola','Local' ],\n", + " 'bubble tea'\n", + "]\n", + "List_of_food" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "6ae6efd6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['pizza', 'cola', 'Local']" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "List_of_food[1]" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "af73d523", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Local'" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "List_of_food[1][2]" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "602393bc", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(List_of_food)" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "e4a3083a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[['dimsum', 'Chinese'], ['pizza', 'cola', 'Local'], 'bubble tea']" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "List_of_food[:]" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "id": "7c37ae06", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[['dimsum', 'Chinese'],\n", + " ['pizza', 'cola', 'Local'],\n", + " 'e',\n", + " 'n',\n", + " 'g',\n", + " 'l',\n", + " 'i',\n", + " 's',\n", + " 'h']" + ] + }, + "execution_count": 35, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "List_of_food[2:] = \"english\"\n", + "List_of_food" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "27851940", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "'<' not supported between instances of 'str' and 'list'", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mTypeError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[36]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[38;5;28;43msorted\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mList_of_food\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[31mTypeError\u001b[39m: '<' not supported between instances of 'str' and 'list'" + ] + } + ], + "source": [ + "sorted(List_of_food)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3ac3865b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[1, 4, 9, 16, 25, 36, 49]" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "perfect_squares = [1,4,9,16,25,36,49]\n", + "perfect_squares" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6c8ccef4", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "7" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(perfect_squares)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e3e91431", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "49" + ] + }, + "execution_count": 42, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "max(perfect_squares)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dc94fd83", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "min(perfect_squares)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2cc15f97", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "140" + ] + }, + "execution_count": 44, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sum(perfect_squares)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "291d24ce", + "metadata": {}, + "outputs": [], + "source": [ + "sq_root = [str(i**0.5) for i in perfect_squares]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "35a4da62", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['1.0', '2.0', '3.0', '4.0', '5.0', '6.0', '7.0']" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sq_root" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7b5ee727", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['red', 'orange', '黃', 'green', 'blue', 'purple']" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# .extend() method is used to add elements from an iterable to the end of a list\n", + "# it unpacks the elements of the iterable and adds them one by one to the end of the origainl list\n", + "colour = ['red','orange','黃','green','blue','purple']\n", + "colour" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7d236027", + "metadata": {}, + "outputs": [], + "source": [ + "colour.append([\"orange2\",'orange3']) # append just add without changing anything" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c2dbfc32", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['red', 'orange', '黃', 'green', 'blue', 'purple', ['orange2', 'orange3']]" + ] + }, + "execution_count": 59, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "colour" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ef503b7b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['red',\n", + " 'orange',\n", + " '黃',\n", + " 'green',\n", + " 'blue',\n", + " 'purple',\n", + " ['orange2', 'orange3'],\n", + " \"blue2','blue3\"]" + ] + }, + "execution_count": 60, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "colour.extend([\"blue2','blue3\"]) # now blue2 and blue3 are added as separated strings but not a list\n", + "colour" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "7f3423fd", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "8" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(colour)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4a208eba", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['red',\n", + " 'orange',\n", + " '黃',\n", + " 'green',\n", + " 'blue',\n", + " 'purple',\n", + " ['orange2', 'orange3'],\n", + " \"blue2','blue3\",\n", + " 'c',\n", + " 'y',\n", + " 'a',\n", + " 'n']" + ] + }, + "execution_count": 62, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "colour.extend(\"cyan\") # now breakdown every single letter from cyan to add to the list\n", + "colour" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "31c7aa32", + "metadata": {}, + "outputs": [], + "source": [ + "new_colour = colour.append(\"hap\")\n", + "new_colour" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6c7d0ccf", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['red',\n", + " 'orange',\n", + " '黃',\n", + " 'green',\n", + " 'blue',\n", + " 'purple',\n", + " ['orange2', 'orange3'],\n", + " \"blue2','blue3\",\n", + " 'c',\n", + " 'y',\n", + " 'a',\n", + " 'n',\n", + " 'happy',\n", + " 'happy',\n", + " 'hap']" + ] + }, + "execution_count": 70, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_colour_2 = [i for i in colour]\n", + "new_colour_2" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e8ba7305", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['red',\n", + " 'orange',\n", + " '黃',\n", + " 'green',\n", + " 'blue',\n", + " 'purple',\n", + " ['orange2', 'orange3'],\n", + " \"blue2','blue3\",\n", + " 'c',\n", + " 'y',\n", + " 'a',\n", + " 'n',\n", + " 'happy',\n", + " 'happy',\n", + " 'hap']" + ] + }, + "execution_count": 83, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "colour" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3c87a172", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['red',\n", + " 'orange',\n", + " '黃',\n", + " 'green',\n", + " 'blue',\n", + " 'purple',\n", + " ['orange2', 'orange3'],\n", + " \"blue2','blue3\",\n", + " 'c',\n", + " 'y',\n", + " 'a',\n", + " 'n']" + ] + }, + "execution_count": 122, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "del colour [-3:]\n", + "colour" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1a9a5653", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['red',\n", + " 'orange',\n", + " '黃',\n", + " 'green',\n", + " 'blue',\n", + " 'purple',\n", + " ['orange2', 'orange3'],\n", + " \"blue2','blue3\",\n", + " 'c',\n", + " 'y',\n", + " 'a',\n", + " 'n']" + ] + }, + "execution_count": 160, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "colour" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e0e3002c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['red',\n", + " 'orange',\n", + " '黃',\n", + " 'green',\n", + " 'blue',\n", + " 'purple',\n", + " ['orange2', 'orange3'],\n", + " \"blue2','blue3\",\n", + " 'c',\n", + " 'y',\n", + " 'a',\n", + " 'n',\n", + " 'fruit']" + ] + }, + "execution_count": 161, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "colour.append(\"fruit\")\n", + "colour" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d641e095", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['red',\n", + " 'orange',\n", + " '黃',\n", + " 'green',\n", + " 'blue',\n", + " 'purple',\n", + " ['orange2', 'orange3'],\n", + " \"blue2','blue3\",\n", + " 'c',\n", + " 'y',\n", + " 'a',\n", + " 'n',\n", + " 'fruit']" + ] + }, + "execution_count": 163, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_colour_3 = [i for i in colour]\n", + "new_colour_3" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "008628c5", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['orange2', 'orange3']" + ] + }, + "execution_count": 164, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "colour[6]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6df5da4f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'purple'" + ] + }, + "execution_count": 165, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "colour[5]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "fc36c390", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['red',\n", + " 'orange',\n", + " '黃',\n", + " 'green',\n", + " 'blue',\n", + " 'oo',\n", + " 'purple',\n", + " ['orange2', 'orange3'],\n", + " \"blue2','blue3\",\n", + " 'c',\n", + " 'y',\n", + " 'a',\n", + " 'n',\n", + " 'fruit']" + ] + }, + "execution_count": 167, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "colour.insert(5,\"oo\")\n", + "colour\n", + "# list_name.clear() --> the list will be cleared" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f8faaead", + "metadata": {}, + "outputs": [], + "source": [ + "characters = [1,2,3,'a','b','c']" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "695084da", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['a', 'b', 'c']" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "characters[3:]" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "id": "9ac1d887", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['apple', 'banana', 'cheerio']" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "breakfast = ['apple','banana','cheerio']\n", + "breakfast_copy = list(breakfast)\n", + "breakfast[0] = 'pizza'\n", + "breakfast\n", + "breakfast_copy" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "id": "6dd1fb81", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['pizza', 'banana', 'cheerio']" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "breakfast" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "id": "3f6407ea", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['banana', 'cheerio', 'pizza']" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "sorted(breakfast)" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "id": "f6895541", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['pizza', 'banana', 'cheerio']" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "breakfast" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "id": "d604358e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['pizza', 'banana', 'cheerio']" + ] + }, + "execution_count": 56, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def last_letter(text):\n", + " return text[-1]\n", + "\n", + "sorted(breakfast, key=last_letter)" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "id": "db2d3bf7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[['Grade 10', 17], ['Grade 11', 13], ['Grade 12', 22], ['Grade 9', 20]]" + ] + }, + "execution_count": 57, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "students_per_class = [[\"Grade 9\", 20],[\"Grade 11\", 13], [\"Grade 10\",17], [\"Grade 12\", 22]]\n", + "sorted(students_per_class)" + ] + }, + { + "cell_type": "code", + "execution_count": 59, + "id": "d261f631", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[['Grade 11', 13], ['Grade 10', 17], ['Grade 9', 20], ['Grade 12', 22]]" + ] + }, + "execution_count": 59, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def second_element(item):\n", + " return item[1]\n", + "\n", + "students_per_class.sort(key = second_element)\n", + "students_per_class" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "76552fe0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "second_element([0,5,7]) #inputting a list and use second_element to get the 2nd element of the list" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "id": "73e0855f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 62, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list = ()\n", + "type(list)" + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "id": "f86d816f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 64, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list_2 = []\n", + "type(list_2)" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "id": "683cd942", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 65, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list_3 = tuple()\n", + "type (list_3)" + ] + }, + { + "cell_type": "code", + "execution_count": 70, + "id": "3e32f364", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "can only concatenate tuple (not \"str\") to tuple", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mTypeError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[70]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43mlist_3\u001b[49m\u001b[43m \u001b[49m\u001b[43m+\u001b[49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mhello\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[31mTypeError\u001b[39m: can only concatenate tuple (not \"str\") to tuple" + ] + } + ], + "source": [ + "list_3 + (\"hello\")" + ] + }, + { + "cell_type": "code", + "execution_count": 71, + "id": "3d93c0cd", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "('hello', 'you')" + ] + }, + "execution_count": 71, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list_3 + (\"hello\", \"you\")" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "id": "16e91897", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[]" + ] + }, + "execution_count": 72, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list_2" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "id": "d0670093", + "metadata": {}, + "outputs": [], + "source": [ + "list_2.append(\"hello\")" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "id": "7c58dff7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['hello']" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list_2" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "id": "75fa6965", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "can only concatenate list (not \"str\") to list", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mTypeError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[75]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43mlist_2\u001b[49m\u001b[43m \u001b[49m\u001b[43m+\u001b[49m\u001b[43m \u001b[49m\u001b[43m(\u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43mhello\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n", + "\u001b[31mTypeError\u001b[39m: can only concatenate list (not \"str\") to list" + ] + } + ], + "source": [ + "list_2 + (\"hello\")" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "id": "a1d76420", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['hello', 'hello']" + ] + }, + "execution_count": 76, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list_2 + [\"hello\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "id": "70c4d5f8", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'apple', 'book', 'box', 'cat', 'coat', 'hair', 'id card', 'lock'}" + ] + }, + "execution_count": 77, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "things = {'coat', 'lock', 'box', 'book', 'apple','hair', 'id card', 'book', 'coat', 'cat'}\n", + "things" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "id": "5009e940", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "8" + ] + }, + "execution_count": 78, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(things)" + ] + }, + { + "cell_type": "code", + "execution_count": 89, + "id": "ba4ac9c7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 89, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(things)" + ] + }, + { + "cell_type": "code", + "execution_count": 82, + "id": "e06b08cb", + "metadata": {}, + "outputs": [ + { + "ename": "AttributeError", + "evalue": "'set' object has no attribute 'sort'", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mAttributeError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[82]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43mthings\u001b[49m\u001b[43m.\u001b[49m\u001b[43msort\u001b[49m()\n", + "\u001b[31mAttributeError\u001b[39m: 'set' object has no attribute 'sort'" + ] + } + ], + "source": [ + "things.sort()" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "id": "b6584a80", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 83, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(things)" + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "id": "529e8ee7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 84, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "postal_codes = [\"M2G\", \"M2G\", \"M1M\", \"M5G\"]\n", + "len(postal_codes)" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "id": "81407b7e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'M1M', 'M2G', 'M5G'}" + ] + }, + "execution_count": 87, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cleaned_postal_code = set(postal_codes)\n", + "cleaned_postal_code" + ] + }, + { + "cell_type": "code", + "execution_count": 88, + "id": "11b42506", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 88, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(cleaned_postal_code)" + ] + }, + { + "cell_type": "code", + "execution_count": 90, + "id": "8d9296ed", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict" + ] + }, + "execution_count": 90, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_item = { }\n", + "type(new_item)" + ] + }, + { + "cell_type": "code", + "execution_count": 91, + "id": "76b95a5a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "tuple" + ] + }, + "execution_count": 91, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_item_2 = ( )\n", + "type(new_item_2)" + ] + }, + { + "cell_type": "code", + "execution_count": 92, + "id": "c05dfd3a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set" + ] + }, + "execution_count": 92, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_item_3 = set( )\n", + "type(new_item_3)" + ] + }, + { + "cell_type": "code", + "execution_count": 97, + "id": "4140c0b2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'aurora', 'eg', 'newmarket'}" + ] + }, + "execution_count": 97, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "york_region = {\"newmarket\",'aurora','eg','rh','markham'}\n", + "north_york_region = {'newmarket', 'aurora','eg'}\n", + "north_york_region" + ] + }, + { + "cell_type": "code", + "execution_count": 98, + "id": "d25051e3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'markham', 'rh'}" + ] + }, + "execution_count": 98, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "york_region.difference(north_york_region)" + ] + }, + { + "cell_type": "code", + "execution_count": 99, + "id": "11e518b9", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set()" + ] + }, + "execution_count": 99, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "north_york_region.difference(york_region)" + ] + }, + { + "cell_type": "code", + "execution_count": 118, + "id": "d572ee57", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "dict" + ] + }, + "execution_count": 118, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "capitals = {\"Canada\":\"Ottawa\", \"Mexico\":\"Mexico City\", \"Canada\":\"Happy\"}\n", + "type(capitals)" + ] + }, + { + "cell_type": "code", + "execution_count": 119, + "id": "d9e50046", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Happy'" + ] + }, + "execution_count": 119, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "capitals[\"Canada\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 120, + "id": "79bf1db4", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Beijing'" + ] + }, + "execution_count": 120, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "capitals.get(\"China\", \"Beijing\")" + ] + }, + { + "cell_type": "code", + "execution_count": 121, + "id": "abc1e0ab", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'Canada': 'Happy', 'Mexico': 'Mexico City'}" + ] + }, + "execution_count": 121, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "capitals" + ] + }, + { + "cell_type": "code", + "execution_count": 124, + "id": "21522864", + "metadata": {}, + "outputs": [ + { + "ename": "SyntaxError", + "evalue": "invalid syntax (3988402761.py, line 1)", + "output_type": "error", + "traceback": [ + " \u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[124]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[31m \u001b[39m\u001b[31mcapitals.append{\"China\":\"Beijing\"}\u001b[39m\n ^\n\u001b[31mSyntaxError\u001b[39m\u001b[31m:\u001b[39m invalid syntax\n" + ] + } + ], + "source": [ + "capitals.append{\"China\":\"Beijing\"}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "18d9d730", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "python-env", + "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 +} diff --git a/02_activities/my_activities/Loops examples.ipynb b/02_activities/my_activities/Loops examples.ipynb new file mode 100644 index 000000000..e7f48e735 --- /dev/null +++ b/02_activities/my_activities/Loops examples.ipynb @@ -0,0 +1,426 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 26, + "id": "90aa3881", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Monday\n", + "Tuesday\n", + "Wednesday\n", + "Thursday\n" + ] + } + ], + "source": [ + "for i in [\"Monday\", \"Tuesday\",\"Wednesday\",\"Thursday\"]: #1st loop, i = Monday, 2nd loop, i = Tuesday, etc\n", + " print(i)" + ] + }, + { + "cell_type": "markdown", + "id": "2c913f01", + "metadata": {}, + "source": [ + "Until all the items in the parameter is ran, then it will run the next command" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "4e183713", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "5\n" + ] + } + ], + "source": [ + "for number in {1,2,3,4,5}: #it works in set as well. \n", + " print(number)" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "dcfb552f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "{'cat', 'dog', 'fork', 'zpple'}" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "items = {\"cat\", \"zpple\", \"dog\", \"fork\"}\n", + "items" + ] + }, + { + "cell_type": "markdown", + "id": "8b9b712c", + "metadata": {}, + "source": [ + "Unsure why it happens this way" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "95b376ec", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "zpple\n", + "dog\n", + "fork\n", + "cat\n" + ] + } + ], + "source": [ + "for item in items:\n", + " print(item)" + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "id": "39392f09", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 0\n", + "1 1\n", + "2 4\n", + "3 9\n", + "4 16\n", + "5 25\n", + "6 36\n" + ] + } + ], + "source": [ + "for i in range(7):\n", + " print(i, i**2)" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "9111585e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "3\n", + "4\n", + "5\n" + ] + } + ], + "source": [ + "for i in range (2,6):\n", + " print (i)" + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "id": "0f04e2d3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['data_01.xlsx', 'data_02.xlsx', 'data_03.xlsx']" + ] + }, + "execution_count": 40, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "input_files = [\"data_01.csv\", \"data_02.csv\", \"data_03.csv\"]\n", + "output_files = [] #create a new list first so that we can use this list \n", + "\n", + "for file in input_files:\n", + " output_file_name = file.replace(\".csv\",'.xlsx')\n", + " output_files.append(output_file_name)\n", + "\n", + "output_files" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "id": "c7bd02a4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0 is Veggies\n", + "1 is Veggies\n", + "2 is Happy\n", + "3 is Bible\n" + ] + } + ], + "source": [ + "for idx, food in enumerate([\"Veggies\", \"Veggies\", \"Happy\", \"Bible\"]):\n", + " print(idx, \"is \" + food)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "787f64a8", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4\n", + "3\n", + "2\n", + "1\n" + ] + } + ], + "source": [ + "#while loop - a value(?) has to be set up first\n", + "\n", + "countdown = 4\n", + "\n", + "while countdown > 0:\n", + " print(countdown)\n", + " countdown -= 1" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "337bab6a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "4\n", + "We will break at 3\n", + "hello\n" + ] + } + ], + "source": [ + "# break statement interrupts the execution of a loop --> exiting the loop\n", + "# just use the previous countdown value as example\n", + "\n", + "countdown = 5\n", + "\n", + "while countdown > 0:\n", + " print(countdown)\n", + " countdown -=1\n", + " if countdown ==3:\n", + " print(\"We will break at 3\")\n", + " break\n", + "print(countdown) # in this case, it prints after the while loop is over" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "5de97069", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "5\n", + "hello\n", + "4\n", + "We will break at 3\n" + ] + } + ], + "source": [ + "# break statement interrupts the execution of a loop --> exiting the loop\n", + "# just use the previous countdown value as example\n", + "\n", + "countdown = 5\n", + "\n", + "while countdown > 0:\n", + " print(countdown)\n", + " countdown -=1\n", + " if countdown ==3:\n", + " print(\"We will break at 3\")\n", + " break\n", + " print(\"hello\") # in this case, it prints within the while loop" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "abd96c21", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "You can't do that!\n", + "You can't do that!\n", + "You want hello? Wish granted\n", + "You want a? Wish granted\n", + "You want d? Wish granted\n", + "You have used all your wishes.\n" + ] + } + ], + "source": [ + "# continue statement says to leave the current iteration of the loop and start back up at the top\n", + "\n", + "wishes = 3\n", + "\n", + "while wishes > 0:\n", + " wish = input(\"Make a wish: \") #now it is interactive!\n", + " if 'infinite wishes' in wish.lower(): #or we can use \"infinite wishes\" == wish.lower()\n", + " print(\"You can\\'t do that!\")\n", + " continue\n", + " else:\n", + " print(f\"You want {wish}? Wish granted\")\n", + " wishes -=1 #mind the intendation of the wishes -= 1, should be just 1 intendation under the while loop\n", + "\n", + "print(\"You have used all your wishes.\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "8657ba7d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Now it is 12.0 x 3\n", + "35\n", + "34\n", + "Now it is 11.0 x 3\n", + "32\n", + "31\n", + "Now it is 10.0 x 3\n", + "29\n", + "28\n", + "Now it is 9.0 x 3\n", + "26\n", + "25\n", + "Now it is 8.0 x 3\n", + "23\n", + "22\n", + "Now it is 7.0 x 3\n", + "20\n", + "19\n", + "Now it is 6.0 x 3\n", + "17\n", + "16\n", + "Now it is 5.0 x 3\n", + "14\n", + "13\n", + "Now it is 4.0 x 3\n", + "11\n", + "10\n", + "Now it is 3.0 x 3\n", + "8\n", + "7\n", + "Now it is 2.0 x 3\n", + "5\n", + "4\n", + "Now it is 1.0 x 3\n", + "2\n", + "1\n", + "hello\n" + ] + } + ], + "source": [ + "countdown = 36\n", + "#while we can't do all integers, we can use divisible or not\n", + "\n", + "while countdown > 0: \n", + " if countdown %3 != 0: \n", + " print(countdown)\n", + " else:\n", + " multiple = countdown/3\n", + " print(f\"Now it is {multiple} x 3\")\n", + " countdown -=1\n", + "print(\"hello\") " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "52cef5a7", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "python-env", + "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 +} diff --git a/02_activities/my_activities/Python_Lesson_04.ipynb b/02_activities/my_activities/Python_Lesson_04.ipynb new file mode 100644 index 000000000..ee23f28ac --- /dev/null +++ b/02_activities/my_activities/Python_Lesson_04.ipynb @@ -0,0 +1,190 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "fb39f575", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "characters = [1,2,3,'a','b','c']\n", + "characters[2]\n" + ] + }, + { + "cell_type": "markdown", + "id": "b02239ce", + "metadata": {}, + "source": [ + "Lists are mutable - data types that are mutable are modified in place" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "8c305047", + "metadata": {}, + "outputs": [], + "source": [ + "julia = [\"bread\",'cheese','happy']\n", + "kaylie = julia" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "bdd392ac", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['bread', 'blue cheese', 'happy']" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "julia[1] = 'blue cheese'\n", + "kaylie" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "a5361296", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(julia)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "190b17b0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "11" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(julia[1])" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "c0cd6aab", + "metadata": {}, + "outputs": [], + "source": [ + "julia[2] = \"bottle\"" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "b3e3dbcf", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['bread', 'blue cheese', 'bottle']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "julia" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "819a8da0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['bread', 'blue cheese', 'bottle']" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "julia[:]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "14d7eb71", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "python-env", + "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 +} diff --git a/02_activities/my_activities/functions_with_multiple_parameters.ipynb b/02_activities/my_activities/functions_with_multiple_parameters.ipynb new file mode 100644 index 000000000..2844aec3c --- /dev/null +++ b/02_activities/my_activities/functions_with_multiple_parameters.ipynb @@ -0,0 +1,307 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 12, + "id": "3a9bc10c", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(174.2, 0.15, 20)" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def cal_sales_tax(price, tax_rate = 0.13, tip_rate = 0.15):\n", + " sales_tax = price * tax_rate\n", + " tips = price * tip_rate\n", + " total = price + sales_tax + tips\n", + " return total, tax_rate, round(tips)\n", + "\n", + "customer_1 = cal_sales_tax(134, 0.15)\n", + "customer_1" + ] + }, + { + "cell_type": "markdown", + "id": "12e8c456", + "metadata": {}, + "source": [ + "We can return many variables in the return line\n", + "and the variables can also be modified differently" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "40e7de3d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "15.0%\n" + ] + }, + { + "data": { + "text/plain": [ + "(174.2, None, 20)" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def cal_sales_tax_print(price, tax_rate = 0.13, tip_rate = 0.15):\n", + " sales_tax = price * tax_rate\n", + " tips = price * tip_rate\n", + " total = price + sales_tax + tips\n", + " return total, print(str(tax_rate*100) + \"%\"), round(tips)\n", + "\n", + "customer_1 = cal_sales_tax_print(134, 0.15)\n", + "customer_1" + ] + }, + { + "cell_type": "markdown", + "id": "766601f4", + "metadata": {}, + "source": [ + "However, in this scenario, \"print\" is performed first before returning all the output as it is a string but not numbers" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "32abf1dc", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(141.25, 0.13, 0)" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "customer_2 = cal_sales_tax(125, tip_rate = 0)\n", + "customer_2" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "a76c9efd", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(62.5, 0.1, 8)" + ] + }, + "execution_count": 17, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "customer_3 = cal_sales_tax(tax_rate = 0.1, price = 50)\n", + "customer_3" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "108480c4", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(160.0, 0.13, 19)" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "new_price = 125\n", + "\n", + "customer_4 = cal_sales_tax(new_price)\n", + "customer_4\n" + ] + }, + { + "cell_type": "markdown", + "id": "becc6b12", + "metadata": {}, + "source": [ + "In terms of global scope and local scope, the function have access to the global variables\n", + "so if we set a new variable called new_price and put it as the first parameter, \n", + "the function will understand this value and runs properly.\n", + "However, we can't call print(tax_rate) out of the function as tax_rate is defined within the function(locally) but not globally\n" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "03a51f92", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\"Let's see if it's a dog\"" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"Let's see if it's a dog\"" + ] + }, + { + "cell_type": "markdown", + "id": "95f5d1a5", + "metadata": {}, + "source": [ + "Use a double quote to have a line containing ' in string" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "92fec011", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'hahahahahahahaha'" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"ha\" *8" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "b2a9e986", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'hello worldhello'" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"hello world\" + \"hello\"" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "21c681fa", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "50" + ] + }, + "execution_count": 28, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "Apple = 50\n", + "apple = Apple\n", + "apple" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "a579cb5a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 30, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"20\" == str(20)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f617a7f9", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "python-env", + "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 +} diff --git a/02_activities/my_activities/numpy.ipynb b/02_activities/my_activities/numpy.ipynb new file mode 100644 index 000000000..dc48e31f7 --- /dev/null +++ b/02_activities/my_activities/numpy.ipynb @@ -0,0 +1,525 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 71, + "id": "a6b94d44", + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "# numpy allows us to works with arrays" + ] + }, + { + "cell_type": "code", + "execution_count": 72, + "id": "abeb7947", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1, 2, 3],\n", + " [4, 5, 6]])" + ] + }, + "execution_count": 72, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "abc = np.array([[1,2,3],[4,5,6]]) #the 2 smaller brackets are in 2 rows\n", + "abc" + ] + }, + { + "cell_type": "markdown", + "id": "13c5a5fa", + "metadata": {}, + "source": [] + }, + { + "cell_type": "code", + "execution_count": 73, + "id": "2b86592a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "(2, 3)\n", + "6\n", + "int64\n" + ] + } + ], + "source": [ + "print(abc.ndim) #print the number of dimension abc is\n", + "print(abc.shape) #rows , columns\n", + "print(abc.size) # rows x columns\n", + "print(abc.dtype)" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "id": "c9317d6a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[0., 0., 0.],\n", + " [0., 0., 0.],\n", + " [0., 0., 0.]],\n", + "\n", + " [[0., 0., 0.],\n", + " [0., 0., 0.],\n", + " [0., 0., 0.]]])" + ] + }, + "execution_count": 74, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.zeros((2,3,3))" + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "id": "83246238", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[1, 1, 1],\n", + " [1, 1, 1]])" + ] + }, + "execution_count": 75, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.ones_like(abc) #but twos not usable!" + ] + }, + { + "cell_type": "code", + "execution_count": 76, + "id": "773a540b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[0., 0., 0.],\n", + " [0., 0., 0.],\n", + " [0., 0., 0.]],\n", + "\n", + " [[0., 0., 0.],\n", + " [0., 0., 0.],\n", + " [0., 0., 0.]]])" + ] + }, + "execution_count": 76, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.zeros([2,3,3]) #now it is list but not tuples" + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "id": "c729ee66", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([1, 3, 5, 7, 9])" + ] + }, + "execution_count": 77, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.arange(1,10,2) #1D array from 1 up to but not including 10 in steps of 2" + ] + }, + { + "cell_type": "code", + "execution_count": 78, + "id": "a98656d7", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[0., 0., 0.],\n", + " [0., 0., 0.]],\n", + "\n", + " [[0., 0., 0.],\n", + " [0., 0., 0.]],\n", + "\n", + " [[0., 0., 0.],\n", + " [0., 0., 0.]],\n", + "\n", + " [[0., 0., 0.],\n", + " [0., 0., 0.]]])" + ] + }, + "execution_count": 78, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.zeros([4,2,3]) # 4 blocks/layers, 2 rows, 3 columns - 4 matrices, each of shape (2, 3), still 3-D array" + ] + }, + { + "cell_type": "code", + "execution_count": 79, + "id": "4b78e2f3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[6, 6, 4, 5],\n", + " [6, 6, 1, 7],\n", + " [7, 4, 3, 5]], dtype=int32)" + ] + }, + "execution_count": 79, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# 3x4 array of random integers that range between 1 up but not including 10\n", + "np.random.randint(1,10,(3,4))" + ] + }, + { + "cell_type": "code", + "execution_count": 80, + "id": "c89276a4", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[[12, 7],\n", + " [18, 3],\n", + " [ 4, 4],\n", + " [13, 17],\n", + " [18, 6]],\n", + "\n", + " [[14, 3],\n", + " [12, 11],\n", + " [ 1, 9],\n", + " [13, 14],\n", + " [19, 4]],\n", + "\n", + " [[ 5, 4],\n", + " [ 2, 1],\n", + " [19, 19],\n", + " [17, 7],\n", + " [14, 10]],\n", + "\n", + " [[14, 17],\n", + " [ 1, 15],\n", + " [16, 2],\n", + " [ 5, 1],\n", + " [ 5, 2]]], dtype=int32)" + ] + }, + "execution_count": 80, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# creates reproducible random number, but fixed after generating - seed!\n", + "np.random.seed(12) \n", + "# this 12 has no attribute to the maximum value that can be randomly generated\n", + "# this 12 can be changed to anything even string, it just acts like a variable\n", + "np.random.randint(1,20, (4,5,2))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "248e87fa", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 5, 4, 3, 2, 1, 0],\n", + " [10, 8, 6, 4, 2, 0]])" + ] + }, + "execution_count": 81, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bbb = np.array([[5,4,3,2,1,0], [10,8,6,4,2,0]]) #total 12 items\n", + "bbb" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b7dfbba6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 5, 10],\n", + " [ 4, 8],\n", + " [ 3, 6],\n", + " [ 2, 4],\n", + " [ 1, 2],\n", + " [ 0, 0]])" + ] + }, + "execution_count": 82, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bbb.T # T stands for transpose" + ] + }, + { + "cell_type": "code", + "execution_count": 83, + "id": "2c32713f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 5, 4, 3],\n", + " [ 2, 1, 0],\n", + " [10, 8, 6],\n", + " [ 4, 2, 0]])" + ] + }, + "execution_count": 83, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bbb.reshape(4,3)" + ] + }, + { + "cell_type": "code", + "execution_count": 84, + "id": "a1eeb2e2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 5],\n", + " [ 4],\n", + " [ 3],\n", + " [ 2],\n", + " [ 1],\n", + " [ 0],\n", + " [10],\n", + " [ 8],\n", + " [ 6],\n", + " [ 4],\n", + " [ 2],\n", + " [ 0]])" + ] + }, + "execution_count": 84, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bbb.reshape(12,1)" + ] + }, + { + "cell_type": "code", + "execution_count": 85, + "id": "98a26b8d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 5, 4, 3, 2, 1, 0, 10, 8, 6, 4, 2, 0]])" + ] + }, + "execution_count": 85, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bbb.reshape(1,12)" + ] + }, + { + "cell_type": "code", + "execution_count": 87, + "id": "74c2a1e2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 5, 4, 3, 2, 1, 0],\n", + " [10, 8, 6, 4, 2, 0]])" + ] + }, + "execution_count": 87, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bbb" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a913c185", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 1, 2, 3, 5, 4, 3, 2, 1, 0],\n", + " [ 4, 5, 6, 10, 8, 6, 4, 2, 0]])" + ] + }, + "execution_count": 89, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.hstack((abc,bbb)) #stack horizontally" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a6453a00", + "metadata": {}, + "outputs": [ + { + "ename": "ValueError", + "evalue": "all the input array dimensions except for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 3 and the array at index 1 has size 6", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mValueError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[90]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43mnp\u001b[49m\u001b[43m.\u001b[49m\u001b[43mvstack\u001b[49m\u001b[43m(\u001b[49m\u001b[43m(\u001b[49m\u001b[43mabc\u001b[49m\u001b[43m,\u001b[49m\u001b[43mbbb\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\Chan Yat Yan\\Desktop\\Python_DSI2025\\python\\python-env\\Lib\\site-packages\\numpy\\_core\\shape_base.py:292\u001b[39m, in \u001b[36mvstack\u001b[39m\u001b[34m(tup, dtype, casting)\u001b[39m\n\u001b[32m 290\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(arrs, \u001b[38;5;28mtuple\u001b[39m):\n\u001b[32m 291\u001b[39m arrs = (arrs,)\n\u001b[32m--> \u001b[39m\u001b[32m292\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43m_nx\u001b[49m\u001b[43m.\u001b[49m\u001b[43mconcatenate\u001b[49m\u001b[43m(\u001b[49m\u001b[43marrs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[32;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdtype\u001b[49m\u001b[43m=\u001b[49m\u001b[43mdtype\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcasting\u001b[49m\u001b[43m=\u001b[49m\u001b[43mcasting\u001b[49m\u001b[43m)\u001b[49m\n", + "\u001b[31mValueError\u001b[39m: all the input array dimensions except for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 3 and the array at index 1 has size 6" + ] + } + ], + "source": [ + "np.vstack((abc,bbb)) #stack vertically - but what is the function??" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e8f11351", + "metadata": {}, + "outputs": [], + "source": [ + "r_bbb = np.reshape(bbb, (4,3)) " + ] + }, + { + "cell_type": "code", + "execution_count": 95, + "id": "4fd0ff8f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "array([[ 1, 2, 3],\n", + " [ 4, 5, 6],\n", + " [ 5, 4, 3],\n", + " [ 2, 1, 0],\n", + " [10, 8, 6],\n", + " [ 4, 2, 0]])" + ] + }, + "execution_count": 95, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "np.vstack((abc, r_bbb))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "02da5d84", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "python-env", + "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 +} diff --git a/02_activities/my_activities/open file.ipynb b/02_activities/my_activities/open file.ipynb new file mode 100644 index 000000000..3a54b4f0f --- /dev/null +++ b/02_activities/my_activities/open file.ipynb @@ -0,0 +1,119 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "12450569", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\"longitude\",\"latitude\",\"housing_median_age\",\"total_rooms\",\"total_bedrooms\",\"population\",\"households\",\"median_income\",\"median_house_value\"\n", + "\n", + "-122.050000,37.370000,27.000000,3885.000000,661.000000,1537.000000,606.000000,6.608500,344700.000000\n", + "\n", + "-118.300000,34.260000,43.000000,1510.000000,310.000000,809.000000,277.000000,3.599000,176500.000000\n", + "\n", + "-117.810000,33.780000,27.000000,3589.000000,507.000000,1484.000000,495.000000,5.793400,270500.000000\n", + "\n", + "-118.360000,33.820000,28.000000,67.000000,15.000000,49.000000,11.000000,6.135900,330000.000000\n", + "\n", + "-119.670000,36.330000,19.000000,1241.000000,244.000000,850.000000,237.000000,2.937500,81700.000000\n", + "\n", + "-119.560000,36.510000,37.000000,1018.000000,213.000000,663.000000,204.000000,1.663500,67000.000000\n", + "\n", + "-121.430000,38.630000,43.000000,1009.000000,225.000000,604.000000,218.000000,1.664100,67000.000000\n", + "\n", + "-120.650000,35.480000,19.000000,2310.000000,471.000000,1341.000000,441.000000,3.225000,166900.000000\n", + "\n", + "-122.840000,38.400000,15.000000,3080.000000,617.000000,1446.000000,599.000000,3.669600,194400.000000\n", + "\n" + ] + } + ], + "source": [ + "with open(r'C:\\Users\\Chan Yat Yan\\Desktop\\Python_DSI2025\\python\\05_src\\data\\slides_data\\california_housing_test.csv', 'r') as f: #f can be any characters I want\n", + " for i in range(10): #print how many times\n", + " print(f.readline())" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "a2ab6948", + "metadata": {}, + "outputs": [], + "source": [ + "# write() write a string to file\n", + "# writelines() write each item in an iterable to afile, with no text in between\n", + "\n", + "provinces_in_canada = [\"BC\", \"ON\", \"AB\", \"SK\", \"MB\", \"QC\", \"NL\"]\n", + "with open('provinces.txt','w') as a_file: #we are now creating a txt file in the same file location as the current file\n", + " a_file.writelines(provinces_in_canada)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "a1f61769", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "BCONABSKMBQCNL\n" + ] + } + ], + "source": [ + "with open(\"provinces.txt\",\"r\") as f:\n", + " print(f.read())" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "dca1c756", + "metadata": {}, + "outputs": [], + "source": [ + "# to use relative file location" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b4bc8435", + "metadata": {}, + "outputs": [], + "source": [ + "#object-oriented programming\n", + "\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "python-env", + "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 +} diff --git a/02_activities/my_activities/provinces.txt b/02_activities/my_activities/provinces.txt new file mode 100644 index 000000000..b37259ca5 --- /dev/null +++ b/02_activities/my_activities/provinces.txt @@ -0,0 +1 @@ +BCONABSKMBQCNL \ No newline at end of file diff --git a/02_activities/my_activities/strings related examples.ipynb b/02_activities/my_activities/strings related examples.ipynb new file mode 100644 index 000000000..793b7a3a9 --- /dev/null +++ b/02_activities/my_activities/strings related examples.ipynb @@ -0,0 +1,833 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 6, + "id": "69972e75", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'CHAN'" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lastname = \"CHAN\"\n", + "firstname = \"Yat_Yan\"\n", + "lastname" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "47b2ca47", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'HAN'" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lastname[1:]" + ] + }, + { + "cell_type": "markdown", + "id": "3d5f77bc", + "metadata": {}, + "source": [ + "\"Wanted from\" position 1 onwards (inclusive)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "5adea9f3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'C'" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lastname[:1]" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "60ae46be", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Yat'" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "firstname[:3]" + ] + }, + { + "cell_type": "markdown", + "id": "c428db5a", + "metadata": {}, + "source": [ + "\"Wanted upto\" position 3 (excluded)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "42581dfe", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Yat_'" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "firstname[:-3]" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "ba664cd1", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'_Yan'" + ] + }, + "execution_count": 11, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "firstname[3:]" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "d91af355", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Yan'" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "firstname[-3:]" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "ec6c6552", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'_Yan'" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "firstname[3:]" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "cefbdc23", + "metadata": {}, + "outputs": [ + { + "ename": "TypeError", + "evalue": "string indices must be integers, not 'tuple'", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mTypeError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[14]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43mfirstname\u001b[49m\u001b[43m[\u001b[49m\u001b[32;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[32;43m4\u001b[39;49m\u001b[43m]\u001b[49m\n", + "\u001b[31mTypeError\u001b[39m: string indices must be integers, not 'tuple'" + ] + } + ], + "source": [ + "firstname[0,4]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "981ed429", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'YY'" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "firstname[0]+firstname[4]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3bbe472b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'CHAN'" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lastname[0:7]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d033f5c0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Yat_Yan'" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "firstname[0:7]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f41e6ca2", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'YY'" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "firstname[0:7:4]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b1279c9e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Y_n'" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "firstname[0:7:3] #str[start:end+1:step]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "52e969fb", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'960'" + ] + }, + "execution_count": 45, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "phone = \"905-960-8684\"\n", + "\n", + "phone[4:7]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "95a9018a", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'960-8684'" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "phone[-8:]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "771a66bf", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'4868-069-509'" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "phone[::-1]" + ] + }, + { + "cell_type": "markdown", + "id": "5b10efbe", + "metadata": {}, + "source": [ + "It reverses everything in phone\n", + "first empty+: refers to start from the end, we can give any numbers referring to desired position\n", + "second empty+: refers to from the start, we can give any numbers referring to the desired position too\n", + "-1 means in step of 1, but reverse" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "72c3455f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'46-6-0'" + ] + }, + "execution_count": 50, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "phone[::-2]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b7b5fd5b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "12" + ] + }, + "execution_count": 51, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "len(phone)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b1b4a5a4", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['-', '-', '0', '0', '4', '5', '6', '6', '8', '8', '9', '9']\n" + ] + } + ], + "source": [ + "sorted_phone_number = sorted(phone)\n", + "print(sorted_phone_number)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "62582c6e", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'I LOVE SCIENCE'" + ] + }, + "execution_count": 53, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a_sentence = \"I love science\"\n", + "a_sentence.upper()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ffa29ad3", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'i love science'" + ] + }, + "execution_count": 54, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "a_sentence.lower()" + ] + }, + { + "cell_type": "markdown", + "id": "4c9422ff", + "metadata": {}, + "source": [ + ".upper() and .lower(), they take nothing in the (), but we'll still need the () as it is a string method" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "71d6c588", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"This string is ununununinuhin\".count(\"u\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e1299d2f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"Hello\".endswith('o')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "c6c34f72", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "False" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"hello\".endswith(\".txt\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "5838f69d", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\"long**file**names**with**a**lot**of**spaces**then**ha**let's**try\"" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"long file names with a lot of spaces then ha let's try\".replace(\" \", \"**\") # replace <> with ** as indicated by me" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "10bb84ad", + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'firstname' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mNameError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[5]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43mfirstname\u001b[49m\n", + "\u001b[31mNameError\u001b[39m: name 'firstname' is not defined" + ] + } + ], + "source": [ + "firstname" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "64603fdc", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'Yat_Yan'" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "firstname" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "e1312ed0", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'CHAN'" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "lastname" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "8c98010b", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'My name is CHAN Yat Yan'" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\"My name is {} {}\".format(lastname,firstname[:3]+ \" \" + firstname[-3:])" + ] + }, + { + "cell_type": "markdown", + "id": "0a197201", + "metadata": {}, + "source": [ + "In this scenario, we can play with a lot of stuffs within the parameter with .format\n", + "but the lecturer prefers f-string better (so am I)" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "540ba2dd", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'My favourite meal is pizza and coca-cola'" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# First we should define a variable outsdie of the f-string\n", + "\n", + "food = \"pizza\"\n", + "drink = \"coca-cola\"\n", + "\n", + "f\"My favourite meal is {food} and {drink}\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f1fdde8f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'My favourite meal is sushi and water'" + ] + }, + "execution_count": 27, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "f\"My favourite meal is {'sushi'} and {'water'}\" \n", + "#in this case, with ' ', I can change the content in {} be anything I want without changing the value stored in the variable" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "1eeb0ddc", + "metadata": {}, + "outputs": [], + "source": [ + "age = input(\"How old are you?\") \n", + "#this input is a function which allows user to input new values into the system\n", + "#the value input is in form of string" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "0f5aeb48", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "35" + ] + }, + "execution_count": 32, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "age\n", + "int(age) #now I convert the value in age from str to number for further operations" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "25d5654b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "I'll be 36 next year\n" + ] + } + ], + "source": [ + "print(\"I'll be \" + str(int(age)+1) + \" next year\") \n", + "#Now we can play with the integer values and convert it to string for printing" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "id": "9b0e7b48", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def anagram_checker(word_a, word_b, is_case_sensitive):\n", + " # Modify your existing code here\n", + " if (sorted(word_a) == sorted(word_b))== is_case_sensitive:\n", + " return True\n", + " else: \n", + " return False\n", + "# Run your code to check using the words below:\n", + "anagram_checker(\"Silent\", \"listen\", False) # True" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "python-env", + "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 +}