File size: 88,704 Bytes
5ab8a9f
1
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"collapsed_sections":["EH1YPTtUtVHC","9tEiHo4FCeF4","Kkx-fAUgLmpe","L0rqeUbuZ5pL","Dk5jkOO6jjJq","YpMF_l7Jx6wM","AVjTUSBN2s-N"],"authorship_tag":"ABX9TyPXfqNDuvBuFd89kkBR20WJ"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"markdown","source":["# **`String Manipulation`**"],"metadata":{"id":"34U5ywTkRZQM"}},{"cell_type":"markdown","source":["# String methods and formatting\n","* Reference: https://www.w3schools.com/python/python_strings.asp\n","* Practice problems: https://www.w3resource.com/python-exercises/string/\n","\n","\n","\n","\n","\n","\n","\n"],"metadata":{"id":"EH1YPTtUtVHC"}},{"cell_type":"code","source":["a = \"\"\"Lorem ipsum dolor sit amet,\n","consectetur adipiscing elit,\n","sed do eiusmod tempor incididunt\n","ut labore et dolore magna aliqua.\"\"\"\n","print(a)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"Ir7GyfGP9eXz","executionInfo":{"status":"ok","timestamp":1746552709203,"user_tz":-360,"elapsed":15,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"f489f10c-7343-4586-b4ff-2cd62cd6891b"},"execution_count":1,"outputs":[{"output_type":"stream","name":"stdout","text":["Lorem ipsum dolor sit amet,\n","consectetur adipiscing elit,\n","sed do eiusmod tempor incididunt\n","ut labore et dolore magna aliqua.\n"]}]},{"cell_type":"code","source":["b = \"Hello, World!\"\n","print(b[1:5])"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"hAHb3Vrh9xV9","executionInfo":{"status":"ok","timestamp":1746552709307,"user_tz":-360,"elapsed":52,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"85d247ad-ab27-4d7d-f7c8-5cf20e636c70"},"execution_count":2,"outputs":[{"output_type":"stream","name":"stdout","text":["ello\n"]}]},{"cell_type":"code","source":["b = \"Hello, World!\"\n","print(b[-5:-2])"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"kX4fnDhB92SW","executionInfo":{"status":"ok","timestamp":1746552709309,"user_tz":-360,"elapsed":34,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"d8bbe758-e97f-49bd-a0ab-f8d913fc2172"},"execution_count":3,"outputs":[{"output_type":"stream","name":"stdout","text":["orl\n"]}]},{"cell_type":"code","source":["a = \"Hello, World!\"\n","print(a.upper())\n","print(a.lower())"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"v1hPa9fG-PAh","executionInfo":{"status":"ok","timestamp":1746552709310,"user_tz":-360,"elapsed":7,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"db356f64-ea31-40a9-f0b2-b969e1747af1"},"execution_count":4,"outputs":[{"output_type":"stream","name":"stdout","text":["HELLO, WORLD!\n","hello, world!\n"]}]},{"cell_type":"code","source":["a = \"           Hello, World!             \\n\"\n","print(a.strip())"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"1Ovm72BV-TCZ","executionInfo":{"status":"ok","timestamp":1746552709318,"user_tz":-360,"elapsed":7,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"531c0161-38c0-4358-f5dc-12947d72e17f"},"execution_count":5,"outputs":[{"output_type":"stream","name":"stdout","text":["Hello, World!\n"]}]},{"cell_type":"code","source":["a = \"Hello, World!\"\n","print(a.replace(\"Hello\", \"HI\"))"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"IXaINAuW-YDa","executionInfo":{"status":"ok","timestamp":1746552709353,"user_tz":-360,"elapsed":28,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"80d09c50-26b6-4f01-9ff7-1a64f650b7bb"},"execution_count":6,"outputs":[{"output_type":"stream","name":"stdout","text":["HI, World!\n"]}]},{"cell_type":"code","source":["a = \"Hello, World!\"\n","print(a.split(\",\"))"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"rW1TnpCq-gZa","executionInfo":{"status":"ok","timestamp":1746552709389,"user_tz":-360,"elapsed":16,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"054cfce0-df16-481b-e3fd-a7cbcd2702e1"},"execution_count":7,"outputs":[{"output_type":"stream","name":"stdout","text":["['Hello', ' World!']\n"]}]},{"cell_type":"code","source":["a = \"Hello\"\n","b = \"World\"\n","c = a + \" \" + b\n","print(c)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"YYEnRueU_31r","executionInfo":{"status":"ok","timestamp":1746552709426,"user_tz":-360,"elapsed":33,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"b2ec0bfe-2f80-4048-b86c-eb834b0dd544"},"execution_count":8,"outputs":[{"output_type":"stream","name":"stdout","text":["Hello World\n"]}]},{"cell_type":"code","source":["age = 36\n","# txt = f\"My name is John, I am {age}\"\n","print(f\"My name is John, I am {age}\")"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"acL4JQ0pAZwY","executionInfo":{"status":"ok","timestamp":1746552709437,"user_tz":-360,"elapsed":5,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"2c9ee530-60e4-44b4-de99-61a41cae7d11"},"execution_count":9,"outputs":[{"output_type":"stream","name":"stdout","text":["My name is John, I am 36\n"]}]},{"cell_type":"code","source":["price = 59.5498459674\n","txt = f\"The price is {price:.2f} dollars\"\n","print(txt)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"HKNFpzF7Aqj6","executionInfo":{"status":"ok","timestamp":1746552709443,"user_tz":-360,"elapsed":5,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"f2be11bb-b983-4315-c137-ce1329456fc7"},"execution_count":10,"outputs":[{"output_type":"stream","name":"stdout","text":["The price is 59.55 dollars\n"]}]},{"cell_type":"code","source":["txt = \"Hi, I am a \\\"student\\\".\"\n","print(txt)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"-atXFBIwA0Mb","executionInfo":{"status":"ok","timestamp":1746552709471,"user_tz":-360,"elapsed":27,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"6ad0a821-5ca2-4582-a5b6-6e2869586615"},"execution_count":11,"outputs":[{"output_type":"stream","name":"stdout","text":["Hi, I am a \"student\".\n"]}]},{"cell_type":"code","source":["txt = \"Hello\\nWorld!\"\n","print(txt)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"Qi0BWARgBVYP","executionInfo":{"status":"ok","timestamp":1746552709489,"user_tz":-360,"elapsed":8,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"23615de5-4b24-4005-9020-ea72cf686f12"},"execution_count":12,"outputs":[{"output_type":"stream","name":"stdout","text":["Hello\n","World!\n"]}]},{"cell_type":"code","source":["text = \"hello world\"\n","print(text.capitalize())"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"H3NWthPyB7Uc","executionInfo":{"status":"ok","timestamp":1746552709514,"user_tz":-360,"elapsed":23,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"0036b297-b10a-43a2-aa2b-7f738354553e"},"execution_count":13,"outputs":[{"output_type":"stream","name":"stdout","text":["Hello world\n"]}]},{"cell_type":"code","source":["text = \"banana\"\n","print(text.count(\"a\"))"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"L34c_HkkB9gs","executionInfo":{"status":"ok","timestamp":1746552709523,"user_tz":-360,"elapsed":8,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"0ae37e6b-5989-4fbe-dd75-2c609a38f415"},"execution_count":14,"outputs":[{"output_type":"stream","name":"stdout","text":["3\n"]}]},{"cell_type":"code","source":["text = \"hello world\"\n","print(text.find(\"world\"))\n","print(text.find(\"python\"))"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"l-sr2fbZCFs9","executionInfo":{"status":"ok","timestamp":1746552709552,"user_tz":-360,"elapsed":28,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"fb69d519-a7d8-4c75-ff5e-7f50b88b111b"},"execution_count":15,"outputs":[{"output_type":"stream","name":"stdout","text":["6\n","-1\n"]}]},{"cell_type":"code","source":["words = [\"Python\", \"is\", \"awesome\"]\n","print(\" \".join(words))"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"MUUZo48FCIWF","executionInfo":{"status":"ok","timestamp":1746552709567,"user_tz":-360,"elapsed":8,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"6b8f15b5-c83d-4a12-d9d5-77ebfb933b65"},"execution_count":16,"outputs":[{"output_type":"stream","name":"stdout","text":["Python is awesome\n"]}]},{"cell_type":"code","source":["text = \"Python\"\n","print(f\"{text:<10}\")\n","print(f\"{text:>10}\")\n","print(f\"{text:^10}\")"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"TwybhbbOLxu1","executionInfo":{"status":"ok","timestamp":1746552709572,"user_tz":-360,"elapsed":4,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"be90a8f2-bd53-45fe-923f-a4b65bd7e1b0"},"execution_count":17,"outputs":[{"output_type":"stream","name":"stdout","text":["Python    \n","    Python\n","  Python  \n"]}]},{"cell_type":"code","source":["text = \"452\"\n","print(text.isalpha())\n","print(text.isdigit())"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"1ggoy0QtLzFN","executionInfo":{"status":"ok","timestamp":1746552709583,"user_tz":-360,"elapsed":5,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"bf7d25bb-dbac-4c74-baad-3fedeaad6d49"},"execution_count":18,"outputs":[{"output_type":"stream","name":"stdout","text":["False\n","True\n"]}]},{"cell_type":"code","source":["text = \"Python is fun and Python is powerful.\"\n","word_count = text.lower().count(\"python\")\n","print(word_count)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"si0R0d4PL06m","executionInfo":{"status":"ok","timestamp":1746552709610,"user_tz":-360,"elapsed":26,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"33ad9f83-7bdf-4c68-94c5-9298f8c0394f"},"execution_count":19,"outputs":[{"output_type":"stream","name":"stdout","text":["2\n"]}]},{"cell_type":"markdown","source":["**Examples**"],"metadata":{"id":"6dIO02aW_0Ui"}},{"cell_type":"code","source":["# Write a function extract_substring, that extracts a substring from the given string from index start to index end. Return the substring.\n","def extract_substring(s, start, end):\n","    return s[start:end]\n","\n","print(extract_substring(s = \"Hello, World!\", start = 1, end = 5))"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"mIGK7bFd56Xp","executionInfo":{"status":"ok","timestamp":1746552709620,"user_tz":-360,"elapsed":8,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"6fc4aa29-4bdc-46dd-d7d4-e80b099da1e5"},"execution_count":20,"outputs":[{"output_type":"stream","name":"stdout","text":["ello\n"]}]},{"cell_type":"code","source":["'''Write a function convert_case, that returns two values:\n","1. The string s converted to uppercase\n","2. The string s converted to lowercase'''\n","\n","def convert_case(s: str):\n","    return s.upper(), s.lower()\n","\n","print(convert_case(\"Hello, World!\"))"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"KUiY5lR18era","executionInfo":{"status":"ok","timestamp":1746552709636,"user_tz":-360,"elapsed":15,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"cbb69b48-da85-45af-dc32-f03fccfd3309"},"execution_count":21,"outputs":[{"output_type":"stream","name":"stdout","text":["('HELLO, WORLD!', 'hello, world!')\n"]}]},{"cell_type":"code","source":["# Write a function replace_substring, that replaces all occurrences of the substring old in string with new. Return the modified string.\n","def replace_substring(s, old, new):\n","    return s.replace(old, new)\n","\n","print(replace_substring(\"Hello, World!\", \"Hello\", \"HI\"))"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"Tz7ehhkj9KWw","executionInfo":{"status":"ok","timestamp":1746552709644,"user_tz":-360,"elapsed":7,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"a1df455e-85c7-4e92-a250-0f820961303c"},"execution_count":22,"outputs":[{"output_type":"stream","name":"stdout","text":["HI, World!\n"]}]},{"cell_type":"code","source":["# Write a function count_substring, that counts the number of times the substring sub appears in the string. Return the count.\n","def count_substring(s, sub):\n","    return s.count(sub)\n","\n","print(count_substring(\"banana\", \"a\"))"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"pBZUZHT--PiY","executionInfo":{"status":"ok","timestamp":1746552710240,"user_tz":-360,"elapsed":594,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"f8484ef7-8115-45ff-c012-ec86e4804113"},"execution_count":23,"outputs":[{"output_type":"stream","name":"stdout","text":["3\n"]}]},{"cell_type":"code","source":["# Write a function split_and_join, that splits the string by the given delimiter and joins the parts together using a space. Return the resulting string.\n","def split_and_join(s, delimiter):\n","    lst = s.split(delimiter)\n","    print(lst)\n","    return ' '.join(lst)\n","\n","print(split_and_join(\"Hello, World!\", \",\"))"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"gVqGNbor-iF6","executionInfo":{"status":"ok","timestamp":1746552710249,"user_tz":-360,"elapsed":7,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"77615440-a13c-4560-e021-33f81aa2c87e"},"execution_count":24,"outputs":[{"output_type":"stream","name":"stdout","text":["['Hello', ' World!']\n","Hello  World!\n"]}]},{"cell_type":"code","source":["# Write a function count_word_occurrences, that counts how many times the word word appears in the string (case-insensitive). Return the count.\n","def count_word_occurrences(s, word):\n","    return s.lower().count(word.lower())\n","\n","print(count_word_occurrences(\"Hello, World! hello, Python!\", \"hello\"))"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"UR-BqaXV_e15","executionInfo":{"status":"ok","timestamp":1746552710262,"user_tz":-360,"elapsed":12,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"07d19816-1556-4921-d116-4c749e932d41"},"execution_count":25,"outputs":[{"output_type":"stream","name":"stdout","text":["2\n"]}]},{"cell_type":"markdown","source":["# Regular expressions basics\n","* Reference: https://www.w3schools.com/python/python_regex.asp#matchobject\n","* Practice Editor: https://regexr.com/\n","* Practice Problems: https://www.w3resource.com/python-exercises/re/"],"metadata":{"id":"9tEiHo4FCeF4"}},{"cell_type":"markdown","source":["The findall() Function"],"metadata":{"id":"IAIjmjEPRT4V"}},{"cell_type":"code","source":["import re\n","txt = \"The rain in Spain\"\n","x = re.findall(\"ai\", txt)\n","print(x)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"b31q-uDGMCa_","executionInfo":{"status":"ok","timestamp":1734101101186,"user_tz":-360,"elapsed":54,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"dbbf6be3-1651-43a6-981c-ec2fc19db451"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["['ai', 'ai']\n"]}]},{"cell_type":"code","source":["import re\n","text = \"cat bat mat\"\n","result = re.findall(r\"[a-z]at\", text)\n","print(result)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"zusEoodmbr-y","executionInfo":{"status":"ok","timestamp":1734167340171,"user_tz":-360,"elapsed":344,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"aa50f2c3-cc0a-46be-8a61-2e4b6b68abd5"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["['cat', 'bat', 'mat']\n"]}]},{"cell_type":"markdown","source":["The search() Function"],"metadata":{"id":"sBX4Lyh7RjYX"}},{"cell_type":"code","source":["import re\n","txt = \"The rain in Spain\"\n","x = re.search(\"\\s\", txt)\n","print(x)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"-DxtmPYhRkev","executionInfo":{"status":"ok","timestamp":1734154897683,"user_tz":-360,"elapsed":315,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"12ca84f0-4a07-41aa-827b-203a669a085b"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["<re.Match object; span=(3, 4), match=' '>\n"]}]},{"cell_type":"code","source":["import re\n","txt = \"The rain in Spain\"\n","x = re.search(\"ai\", txt)\n","print(x)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"an2v09fsRtY_","executionInfo":{"status":"ok","timestamp":1734101101187,"user_tz":-360,"elapsed":52,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"45470f65-8dd3-4f11-99c6-93735512ec61"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["<re.Match object; span=(5, 7), match='ai'>\n"]}]},{"cell_type":"code","source":["import re\n","txt = \"The rain in Spain\"\n","x = re.search(r\"S\\w+\", txt)\n","print(x)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"bwnZBfGfSW9l","executionInfo":{"status":"ok","timestamp":1734167476144,"user_tz":-360,"elapsed":317,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"ce39cc00-9a4e-4834-ecab-50831e597b3a"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["<re.Match object; span=(12, 17), match='Spain'>\n"]}]},{"cell_type":"code","source":["import re\n","text = \"say hello to the world\"\n","result = re.search(r\"hello\", text)\n","print(result)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"dv-N_PKcbCnV","executionInfo":{"status":"ok","timestamp":1734156133681,"user_tz":-360,"elapsed":344,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"2e8f4233-248b-4c72-8a74-b99147f2f73d"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["<re.Match object; span=(4, 9), match='hello'>\n"]}]},{"cell_type":"markdown","source":["The split() Function"],"metadata":{"id":"LPbjCQeQXfXj"}},{"cell_type":"code","source":["import re\n","txt = \"The rain in Spain\"\n","x = re.split(\"\\s\", txt)\n","print(x)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"d3TcAi4GXfu7","executionInfo":{"status":"ok","timestamp":1734101101187,"user_tz":-360,"elapsed":48,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"d0e7bab0-f9d9-49d8-9c89-3295c6f60c2a"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["['The', 'rain', 'in', 'Spain']\n"]}]},{"cell_type":"code","source":["import re\n","txt = \"The rain in Spain\"\n","x = re.split(\"\\s\", txt, 1)\n","print(x)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"S7oDuDddX3BV","executionInfo":{"status":"ok","timestamp":1734101101187,"user_tz":-360,"elapsed":46,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"5299b60f-1b01-4dc8-e376-1569a2bec21b"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["['The', 'rain in Spain']\n"]}]},{"cell_type":"code","source":["import re\n","text = \"apple;banana,orange|grape\"\n","result = re.split(r\"[;,|]\", text)\n","print(result)"],"metadata":{"id":"gHNzWp-ucHN9","executionInfo":{"status":"ok","timestamp":1734101101187,"user_tz":-360,"elapsed":44,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"b8631118-2363-49bb-8d03-1312c439cd5f","colab":{"base_uri":"https://localhost:8080/"}},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["['apple', 'banana', 'orange', 'grape']\n"]}]},{"cell_type":"markdown","source":["The sub() Function"],"metadata":{"id":"HBeB8DxsZEKH"}},{"cell_type":"code","source":["import re\n","txt = \"The rain in Spain\"\n","x = re.sub(\"\\s\", \"9\", txt)\n","print(x)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"rROAkRoSX6cl","executionInfo":{"status":"ok","timestamp":1734101101187,"user_tz":-360,"elapsed":43,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"45e8e616-7c67-4a3c-95e5-8f073b4ae613"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["The9rain9in9Spain\n"]}]},{"cell_type":"code","source":["import re\n","txt = \"The rain in Spain\"\n","x = re.sub(\"\\s\", \"9\", txt, 2)\n","print(x)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"fXxTe2rrZMpo","executionInfo":{"status":"ok","timestamp":1734101101187,"user_tz":-360,"elapsed":41,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"5d8f5589-c5c1-454d-870d-7ee7a0de4af8"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["The9rain9in Spain\n"]}]},{"cell_type":"code","source":["import re\n","text = \"I love cats. Cats are great!\"\n","result = re.sub(r\"cats\", \"dogs\", text, flags=re.IGNORECASE)\n","print(result)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"WhNrguoJb5JX","executionInfo":{"status":"ok","timestamp":1734156342291,"user_tz":-360,"elapsed":332,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"a1002c49-4f5c-4aff-bca1-e3644bea9a3f"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["I love dogs. dogs are great!\n"]}]},{"cell_type":"markdown","source":["**Examples**"],"metadata":{"id":"Kkx-fAUgLmpe"}},{"cell_type":"code","source":["import re\n","text = \"Do you like C++ or Python?\"\n","result = re.search(r\"C\\+\\+\", text)\n","print(result.group())"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"oe9IKx5gLsYq","executionInfo":{"status":"ok","timestamp":1734156436620,"user_tz":-360,"elapsed":332,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"c704e2d4-4bea-4220-f826-d68ce11b5609"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["C++\n"]}]},{"cell_type":"code","source":["import re\n","email = \"user@example.com\"\n","pattern = r\"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,3}\"\n","result = re.search(pattern, email)\n","print(bool(result))\n","print(result.group())"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"yRUz97o2MeUZ","executionInfo":{"status":"ok","timestamp":1734157115787,"user_tz":-360,"elapsed":311,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"70591688-ed92-4c5c-8eea-394d3f8d8f2c"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["True\n","user@example.com\n"]}]},{"cell_type":"code","source":["import re\n","email = \"19202103271@cse.bubt.edu.bd\"\n","pattern = r\"([a-zA-Z0-9._%+-])+@([a-zA-Z0-9.-]{3})\\.([a-zA-Z0-9.-])+.[bd]{2}\"\n","result = re.search(pattern, email)\n","print(bool(result))\n","print(result.group())"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"q3WeAnKF3Hrf","executionInfo":{"status":"ok","timestamp":1734157444053,"user_tz":-360,"elapsed":342,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"6382bfbc-78c8-4a0e-bc47-872e0576bac5"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["True\n","19202103271@cse.bubt.edu.bd\n"]}]},{"cell_type":"code","source":["import re\n","email = '''RegExr was created by gskinner.com.\n","01792103884\n","Edit the Expression & Text to see matches. Roll over matches or the expression for details. PCRE & JavaScript flavo+88-01692105884rs of RegEx are supported. Validate your expression with Tests mode.\n","The side bar includes a Cheatsheet, full Reference, and Help. You can a0lso Save & Share with the C01792103884ommunity and view arif@gmail.comatterns you create or favorite in My Patterns.\n","Explore results 01649305402654694945 with the Tools below. Replace &  List user@example.comput custom @example.comresults. Detai+88-ls lists capture gro. Ex+88-01794503784plain describes your expression in plain English.\n","'''\n","pattern = r\"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,3}\"\n","result = re.findall(pattern, email)\n","print(result)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"bZnuulozMRpG","executionInfo":{"status":"ok","timestamp":1734101101188,"user_tz":-360,"elapsed":35,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"08650104-043c-4edf-f6b2-c9b310f12a50"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["['arif@gmail.com', 'user@example.com']\n"]}]},{"cell_type":"code","source":["import re\n","text = \"There are 12 cats and 45 dogs.\"\n","numbers = re.findall(r\"\\d+\", text)\n","print(numbers)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"ATA0o28qNVVN","executionInfo":{"status":"ok","timestamp":1734101101188,"user_tz":-360,"elapsed":33,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"03d45c5f-0b50-4410-e262-9d6f4a000477"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["['12', '45']\n"]}]},{"cell_type":"code","source":["import re\n","email = \"+88-01792103884\"\n","pattern = r\"^((?:\\+88-)?01[3-9]\\d{8})$\"\n","result = re.findall(pattern, email)\n","print(result)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"d49PNGPaMeZw","executionInfo":{"status":"ok","timestamp":1734170409589,"user_tz":-360,"elapsed":391,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"cdc68754-e861-439e-f9cc-de1efefc62dd"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["['+88-01792103884']\n"]}]},{"cell_type":"code","source":["import re\n","text = '''RegExr was created by gskinner.com. Edit the Expression & Text to see matches. Roll over matches or the expression for details. PCRE & JavaScript flavo+88-01692105884rs of RegEx are supported. Validate your expression with Tests mode.\n","The side bar includes a Cheatsheet, full Reference, and Help. You can also Save & Share with the C017982103484ommunity and view patterns you create or favorite in My Patterns.\n","Explore results 01649305402 with the Tools below. Replace & List output example@example.comom results. Details lists capture groups. Ex+88-01794503784plain describes your expression in plain English.'''\n","pattern = r\"((?:\\+88-)?01[3-9]\\d{8})\"\n","result = re.findall(pattern, text)\n","print(result)"],"metadata":{"id":"gmDxIVP0OD4R","colab":{"base_uri":"https://localhost:8080/"},"executionInfo":{"status":"ok","timestamp":1734170546392,"user_tz":-360,"elapsed":395,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"47534464-d986-4289-f92c-35bf15eee2ea"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["['+88-01692105884', '01798210348', '01649305402', '+88-01794503784']\n"]}]},{"cell_type":"markdown","source":["# **`File Handling`**"],"metadata":{"id":"dK5WUlZgZDvE"}},{"cell_type":"markdown","source":["# Reading from and writing to files\n","* Practice Problems: https://www.w3resource.com/python-exercises/file/"],"metadata":{"id":"L0rqeUbuZ5pL"}},{"cell_type":"markdown","source":["**Reading from a File**\n","\n"],"metadata":{"id":"5cR2evZSa3_i"}},{"cell_type":"code","source":["# Read the Entire File\n","file = open(\"example.txt\", \"r\")\n","content = file.read()\n","print(content)\n","file.close()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"ZtrjKU3id-tB","executionInfo":{"status":"ok","timestamp":1734101796259,"user_tz":-360,"elapsed":395,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"b4d4cec3-de38-4333-bb9e-e356718a2e1d"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["This is a new line of text.\n"]}]},{"cell_type":"code","source":["# Read the Entire File\n","with open(\"example.txt\", \"r\") as file:\n","    content = file.read()\n","    print(content)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"ip3V6lvCZI3N","executionInfo":{"status":"ok","timestamp":1734101799502,"user_tz":-360,"elapsed":411,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"4ad95c81-eab4-4f47-aff1-7dba18f034d0"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["This is a new line of text.\n"]}]},{"cell_type":"code","source":["# Read Line by Line\n","with open(\"example.txt\", \"r\") as file:\n","    for line in file:\n","        print(line.strip())\n","        # print(line,end='')"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"_8yiFXqRbd1Q","executionInfo":{"status":"ok","timestamp":1734101804775,"user_tz":-360,"elapsed":389,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"494147e8-40ce-4cb3-e6cc-96b805852f69"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["This is a new line of text.\n"]}]},{"cell_type":"code","source":["# Read Specific Number of Characters\n","with open(\"example.txt\", \"r\") as file:\n","    content = file.read(10)\n","    print(content)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"k0yevkp4bjW3","executionInfo":{"status":"ok","timestamp":1734101815147,"user_tz":-360,"elapsed":388,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"67f95811-aefe-4866-98b3-bd3f24e71f9e"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["This is a \n"]}]},{"cell_type":"code","source":["# Use readlines() to Read All Lines into a List\n","with open(\"example.txt\", \"r\") as file:\n","    lines = file.readlines()\n","    print(lines)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"wg_BGhOfbqz4","executionInfo":{"status":"ok","timestamp":1734101817888,"user_tz":-360,"elapsed":405,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"ea9df89a-33b6-4a11-dcfd-944ce5ba10fa"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["['This is a new line of text.']\n"]}]},{"cell_type":"markdown","source":["**Writing to a File**"],"metadata":{"id":"Qc3ahbN6bxPo"}},{"cell_type":"code","source":["# Writing Text\n","with open(\"example.txt\", \"w\") as file:\n","    file.write(\"This is a new line of text.\")"],"metadata":{"id":"PqGECZsub3sR"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# Appending Text\n","with open(\"example.txt\", \"a\") as file:\n","    file.write(\"\\nThis text is appended.\")"],"metadata":{"id":"DwrH6SK-b8u6"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["**Example: Reading and Writing**"],"metadata":{"id":"aOedk6hJcgIW"}},{"cell_type":"code","source":["# Writing to a file\n","with open(\"example.txt\", \"w\") as file:\n","    file.write(\"Hello, World!\\n\")\n","    file.write(\"This is Python file handling.\")\n","\n","# Reading from the file\n","with open(\"example.txt\", \"r\") as file:\n","    content = file.read()\n","    print(content)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"RBbDFz11cABi","executionInfo":{"status":"ok","timestamp":1734170982517,"user_tz":-360,"elapsed":418,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"4b7ff06e-ce8d-4402-8c6a-bafeae532a51"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Hello, World!\n","This is Python file handling.\n"]}]},{"cell_type":"code","source":["# Checking if a File Exists\n","import os\n","\n","if os.path.exists(\"/content/example.txt\"):\n","    print(\"File exists.\")\n","else:\n","    print(\"File does not exist.\")"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"NlxTCXBdcxPR","executionInfo":{"status":"ok","timestamp":1734171019666,"user_tz":-360,"elapsed":402,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"c238785a-a06a-42b7-cc52-ad0e6e9cb66d"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["File exists.\n"]}]},{"cell_type":"markdown","source":["**File Operations with Examples**"],"metadata":{"id":"WBtQY752c8IB"}},{"cell_type":"code","source":["# # Copying a File\n","# with open(\"source.txt\", \"w\") as src, open(\"destination.txt\", \"w\") as dest:\n","#     src.write(\"Hello, World!\\n\")\n","#     dest.write(\"Hello, World!\\n\")"],"metadata":{"id":"1QbVou1zhorv"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# Copying a File\n","with open(\"source.txt\", \"r\") as src, open(\"destination.txt\", \"w\") as dest:\n","    data = src.read()\n","    dest.write(data)"],"metadata":{"id":"rJB1WCDWc9f6"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# Counting Words in a File\n","with open(\"example.txt\", \"r\") as file:\n","    content = file.read()\n","    print(content)\n","    words = content.split()\n","    print(words)\n","    print(f\"Number of words: {len(words)}\")"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"MoR76L7idBY5","executionInfo":{"status":"ok","timestamp":1734171175937,"user_tz":-360,"elapsed":379,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"07b952e2-19de-466a-9578-c11baee90814"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Hello, World!\n","This is Python file handling.\n","['Hello,', 'World!', 'This', 'is', 'Python', 'file', 'handling.']\n","Number of words: 7\n"]}]},{"cell_type":"code","source":["# Reading and Reversing Lines\n","with open(\"example.txt\", \"r\") as file:\n","    lines = file.readlines()\n","\n","with open(\"reversed.txt\", \"w\") as file:\n","    for line in reversed(lines):\n","        print(line)\n","        file.write(line)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"8HZMjvVidE6y","executionInfo":{"status":"ok","timestamp":1734171200224,"user_tz":-360,"elapsed":381,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"ec19cc2f-a5a3-490a-b122-1079de45eb0c"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["This is Python file handling.\n","Hello, World!\n","\n"]}]},{"cell_type":"markdown","source":["**Error Handling with File Operations**"],"metadata":{"id":"BPxWzGXUdKZT"}},{"cell_type":"code","source":["try:\n","    with open(\"nonexistent.txt\", \"r\") as file:\n","        content = file.read()\n","except Exception as e:\n","    print(\"File not found!\")"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"NcdP3tP_dLeb","executionInfo":{"status":"ok","timestamp":1734171250318,"user_tz":-360,"elapsed":402,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"7b41ba27-2d09-44b1-99c5-7634b134bd0f"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["File not found!\n"]}]},{"cell_type":"markdown","source":["# **Working with different file types (CSV)**\n","* Practice Problems: https://www.w3resource.com/python-exercises/csv/index.php"],"metadata":{"id":"Dk5jkOO6jjJq"}},{"cell_type":"markdown","source":["**Reading from a File**"],"metadata":{"id":"jwQH68Lan6ri"}},{"cell_type":"code","source":["# Using csv.reader\n","import csv\n","with open(\"data.csv\", \"r\") as file:\n","    reader = csv.reader(file)\n","    for row in reader:\n","        print(row)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"rUo-ZiGZjm3a","executionInfo":{"status":"ok","timestamp":1734102025580,"user_tz":-360,"elapsed":383,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"8cfe3d47-8281-467f-e820-b474ae4b70e4"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["['Name', 'Age', 'City']\n","['Alice', '30', 'New York']\n","['Bob', '25', 'Los Angeles']\n"]}]},{"cell_type":"code","source":["# Reading CSV into a List\n","import csv\n","with open(\"data.csv\", \"r\") as file:\n","    reader = csv.reader(file)\n","    data = list(reader)\n","    print(data)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"TPOvmAJen2YK","executionInfo":{"status":"ok","timestamp":1734102029188,"user_tz":-360,"elapsed":396,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"b42a6e55-f66f-4e8f-c163-f07e9662abae"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["[['Name', 'Age', 'City'], ['Alice', '30', 'New York'], ['Bob', '25', 'Los Angeles']]\n"]}]},{"cell_type":"code","source":["# Using DictReader\n","# Reads each row as a dictionary where the column headers are the keys.\n","import csv\n","with open(\"data.csv\", \"r\") as file:\n","    reader = csv.DictReader(file)\n","    for row in reader:\n","        print(row)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"iCgQQ5vSoG9U","executionInfo":{"status":"ok","timestamp":1734159148983,"user_tz":-360,"elapsed":521,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"6372f9b8-daba-4220-81f5-d8dd2a4a67a8"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["{'Name': 'Alice', 'Age': '30', 'City': 'New York'}\n","{'Name': 'Bob', 'Age': '25', 'City': 'Los Angeles'}\n"]}]},{"cell_type":"markdown","source":["**Writing to CSV Files**"],"metadata":{"id":"8UXIzaEKontv"}},{"cell_type":"code","source":["# Using csv.writer\n","import csv\n","data = [[\"Name\", \"Age\", \"City\"], [\"Alice\", 30, \"New York\"], [\"Bob\", 25, \"Los Angeles\"]]\n","with open(\"data.csv\", \"w\", newline=\"\") as file:\n","    writer = csv.writer(file)\n","    writer.writerows(data)"],"metadata":{"id":"hWHeCDq9oWie"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# Using DictWriter\n","import csv\n","data = [{\"Name\": \"Alice\", \"Age\": 30, \"City\": \"New York\"}, {\"Name\": \"Bob\", \"Age\": 25, \"City\": \"Los Angeles\"}]\n","with open(\"output.csv\", \"w\", newline=\"\") as file:\n","    fieldnames = [\"Name\", \"Age\", \"City\"]\n","    writer = csv.DictWriter(file, fieldnames=fieldnames)\n","\n","    writer.writeheader()  # Write column headers\n","    writer.writerows(data)  # Write rows"],"metadata":{"id":"K5KCnht-o3mq"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["**Working with CSV Using pandas**"],"metadata":{"id":"y4_PW61JpIjr"}},{"cell_type":"code","source":["# Reading CSV\n","import pandas as pd\n","df = pd.read_csv(\"data.csv\")\n","print(df)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"gOT9ijeWpJwE","executionInfo":{"status":"ok","timestamp":1734171616623,"user_tz":-360,"elapsed":333,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"84762a10-18a5-4731-e281-0607d9f39f2e"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["    Name  Age         City\n","0  Alice   30     New York\n","1    Bob   25  Los Angeles\n"]}]},{"cell_type":"code","source":["# Writing CSV\n","import pandas as pd\n","data = {\"Name\": [\"Alice\", \"Bob\"], \"Age\": [30, 25], \"City\": [\"New York\", \"Los Angeles\"]}\n","df = pd.DataFrame(data)\n","df.to_csv(\"data.csv\", index=False)  # `index=False` avoids adding row numbers"],"metadata":{"id":"84299D4SpPkk"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["# Hands-On Project: Reading and Writing Data"],"metadata":{"id":"YpMF_l7Jx6wM"}},{"cell_type":"code","source":["with open(\"data.txt\", \"w\") as file:\n","    file.write(\"Arif,25,Dhaka\\nNayem,30,Barisal\\nRajib,22,Khulna\")"],"metadata":{"id":"cuKEFC6Njz2J"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["import csv\n","\n","# Step 1: Reading data from a text file\n","with open(\"data.txt\", \"r\") as file:\n","    lines = file.readlines()\n","\n","print(\"Original Data:\")\n","for line in lines:\n","    print(line.strip())\n","\n","# Step 2: Processing the data\n","data = []\n","for line in lines:\n","    print(line.strip().split(\",\"))\n","    name, age, city = line.strip().split(\",\")\n","    data.append({\"Name\": name, \"Age\": int(age), \"City\": city})\n","\n","print(\"\\nProcessed Data:\")\n","for entry in data:\n","    print(entry)\n","\n","# Step 3: Writing data to a CSV file\n","with open(\"output.csv\", \"w\", newline=\"\") as file:\n","    fieldnames = [\"Name\", \"Age\", \"City\"]\n","    writer = csv.DictWriter(file, fieldnames=fieldnames)\n","\n","    writer.writeheader()\n","    writer.writerows(data)\n","\n","print(\"\\nData has been written to output.csv\")"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"kLS4fqkOxl24","executionInfo":{"status":"ok","timestamp":1734171841555,"user_tz":-360,"elapsed":357,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"01d3c27c-d0e7-4130-a568-18822b1ea310"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Original Data:\n","Arif,25,Dhaka\n","Nayem,30,Barisal\n","Rajib,22,Khulna\n","['Arif', '25', 'Dhaka']\n","['Nayem', '30', 'Barisal']\n","['Rajib', '22', 'Khulna']\n","\n","Processed Data:\n","{'Name': 'Arif', 'Age': 25, 'City': 'Dhaka'}\n","{'Name': 'Nayem', 'Age': 30, 'City': 'Barisal'}\n","{'Name': 'Rajib', 'Age': 22, 'City': 'Khulna'}\n","\n","Data has been written to output.csv\n"]}]},{"cell_type":"markdown","source":["# Using pandas for CSV"],"metadata":{"id":"AVjTUSBN2s-N"}},{"cell_type":"code","source":["import pandas as pd\n","df = pd.read_csv('data.csv')\n","df"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":125},"id":"Zmw-fNQg2vYX","executionInfo":{"status":"ok","timestamp":1734102497469,"user_tz":-360,"elapsed":395,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"55cfa065-6b35-49f1-8906-0bbe1eaf0a7c"},"execution_count":null,"outputs":[{"output_type":"execute_result","data":{"text/plain":["    Name  Age         City\n","0  Alice   30     New York\n","1    Bob   25  Los Angeles"],"text/html":["\n","  <div id=\"df-7d3e416e-77bc-4751-bd3b-c3d984752d0e\" class=\"colab-df-container\">\n","    <div>\n","<style scoped>\n","    .dataframe tbody tr th:only-of-type {\n","        vertical-align: middle;\n","    }\n","\n","    .dataframe tbody tr th {\n","        vertical-align: top;\n","    }\n","\n","    .dataframe thead th {\n","        text-align: right;\n","    }\n","</style>\n","<table border=\"1\" class=\"dataframe\">\n","  <thead>\n","    <tr style=\"text-align: right;\">\n","      <th></th>\n","      <th>Name</th>\n","      <th>Age</th>\n","      <th>City</th>\n","    </tr>\n","  </thead>\n","  <tbody>\n","    <tr>\n","      <th>0</th>\n","      <td>Alice</td>\n","      <td>30</td>\n","      <td>New York</td>\n","    </tr>\n","    <tr>\n","      <th>1</th>\n","      <td>Bob</td>\n","      <td>25</td>\n","      <td>Los Angeles</td>\n","    </tr>\n","  </tbody>\n","</table>\n","</div>\n","    <div class=\"colab-df-buttons\">\n","\n","  <div class=\"colab-df-container\">\n","    <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-7d3e416e-77bc-4751-bd3b-c3d984752d0e')\"\n","            title=\"Convert this dataframe to an interactive table.\"\n","            style=\"display:none;\">\n","\n","  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n","    <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n","  </svg>\n","    </button>\n","\n","  <style>\n","    .colab-df-container {\n","      display:flex;\n","      gap: 12px;\n","    }\n","\n","    .colab-df-convert {\n","      background-color: #E8F0FE;\n","      border: none;\n","      border-radius: 50%;\n","      cursor: pointer;\n","      display: none;\n","      fill: #1967D2;\n","      height: 32px;\n","      padding: 0 0 0 0;\n","      width: 32px;\n","    }\n","\n","    .colab-df-convert:hover {\n","      background-color: #E2EBFA;\n","      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n","      fill: #174EA6;\n","    }\n","\n","    .colab-df-buttons div {\n","      margin-bottom: 4px;\n","    }\n","\n","    [theme=dark] .colab-df-convert {\n","      background-color: #3B4455;\n","      fill: #D2E3FC;\n","    }\n","\n","    [theme=dark] .colab-df-convert:hover {\n","      background-color: #434B5C;\n","      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n","      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n","      fill: #FFFFFF;\n","    }\n","  </style>\n","\n","    <script>\n","      const buttonEl =\n","        document.querySelector('#df-7d3e416e-77bc-4751-bd3b-c3d984752d0e button.colab-df-convert');\n","      buttonEl.style.display =\n","        google.colab.kernel.accessAllowed ? 'block' : 'none';\n","\n","      async function convertToInteractive(key) {\n","        const element = document.querySelector('#df-7d3e416e-77bc-4751-bd3b-c3d984752d0e');\n","        const dataTable =\n","          await google.colab.kernel.invokeFunction('convertToInteractive',\n","                                                    [key], {});\n","        if (!dataTable) return;\n","\n","        const docLinkHtml = 'Like what you see? Visit the ' +\n","          '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n","          + ' to learn more about interactive tables.';\n","        element.innerHTML = '';\n","        dataTable['output_type'] = 'display_data';\n","        await google.colab.output.renderOutput(dataTable, element);\n","        const docLink = document.createElement('div');\n","        docLink.innerHTML = docLinkHtml;\n","        element.appendChild(docLink);\n","      }\n","    </script>\n","  </div>\n","\n","\n","<div id=\"df-f53471d5-127c-4be5-8e28-6b83e670bd40\">\n","  <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-f53471d5-127c-4be5-8e28-6b83e670bd40')\"\n","            title=\"Suggest charts\"\n","            style=\"display:none;\">\n","\n","<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n","     width=\"24px\">\n","    <g>\n","        <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n","    </g>\n","</svg>\n","  </button>\n","\n","<style>\n","  .colab-df-quickchart {\n","      --bg-color: #E8F0FE;\n","      --fill-color: #1967D2;\n","      --hover-bg-color: #E2EBFA;\n","      --hover-fill-color: #174EA6;\n","      --disabled-fill-color: #AAA;\n","      --disabled-bg-color: #DDD;\n","  }\n","\n","  [theme=dark] .colab-df-quickchart {\n","      --bg-color: #3B4455;\n","      --fill-color: #D2E3FC;\n","      --hover-bg-color: #434B5C;\n","      --hover-fill-color: #FFFFFF;\n","      --disabled-bg-color: #3B4455;\n","      --disabled-fill-color: #666;\n","  }\n","\n","  .colab-df-quickchart {\n","    background-color: var(--bg-color);\n","    border: none;\n","    border-radius: 50%;\n","    cursor: pointer;\n","    display: none;\n","    fill: var(--fill-color);\n","    height: 32px;\n","    padding: 0;\n","    width: 32px;\n","  }\n","\n","  .colab-df-quickchart:hover {\n","    background-color: var(--hover-bg-color);\n","    box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n","    fill: var(--button-hover-fill-color);\n","  }\n","\n","  .colab-df-quickchart-complete:disabled,\n","  .colab-df-quickchart-complete:disabled:hover {\n","    background-color: var(--disabled-bg-color);\n","    fill: var(--disabled-fill-color);\n","    box-shadow: none;\n","  }\n","\n","  .colab-df-spinner {\n","    border: 2px solid var(--fill-color);\n","    border-color: transparent;\n","    border-bottom-color: var(--fill-color);\n","    animation:\n","      spin 1s steps(1) infinite;\n","  }\n","\n","  @keyframes spin {\n","    0% {\n","      border-color: transparent;\n","      border-bottom-color: var(--fill-color);\n","      border-left-color: var(--fill-color);\n","    }\n","    20% {\n","      border-color: transparent;\n","      border-left-color: var(--fill-color);\n","      border-top-color: var(--fill-color);\n","    }\n","    30% {\n","      border-color: transparent;\n","      border-left-color: var(--fill-color);\n","      border-top-color: var(--fill-color);\n","      border-right-color: var(--fill-color);\n","    }\n","    40% {\n","      border-color: transparent;\n","      border-right-color: var(--fill-color);\n","      border-top-color: var(--fill-color);\n","    }\n","    60% {\n","      border-color: transparent;\n","      border-right-color: var(--fill-color);\n","    }\n","    80% {\n","      border-color: transparent;\n","      border-right-color: var(--fill-color);\n","      border-bottom-color: var(--fill-color);\n","    }\n","    90% {\n","      border-color: transparent;\n","      border-bottom-color: var(--fill-color);\n","    }\n","  }\n","</style>\n","\n","  <script>\n","    async function quickchart(key) {\n","      const quickchartButtonEl =\n","        document.querySelector('#' + key + ' button');\n","      quickchartButtonEl.disabled = true;  // To prevent multiple clicks.\n","      quickchartButtonEl.classList.add('colab-df-spinner');\n","      try {\n","        const charts = await google.colab.kernel.invokeFunction(\n","            'suggestCharts', [key], {});\n","      } catch (error) {\n","        console.error('Error during call to suggestCharts:', error);\n","      }\n","      quickchartButtonEl.classList.remove('colab-df-spinner');\n","      quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n","    }\n","    (() => {\n","      let quickchartButtonEl =\n","        document.querySelector('#df-f53471d5-127c-4be5-8e28-6b83e670bd40 button');\n","      quickchartButtonEl.style.display =\n","        google.colab.kernel.accessAllowed ? 'block' : 'none';\n","    })();\n","  </script>\n","</div>\n","\n","  <div id=\"id_7a596856-ed6c-4f79-a49c-a1e6c260711c\">\n","    <style>\n","      .colab-df-generate {\n","        background-color: #E8F0FE;\n","        border: none;\n","        border-radius: 50%;\n","        cursor: pointer;\n","        display: none;\n","        fill: #1967D2;\n","        height: 32px;\n","        padding: 0 0 0 0;\n","        width: 32px;\n","      }\n","\n","      .colab-df-generate:hover {\n","        background-color: #E2EBFA;\n","        box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n","        fill: #174EA6;\n","      }\n","\n","      [theme=dark] .colab-df-generate {\n","        background-color: #3B4455;\n","        fill: #D2E3FC;\n","      }\n","\n","      [theme=dark] .colab-df-generate:hover {\n","        background-color: #434B5C;\n","        box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n","        filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n","        fill: #FFFFFF;\n","      }\n","    </style>\n","    <button class=\"colab-df-generate\" onclick=\"generateWithVariable('df')\"\n","            title=\"Generate code using this dataframe.\"\n","            style=\"display:none;\">\n","\n","  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n","       width=\"24px\">\n","    <path d=\"M7,19H8.4L18.45,9,17,7.55,7,17.6ZM5,21V16.75L18.45,3.32a2,2,0,0,1,2.83,0l1.4,1.43a1.91,1.91,0,0,1,.58,1.4,1.91,1.91,0,0,1-.58,1.4L9.25,21ZM18.45,9,17,7.55Zm-12,3A5.31,5.31,0,0,0,4.9,8.1,5.31,5.31,0,0,0,1,6.5,5.31,5.31,0,0,0,4.9,4.9,5.31,5.31,0,0,0,6.5,1,5.31,5.31,0,0,0,8.1,4.9,5.31,5.31,0,0,0,12,6.5,5.46,5.46,0,0,0,6.5,12Z\"/>\n","  </svg>\n","    </button>\n","    <script>\n","      (() => {\n","      const buttonEl =\n","        document.querySelector('#id_7a596856-ed6c-4f79-a49c-a1e6c260711c button.colab-df-generate');\n","      buttonEl.style.display =\n","        google.colab.kernel.accessAllowed ? 'block' : 'none';\n","\n","      buttonEl.onclick = () => {\n","        google.colab.notebook.generateWithVariable('df');\n","      }\n","      })();\n","    </script>\n","  </div>\n","\n","    </div>\n","  </div>\n"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"dataframe","variable_name":"df","summary":"{\n  \"name\": \"df\",\n  \"rows\": 2,\n  \"fields\": [\n    {\n      \"column\": \"Name\",\n      \"properties\": {\n        \"dtype\": \"string\",\n        \"num_unique_values\": 2,\n        \"samples\": [\n          \"Bob\",\n          \"Alice\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Age\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 3,\n        \"min\": 25,\n        \"max\": 30,\n        \"num_unique_values\": 2,\n        \"samples\": [\n          25,\n          30\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"City\",\n      \"properties\": {\n        \"dtype\": \"string\",\n        \"num_unique_values\": 2,\n        \"samples\": [\n          \"Los Angeles\",\n          \"New York\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    }\n  ]\n}"}},"metadata":{},"execution_count":81}]},{"cell_type":"code","source":["# Display the first few rows\n","print(df.head())"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"-AVdtSyv2_WS","executionInfo":{"status":"ok","timestamp":1734102499310,"user_tz":-360,"elapsed":3,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"38984caa-1985-452a-f1b7-5c054c4922e7"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["    Name  Age         City\n","0  Alice   30     New York\n","1    Bob   25  Los Angeles\n"]}]},{"cell_type":"code","source":["# Display basic info about the dataset\n","print(df.info())"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"cVyOBWva3Cq7","executionInfo":{"status":"ok","timestamp":1734102504131,"user_tz":-360,"elapsed":439,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"95d1b99e-d7af-42ae-fc3b-12188ebdbbc1"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["<class 'pandas.core.frame.DataFrame'>\n","RangeIndex: 2 entries, 0 to 1\n","Data columns (total 3 columns):\n"," #   Column  Non-Null Count  Dtype \n","---  ------  --------------  ----- \n"," 0   Name    2 non-null      object\n"," 1   Age     2 non-null      int64 \n"," 2   City    2 non-null      object\n","dtypes: int64(1), object(2)\n","memory usage: 176.0+ bytes\n","None\n"]}]},{"cell_type":"code","source":["# Summary statistics\n","print(df.describe())"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"cFpdNlF73__5","executionInfo":{"status":"ok","timestamp":1734102508862,"user_tz":-360,"elapsed":373,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"8b97eca5-dd0e-475a-bf8a-21e2a080e4ee"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["             Age\n","count   2.000000\n","mean   27.500000\n","std     3.535534\n","min    25.000000\n","25%    26.250000\n","50%    27.500000\n","75%    28.750000\n","max    30.000000\n"]}]},{"cell_type":"markdown","source":["**Manipulating Data**"],"metadata":{"id":"vLHXxBe84Kli"}},{"cell_type":"code","source":["# Selecting Columns\n","selected_column = df['Name']\n","print(selected_column)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"3k3TIZnv4IMi","executionInfo":{"status":"ok","timestamp":1734102514845,"user_tz":-360,"elapsed":384,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"57b85c4c-b2fe-4e61-a02a-214a027fa8a8"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["0    Alice\n","1      Bob\n","Name: Name, dtype: object\n"]}]},{"cell_type":"code","source":["# Filtering Rows\n","filtered_df = df[df['Age'] > 20]\n","print(filtered_df)"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"F3aApGde4bUM","executionInfo":{"status":"ok","timestamp":1734102625153,"user_tz":-360,"elapsed":381,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"2988493f-c52a-4f1a-b488-3832f53a3000"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["    Name  Age         City\n","0  Alice   30     New York\n","1    Bob   25  Los Angeles\n"]}]},{"cell_type":"code","source":["# Adding/Modifying Columns\n","df['new_column'] = df['Name'] + df['City']\n","df"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":125},"id":"zVxF6iqJ4upV","executionInfo":{"status":"ok","timestamp":1734102637586,"user_tz":-360,"elapsed":399,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"ac2a960e-2458-406a-a312-96a0c23a8ced"},"execution_count":null,"outputs":[{"output_type":"execute_result","data":{"text/plain":["    Name  Age         City      new_column\n","0  Alice   30     New York   AliceNew York\n","1    Bob   25  Los Angeles  BobLos Angeles"],"text/html":["\n","  <div id=\"df-857da180-26db-4794-927f-0dae834454fd\" class=\"colab-df-container\">\n","    <div>\n","<style scoped>\n","    .dataframe tbody tr th:only-of-type {\n","        vertical-align: middle;\n","    }\n","\n","    .dataframe tbody tr th {\n","        vertical-align: top;\n","    }\n","\n","    .dataframe thead th {\n","        text-align: right;\n","    }\n","</style>\n","<table border=\"1\" class=\"dataframe\">\n","  <thead>\n","    <tr style=\"text-align: right;\">\n","      <th></th>\n","      <th>Name</th>\n","      <th>Age</th>\n","      <th>City</th>\n","      <th>new_column</th>\n","    </tr>\n","  </thead>\n","  <tbody>\n","    <tr>\n","      <th>0</th>\n","      <td>Alice</td>\n","      <td>30</td>\n","      <td>New York</td>\n","      <td>AliceNew York</td>\n","    </tr>\n","    <tr>\n","      <th>1</th>\n","      <td>Bob</td>\n","      <td>25</td>\n","      <td>Los Angeles</td>\n","      <td>BobLos Angeles</td>\n","    </tr>\n","  </tbody>\n","</table>\n","</div>\n","    <div class=\"colab-df-buttons\">\n","\n","  <div class=\"colab-df-container\">\n","    <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-857da180-26db-4794-927f-0dae834454fd')\"\n","            title=\"Convert this dataframe to an interactive table.\"\n","            style=\"display:none;\">\n","\n","  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n","    <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n","  </svg>\n","    </button>\n","\n","  <style>\n","    .colab-df-container {\n","      display:flex;\n","      gap: 12px;\n","    }\n","\n","    .colab-df-convert {\n","      background-color: #E8F0FE;\n","      border: none;\n","      border-radius: 50%;\n","      cursor: pointer;\n","      display: none;\n","      fill: #1967D2;\n","      height: 32px;\n","      padding: 0 0 0 0;\n","      width: 32px;\n","    }\n","\n","    .colab-df-convert:hover {\n","      background-color: #E2EBFA;\n","      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n","      fill: #174EA6;\n","    }\n","\n","    .colab-df-buttons div {\n","      margin-bottom: 4px;\n","    }\n","\n","    [theme=dark] .colab-df-convert {\n","      background-color: #3B4455;\n","      fill: #D2E3FC;\n","    }\n","\n","    [theme=dark] .colab-df-convert:hover {\n","      background-color: #434B5C;\n","      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n","      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n","      fill: #FFFFFF;\n","    }\n","  </style>\n","\n","    <script>\n","      const buttonEl =\n","        document.querySelector('#df-857da180-26db-4794-927f-0dae834454fd button.colab-df-convert');\n","      buttonEl.style.display =\n","        google.colab.kernel.accessAllowed ? 'block' : 'none';\n","\n","      async function convertToInteractive(key) {\n","        const element = document.querySelector('#df-857da180-26db-4794-927f-0dae834454fd');\n","        const dataTable =\n","          await google.colab.kernel.invokeFunction('convertToInteractive',\n","                                                    [key], {});\n","        if (!dataTable) return;\n","\n","        const docLinkHtml = 'Like what you see? Visit the ' +\n","          '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n","          + ' to learn more about interactive tables.';\n","        element.innerHTML = '';\n","        dataTable['output_type'] = 'display_data';\n","        await google.colab.output.renderOutput(dataTable, element);\n","        const docLink = document.createElement('div');\n","        docLink.innerHTML = docLinkHtml;\n","        element.appendChild(docLink);\n","      }\n","    </script>\n","  </div>\n","\n","\n","<div id=\"df-3539aa54-1390-4bc3-ac6e-1e918c279762\">\n","  <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-3539aa54-1390-4bc3-ac6e-1e918c279762')\"\n","            title=\"Suggest charts\"\n","            style=\"display:none;\">\n","\n","<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n","     width=\"24px\">\n","    <g>\n","        <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n","    </g>\n","</svg>\n","  </button>\n","\n","<style>\n","  .colab-df-quickchart {\n","      --bg-color: #E8F0FE;\n","      --fill-color: #1967D2;\n","      --hover-bg-color: #E2EBFA;\n","      --hover-fill-color: #174EA6;\n","      --disabled-fill-color: #AAA;\n","      --disabled-bg-color: #DDD;\n","  }\n","\n","  [theme=dark] .colab-df-quickchart {\n","      --bg-color: #3B4455;\n","      --fill-color: #D2E3FC;\n","      --hover-bg-color: #434B5C;\n","      --hover-fill-color: #FFFFFF;\n","      --disabled-bg-color: #3B4455;\n","      --disabled-fill-color: #666;\n","  }\n","\n","  .colab-df-quickchart {\n","    background-color: var(--bg-color);\n","    border: none;\n","    border-radius: 50%;\n","    cursor: pointer;\n","    display: none;\n","    fill: var(--fill-color);\n","    height: 32px;\n","    padding: 0;\n","    width: 32px;\n","  }\n","\n","  .colab-df-quickchart:hover {\n","    background-color: var(--hover-bg-color);\n","    box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n","    fill: var(--button-hover-fill-color);\n","  }\n","\n","  .colab-df-quickchart-complete:disabled,\n","  .colab-df-quickchart-complete:disabled:hover {\n","    background-color: var(--disabled-bg-color);\n","    fill: var(--disabled-fill-color);\n","    box-shadow: none;\n","  }\n","\n","  .colab-df-spinner {\n","    border: 2px solid var(--fill-color);\n","    border-color: transparent;\n","    border-bottom-color: var(--fill-color);\n","    animation:\n","      spin 1s steps(1) infinite;\n","  }\n","\n","  @keyframes spin {\n","    0% {\n","      border-color: transparent;\n","      border-bottom-color: var(--fill-color);\n","      border-left-color: var(--fill-color);\n","    }\n","    20% {\n","      border-color: transparent;\n","      border-left-color: var(--fill-color);\n","      border-top-color: var(--fill-color);\n","    }\n","    30% {\n","      border-color: transparent;\n","      border-left-color: var(--fill-color);\n","      border-top-color: var(--fill-color);\n","      border-right-color: var(--fill-color);\n","    }\n","    40% {\n","      border-color: transparent;\n","      border-right-color: var(--fill-color);\n","      border-top-color: var(--fill-color);\n","    }\n","    60% {\n","      border-color: transparent;\n","      border-right-color: var(--fill-color);\n","    }\n","    80% {\n","      border-color: transparent;\n","      border-right-color: var(--fill-color);\n","      border-bottom-color: var(--fill-color);\n","    }\n","    90% {\n","      border-color: transparent;\n","      border-bottom-color: var(--fill-color);\n","    }\n","  }\n","</style>\n","\n","  <script>\n","    async function quickchart(key) {\n","      const quickchartButtonEl =\n","        document.querySelector('#' + key + ' button');\n","      quickchartButtonEl.disabled = true;  // To prevent multiple clicks.\n","      quickchartButtonEl.classList.add('colab-df-spinner');\n","      try {\n","        const charts = await google.colab.kernel.invokeFunction(\n","            'suggestCharts', [key], {});\n","      } catch (error) {\n","        console.error('Error during call to suggestCharts:', error);\n","      }\n","      quickchartButtonEl.classList.remove('colab-df-spinner');\n","      quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n","    }\n","    (() => {\n","      let quickchartButtonEl =\n","        document.querySelector('#df-3539aa54-1390-4bc3-ac6e-1e918c279762 button');\n","      quickchartButtonEl.style.display =\n","        google.colab.kernel.accessAllowed ? 'block' : 'none';\n","    })();\n","  </script>\n","</div>\n","\n","  <div id=\"id_93e2f479-49f9-4f6d-ad74-c474c95522ae\">\n","    <style>\n","      .colab-df-generate {\n","        background-color: #E8F0FE;\n","        border: none;\n","        border-radius: 50%;\n","        cursor: pointer;\n","        display: none;\n","        fill: #1967D2;\n","        height: 32px;\n","        padding: 0 0 0 0;\n","        width: 32px;\n","      }\n","\n","      .colab-df-generate:hover {\n","        background-color: #E2EBFA;\n","        box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n","        fill: #174EA6;\n","      }\n","\n","      [theme=dark] .colab-df-generate {\n","        background-color: #3B4455;\n","        fill: #D2E3FC;\n","      }\n","\n","      [theme=dark] .colab-df-generate:hover {\n","        background-color: #434B5C;\n","        box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n","        filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n","        fill: #FFFFFF;\n","      }\n","    </style>\n","    <button class=\"colab-df-generate\" onclick=\"generateWithVariable('df')\"\n","            title=\"Generate code using this dataframe.\"\n","            style=\"display:none;\">\n","\n","  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n","       width=\"24px\">\n","    <path d=\"M7,19H8.4L18.45,9,17,7.55,7,17.6ZM5,21V16.75L18.45,3.32a2,2,0,0,1,2.83,0l1.4,1.43a1.91,1.91,0,0,1,.58,1.4,1.91,1.91,0,0,1-.58,1.4L9.25,21ZM18.45,9,17,7.55Zm-12,3A5.31,5.31,0,0,0,4.9,8.1,5.31,5.31,0,0,0,1,6.5,5.31,5.31,0,0,0,4.9,4.9,5.31,5.31,0,0,0,6.5,1,5.31,5.31,0,0,0,8.1,4.9,5.31,5.31,0,0,0,12,6.5,5.46,5.46,0,0,0,6.5,12Z\"/>\n","  </svg>\n","    </button>\n","    <script>\n","      (() => {\n","      const buttonEl =\n","        document.querySelector('#id_93e2f479-49f9-4f6d-ad74-c474c95522ae button.colab-df-generate');\n","      buttonEl.style.display =\n","        google.colab.kernel.accessAllowed ? 'block' : 'none';\n","\n","      buttonEl.onclick = () => {\n","        google.colab.notebook.generateWithVariable('df');\n","      }\n","      })();\n","    </script>\n","  </div>\n","\n","    </div>\n","  </div>\n"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"dataframe","variable_name":"df","summary":"{\n  \"name\": \"df\",\n  \"rows\": 2,\n  \"fields\": [\n    {\n      \"column\": \"Name\",\n      \"properties\": {\n        \"dtype\": \"string\",\n        \"num_unique_values\": 2,\n        \"samples\": [\n          \"Bob\",\n          \"Alice\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Age\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 3,\n        \"min\": 25,\n        \"max\": 30,\n        \"num_unique_values\": 2,\n        \"samples\": [\n          25,\n          30\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"City\",\n      \"properties\": {\n        \"dtype\": \"string\",\n        \"num_unique_values\": 2,\n        \"samples\": [\n          \"Los Angeles\",\n          \"New York\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"new_column\",\n      \"properties\": {\n        \"dtype\": \"string\",\n        \"num_unique_values\": 2,\n        \"samples\": [\n          \"BobLos Angeles\",\n          \"AliceNew York\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    }\n  ]\n}"}},"metadata":{},"execution_count":88}]},{"cell_type":"code","source":["# Dropping Columns\n","df.drop('new_column', axis=1, inplace=True)\n","df"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":125},"id":"us-WJzRm5EhJ","executionInfo":{"status":"ok","timestamp":1734102644178,"user_tz":-360,"elapsed":444,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"112cb3e8-7b90-4ad5-c9f0-46df7269daee"},"execution_count":null,"outputs":[{"output_type":"execute_result","data":{"text/plain":["    Name  Age         City\n","0  Alice   30     New York\n","1    Bob   25  Los Angeles"],"text/html":["\n","  <div id=\"df-b1dedfc6-bb44-4743-a088-9510d4b1d301\" class=\"colab-df-container\">\n","    <div>\n","<style scoped>\n","    .dataframe tbody tr th:only-of-type {\n","        vertical-align: middle;\n","    }\n","\n","    .dataframe tbody tr th {\n","        vertical-align: top;\n","    }\n","\n","    .dataframe thead th {\n","        text-align: right;\n","    }\n","</style>\n","<table border=\"1\" class=\"dataframe\">\n","  <thead>\n","    <tr style=\"text-align: right;\">\n","      <th></th>\n","      <th>Name</th>\n","      <th>Age</th>\n","      <th>City</th>\n","    </tr>\n","  </thead>\n","  <tbody>\n","    <tr>\n","      <th>0</th>\n","      <td>Alice</td>\n","      <td>30</td>\n","      <td>New York</td>\n","    </tr>\n","    <tr>\n","      <th>1</th>\n","      <td>Bob</td>\n","      <td>25</td>\n","      <td>Los Angeles</td>\n","    </tr>\n","  </tbody>\n","</table>\n","</div>\n","    <div class=\"colab-df-buttons\">\n","\n","  <div class=\"colab-df-container\">\n","    <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-b1dedfc6-bb44-4743-a088-9510d4b1d301')\"\n","            title=\"Convert this dataframe to an interactive table.\"\n","            style=\"display:none;\">\n","\n","  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n","    <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n","  </svg>\n","    </button>\n","\n","  <style>\n","    .colab-df-container {\n","      display:flex;\n","      gap: 12px;\n","    }\n","\n","    .colab-df-convert {\n","      background-color: #E8F0FE;\n","      border: none;\n","      border-radius: 50%;\n","      cursor: pointer;\n","      display: none;\n","      fill: #1967D2;\n","      height: 32px;\n","      padding: 0 0 0 0;\n","      width: 32px;\n","    }\n","\n","    .colab-df-convert:hover {\n","      background-color: #E2EBFA;\n","      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n","      fill: #174EA6;\n","    }\n","\n","    .colab-df-buttons div {\n","      margin-bottom: 4px;\n","    }\n","\n","    [theme=dark] .colab-df-convert {\n","      background-color: #3B4455;\n","      fill: #D2E3FC;\n","    }\n","\n","    [theme=dark] .colab-df-convert:hover {\n","      background-color: #434B5C;\n","      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n","      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n","      fill: #FFFFFF;\n","    }\n","  </style>\n","\n","    <script>\n","      const buttonEl =\n","        document.querySelector('#df-b1dedfc6-bb44-4743-a088-9510d4b1d301 button.colab-df-convert');\n","      buttonEl.style.display =\n","        google.colab.kernel.accessAllowed ? 'block' : 'none';\n","\n","      async function convertToInteractive(key) {\n","        const element = document.querySelector('#df-b1dedfc6-bb44-4743-a088-9510d4b1d301');\n","        const dataTable =\n","          await google.colab.kernel.invokeFunction('convertToInteractive',\n","                                                    [key], {});\n","        if (!dataTable) return;\n","\n","        const docLinkHtml = 'Like what you see? Visit the ' +\n","          '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n","          + ' to learn more about interactive tables.';\n","        element.innerHTML = '';\n","        dataTable['output_type'] = 'display_data';\n","        await google.colab.output.renderOutput(dataTable, element);\n","        const docLink = document.createElement('div');\n","        docLink.innerHTML = docLinkHtml;\n","        element.appendChild(docLink);\n","      }\n","    </script>\n","  </div>\n","\n","\n","<div id=\"df-834c0a91-a5f9-4463-b032-75df424a8c75\">\n","  <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-834c0a91-a5f9-4463-b032-75df424a8c75')\"\n","            title=\"Suggest charts\"\n","            style=\"display:none;\">\n","\n","<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n","     width=\"24px\">\n","    <g>\n","        <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n","    </g>\n","</svg>\n","  </button>\n","\n","<style>\n","  .colab-df-quickchart {\n","      --bg-color: #E8F0FE;\n","      --fill-color: #1967D2;\n","      --hover-bg-color: #E2EBFA;\n","      --hover-fill-color: #174EA6;\n","      --disabled-fill-color: #AAA;\n","      --disabled-bg-color: #DDD;\n","  }\n","\n","  [theme=dark] .colab-df-quickchart {\n","      --bg-color: #3B4455;\n","      --fill-color: #D2E3FC;\n","      --hover-bg-color: #434B5C;\n","      --hover-fill-color: #FFFFFF;\n","      --disabled-bg-color: #3B4455;\n","      --disabled-fill-color: #666;\n","  }\n","\n","  .colab-df-quickchart {\n","    background-color: var(--bg-color);\n","    border: none;\n","    border-radius: 50%;\n","    cursor: pointer;\n","    display: none;\n","    fill: var(--fill-color);\n","    height: 32px;\n","    padding: 0;\n","    width: 32px;\n","  }\n","\n","  .colab-df-quickchart:hover {\n","    background-color: var(--hover-bg-color);\n","    box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n","    fill: var(--button-hover-fill-color);\n","  }\n","\n","  .colab-df-quickchart-complete:disabled,\n","  .colab-df-quickchart-complete:disabled:hover {\n","    background-color: var(--disabled-bg-color);\n","    fill: var(--disabled-fill-color);\n","    box-shadow: none;\n","  }\n","\n","  .colab-df-spinner {\n","    border: 2px solid var(--fill-color);\n","    border-color: transparent;\n","    border-bottom-color: var(--fill-color);\n","    animation:\n","      spin 1s steps(1) infinite;\n","  }\n","\n","  @keyframes spin {\n","    0% {\n","      border-color: transparent;\n","      border-bottom-color: var(--fill-color);\n","      border-left-color: var(--fill-color);\n","    }\n","    20% {\n","      border-color: transparent;\n","      border-left-color: var(--fill-color);\n","      border-top-color: var(--fill-color);\n","    }\n","    30% {\n","      border-color: transparent;\n","      border-left-color: var(--fill-color);\n","      border-top-color: var(--fill-color);\n","      border-right-color: var(--fill-color);\n","    }\n","    40% {\n","      border-color: transparent;\n","      border-right-color: var(--fill-color);\n","      border-top-color: var(--fill-color);\n","    }\n","    60% {\n","      border-color: transparent;\n","      border-right-color: var(--fill-color);\n","    }\n","    80% {\n","      border-color: transparent;\n","      border-right-color: var(--fill-color);\n","      border-bottom-color: var(--fill-color);\n","    }\n","    90% {\n","      border-color: transparent;\n","      border-bottom-color: var(--fill-color);\n","    }\n","  }\n","</style>\n","\n","  <script>\n","    async function quickchart(key) {\n","      const quickchartButtonEl =\n","        document.querySelector('#' + key + ' button');\n","      quickchartButtonEl.disabled = true;  // To prevent multiple clicks.\n","      quickchartButtonEl.classList.add('colab-df-spinner');\n","      try {\n","        const charts = await google.colab.kernel.invokeFunction(\n","            'suggestCharts', [key], {});\n","      } catch (error) {\n","        console.error('Error during call to suggestCharts:', error);\n","      }\n","      quickchartButtonEl.classList.remove('colab-df-spinner');\n","      quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n","    }\n","    (() => {\n","      let quickchartButtonEl =\n","        document.querySelector('#df-834c0a91-a5f9-4463-b032-75df424a8c75 button');\n","      quickchartButtonEl.style.display =\n","        google.colab.kernel.accessAllowed ? 'block' : 'none';\n","    })();\n","  </script>\n","</div>\n","\n","  <div id=\"id_80feeaba-39ac-49dc-89a2-691b25156adb\">\n","    <style>\n","      .colab-df-generate {\n","        background-color: #E8F0FE;\n","        border: none;\n","        border-radius: 50%;\n","        cursor: pointer;\n","        display: none;\n","        fill: #1967D2;\n","        height: 32px;\n","        padding: 0 0 0 0;\n","        width: 32px;\n","      }\n","\n","      .colab-df-generate:hover {\n","        background-color: #E2EBFA;\n","        box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n","        fill: #174EA6;\n","      }\n","\n","      [theme=dark] .colab-df-generate {\n","        background-color: #3B4455;\n","        fill: #D2E3FC;\n","      }\n","\n","      [theme=dark] .colab-df-generate:hover {\n","        background-color: #434B5C;\n","        box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n","        filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n","        fill: #FFFFFF;\n","      }\n","    </style>\n","    <button class=\"colab-df-generate\" onclick=\"generateWithVariable('df')\"\n","            title=\"Generate code using this dataframe.\"\n","            style=\"display:none;\">\n","\n","  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n","       width=\"24px\">\n","    <path d=\"M7,19H8.4L18.45,9,17,7.55,7,17.6ZM5,21V16.75L18.45,3.32a2,2,0,0,1,2.83,0l1.4,1.43a1.91,1.91,0,0,1,.58,1.4,1.91,1.91,0,0,1-.58,1.4L9.25,21ZM18.45,9,17,7.55Zm-12,3A5.31,5.31,0,0,0,4.9,8.1,5.31,5.31,0,0,0,1,6.5,5.31,5.31,0,0,0,4.9,4.9,5.31,5.31,0,0,0,6.5,1,5.31,5.31,0,0,0,8.1,4.9,5.31,5.31,0,0,0,12,6.5,5.46,5.46,0,0,0,6.5,12Z\"/>\n","  </svg>\n","    </button>\n","    <script>\n","      (() => {\n","      const buttonEl =\n","        document.querySelector('#id_80feeaba-39ac-49dc-89a2-691b25156adb button.colab-df-generate');\n","      buttonEl.style.display =\n","        google.colab.kernel.accessAllowed ? 'block' : 'none';\n","\n","      buttonEl.onclick = () => {\n","        google.colab.notebook.generateWithVariable('df');\n","      }\n","      })();\n","    </script>\n","  </div>\n","\n","    </div>\n","  </div>\n"],"application/vnd.google.colaboratory.intrinsic+json":{"type":"dataframe","variable_name":"df","summary":"{\n  \"name\": \"df\",\n  \"rows\": 2,\n  \"fields\": [\n    {\n      \"column\": \"Name\",\n      \"properties\": {\n        \"dtype\": \"string\",\n        \"num_unique_values\": 2,\n        \"samples\": [\n          \"Bob\",\n          \"Alice\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Age\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 3,\n        \"min\": 25,\n        \"max\": 30,\n        \"num_unique_values\": 2,\n        \"samples\": [\n          25,\n          30\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"City\",\n      \"properties\": {\n        \"dtype\": \"string\",\n        \"num_unique_values\": 2,\n        \"samples\": [\n          \"Los Angeles\",\n          \"New York\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    }\n  ]\n}"}},"metadata":{},"execution_count":89}]},{"cell_type":"code","source":["# Save the modified DataFrame to a new CSV\n","df.to_csv('modified_file.csv', index=False)"],"metadata":{"id":"_g6Nx79B5bpM"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["# Students Database Project With Pandas"],"metadata":{"id":"Qlayisw86qqZ"}},{"cell_type":"code","source":["import pandas as pd\n","import os\n","\n","# File path for the CSV\n","file_path = \"student.csv\"\n","\n","# Create a sample CSV if it doesn't exist\n","if not os.path.exists(file_path):\n","    df = pd.DataFrame(columns=[\"ID\", \"Name\", \"Intake\", \"Department\"])\n","    df.to_csv(file_path, index=False)\n","\n","\n","def read_data():\n","    \"\"\"Read and display the student database.\"\"\"\n","    df = pd.read_csv(file_path)\n","    if df.empty:\n","        print(\"No student data found!\")\n","    else:\n","        print(\"\\nStudent Database:\")\n","        print(df)\n","\n","\n","def create_student():\n","    \"\"\"Add a new student to the database.\"\"\"\n","    df = pd.read_csv(file_path)\n","\n","    # Collect student details\n","    student_id = input(\"Enter Student ID: \").strip()\n","    name = input(\"Enter Name: \").strip()\n","    intake = input(\"Enter Intake: \").strip()\n","    department = input(\"Enter Department: \").strip()\n","\n","    # Check if ID already exists\n","    if student_id in df[\"ID\"].values:\n","        print(\"Error: Student ID already exists.\")\n","        return\n","\n","    # Add to the DataFrame\n","    new_student = pd.DataFrame([{\"ID\": student_id, \"Name\": name, \"Intake\": intake, \"Department\": department}])\n","    df = pd.concat([df, new_student], ignore_index=True)\n","    df.to_csv(file_path, index=False)\n","    print(\"Student added successfully!\")\n","\n","\n","def update_student():\n","    \"\"\"Update details of an existing student.\"\"\"\n","    df = pd.read_csv(file_path)\n","\n","    if df.empty:\n","        print(\"No student data to update!\")\n","        return\n","\n","    student_id = input(\"Enter Student ID to update: \").strip()\n","\n","    # Check if the ID exists\n","    if student_id not in df[\"ID\"].values:\n","        print(\"Student ID not found!\")\n","        return\n","\n","    # Get new details\n","    print(\"Leave field empty to keep existing data.\")\n","    name = input(\"Enter new Name: \").strip()\n","    intake = input(\"Enter new Intake: \").strip()\n","    department = input(\"Enter new Department: \").strip()\n","\n","    # Update DataFrame\n","    if name:\n","        df.loc[df[\"ID\"] == student_id, \"Name\"] = name\n","    if intake:\n","        df.loc[df[\"ID\"] == student_id, \"Intake\"] = intake\n","    if department:\n","        df.loc[df[\"ID\"] == student_id, \"Department\"] = department\n","\n","    df.to_csv(file_path, index=False)\n","    print(\"Student updated successfully!\")\n","\n","\n","def delete_student():\n","    \"\"\"Delete a student from the database.\"\"\"\n","    df = pd.read_csv(file_path)\n","\n","    if df.empty:\n","        print(\"No student data to delete!\")\n","        return\n","\n","    student_id = input(\"Enter Student ID to delete: \").strip()\n","\n","    # Check if the ID exists\n","    if student_id not in df[\"ID\"].values:\n","        print(\"Student ID not found!\")\n","        return\n","\n","    # Remove from DataFrame\n","    df = df[df[\"ID\"] != student_id]\n","    df.to_csv(file_path, index=False)\n","    print(\"Student deleted successfully!\")\n","\n","\n","def main():\n","    \"\"\"Main menu for the Student Database.\"\"\"\n","    while True:\n","        print(\"\\nStudent Database Management\")\n","        print(\"1. View Students\")\n","        print(\"2. Add Student\")\n","        print(\"3. Update Student\")\n","        print(\"4. Delete Student\")\n","        print(\"5. Exit\")\n","\n","        choice = input(\"Enter your choice: \").strip()\n","\n","        if choice == \"1\":\n","            read_data()\n","        elif choice == \"2\":\n","            create_student()\n","        elif choice == \"3\":\n","            update_student()\n","        elif choice == \"4\":\n","            delete_student()\n","        elif choice == \"5\":\n","            print(\"Exiting the program.\")\n","            break\n","        else:\n","            print(\"Invalid choice. Please try again.\")\n","\n","\n","if __name__ == \"__main__\":\n","    main()"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"rCjhFeaQ5ytO","executionInfo":{"status":"ok","timestamp":1734172328584,"user_tz":-360,"elapsed":69543,"user":{"displayName":"44-271-Munsi Walid Al Hassan Nizhu","userId":"16216461530557409787"}},"outputId":"57051875-cf26-47b4-a09e-6a8c157ea332"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["\n","Student Database Management\n","1. View Students\n","2. Add Student\n","3. Update Student\n","4. Delete Student\n","5. Exit\n","Enter your choice: 2\n","Enter Student ID: 71\n","Enter Name: AA\n","Enter Intake: 44\n","Enter Department: CSE\n","Student added successfully!\n","\n","Student Database Management\n","1. View Students\n","2. Add Student\n","3. Update Student\n","4. Delete Student\n","5. Exit\n","Enter your choice: 1\n","\n","Student Database:\n","   ID Name  Intake Department\n","0  71   AA      44        CSE\n","\n","Student Database Management\n","1. View Students\n","2. Add Student\n","3. Update Student\n","4. Delete Student\n","5. Exit\n","Enter your choice: 4\n","Enter Student ID to delete: 1\n","Student ID not found!\n","\n","Student Database Management\n","1. View Students\n","2. Add Student\n","3. Update Student\n","4. Delete Student\n","5. Exit\n","Enter your choice: 5\n","Exiting the program.\n"]}]},{"cell_type":"code","source":[],"metadata":{"id":"ntYUfOhOmv_T"},"execution_count":null,"outputs":[]}]}