Skip to content

Commit 8d4fef6

Browse files
committed
Drop exceptions in advanced string manipulation and restore notes slide type from skip
1 parent 45b3b5d commit 8d4fef6

File tree

2 files changed

+26
-258
lines changed

2 files changed

+26
-258
lines changed

Intermediate.ipynb

Lines changed: 2 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -4848,8 +4848,8 @@
48484848
},
48494849
"source": [
48504850
"These methods help you locate substrings within a string, returning their position or checking for their presence:\n",
4851-
"* `index()` - returns `-1` if the substring is not found\n",
4852-
"* `find()` - raises a `ValueError` if the substring is not found"
4851+
"* `index()`\n",
4852+
"* `find()`"
48534853
]
48544854
},
48554855
{
@@ -4886,38 +4886,6 @@
48864886
"print(line.find('wombat'))"
48874887
]
48884888
},
4889-
{
4890-
"cell_type": "markdown",
4891-
"id": "714fc569",
4892-
"metadata": {
4893-
"editable": false,
4894-
"slideshow": {
4895-
"slide_type": ""
4896-
}
4897-
},
4898-
"source": [
4899-
"The key difference is how they handle missing substrings - `find()` returns `-1` while `index()` raises an exception:"
4900-
]
4901-
},
4902-
{
4903-
"cell_type": "code",
4904-
"execution_count": null,
4905-
"id": "fc5a28ef-8a31-434a-a653-1662cc274945",
4906-
"metadata": {
4907-
"editable": true,
4908-
"slideshow": {
4909-
"slide_type": ""
4910-
},
4911-
"tags": []
4912-
},
4913-
"outputs": [],
4914-
"source": [
4915-
"try:\n",
4916-
" print(line.index('wombat'))\n",
4917-
"except ValueError:\n",
4918-
" print(\"A wombat isn't mentioned in the text\")"
4919-
]
4920-
},
49214889
{
49224890
"cell_type": "markdown",
49234891
"id": "40d4c38b-9a07-4e49-a9b3-003dd13f129d",
@@ -5106,59 +5074,6 @@
51065074
"print(f\"Cleaned: '{email}'\")"
51075075
]
51085076
},
5109-
{
5110-
"cell_type": "markdown",
5111-
"id": "2fd63835",
5112-
"metadata": {
5113-
"editable": false,
5114-
"slideshow": {
5115-
"slide_type": "slide"
5116-
}
5117-
},
5118-
"source": [
5119-
"These methods work with any string and don't raise exceptions, but you may want to validate the results."
5120-
]
5121-
},
5122-
{
5123-
"cell_type": "code",
5124-
"execution_count": null,
5125-
"id": "8f1ceba1",
5126-
"metadata": {},
5127-
"outputs": [],
5128-
"source": [
5129-
"# Transforming methods don't raise exceptions, but you can validate transformed results\n",
5130-
"def process_username(username):\n",
5131-
" \"\"\"Process and validate a username.\"\"\"\n",
5132-
" if not isinstance(username, str):\n",
5133-
" raise TypeError(f\"Username must be a string, got {type(username).__name__}\")\n",
5134-
" \n",
5135-
" # Transform: strip whitespace and convert to lowercase\n",
5136-
" processed = username.strip().lower()\n",
5137-
" \n",
5138-
" # Validate the result\n",
5139-
" if not processed:\n",
5140-
" raise ValueError(\"Username cannot be empty after processing\")\n",
5141-
" \n",
5142-
" if len(processed) < 3:\n",
5143-
" raise ValueError(f\"Username must be at least 3 characters, got {len(processed)}\")\n",
5144-
" \n",
5145-
" return processed\n",
5146-
"\n",
5147-
"# Valid case\n",
5148-
"try:\n",
5149-
" result = process_username(\" JohnDoe \")\n",
5150-
" print(f\"\u2713 Processed username: '{result}'\")\n",
5151-
"except ValueError as e:\n",
5152-
" print(f\"Error: {e}\")\n",
5153-
"\n",
5154-
"# Invalid case\n",
5155-
"try:\n",
5156-
" result = process_username(\" AB \")\n",
5157-
" print(f\"\u2713 Processed username: '{result}'\")\n",
5158-
"except ValueError as e:\n",
5159-
" print(f\"Error: {e}\")"
5160-
]
5161-
},
51625077
{
51635078
"cell_type": "markdown",
51645079
"id": "6cf8076f-8f50-49ae-b558-39336c323880",
@@ -5258,40 +5173,6 @@
52585173
"print(\"\\n\".join(line_list))"
52595174
]
52605175
},
5261-
{
5262-
"cell_type": "markdown",
5263-
"id": "0deaf239",
5264-
"metadata": {
5265-
"editable": false,
5266-
"slideshow": {
5267-
"slide_type": ""
5268-
}
5269-
},
5270-
"source": [
5271-
"`join()` will raise a `TypeError` if the iterable contains non-string elements"
5272-
]
5273-
},
5274-
{
5275-
"cell_type": "code",
5276-
"execution_count": null,
5277-
"id": "225bdd40",
5278-
"metadata": {
5279-
"editable": true,
5280-
"slideshow": {
5281-
"slide_type": ""
5282-
},
5283-
"tags": []
5284-
},
5285-
"outputs": [],
5286-
"source": [
5287-
"flags = [16384,1048576]\n",
5288-
"\n",
5289-
"for i in range(len(flags)):\n",
5290-
" flags_str = \"+\".join(flags[i])\n",
5291-
"#flags_str = \"+\".join(str(flag) for flag in flags)\n",
5292-
"#print(flags_str)"
5293-
]
5294-
},
52955176
{
52965177
"cell_type": "markdown",
52975178
"id": "a965b5d9-59f4-4ed5-b7e9-89f5f0bcf60f",

0 commit comments

Comments
 (0)