Skip to content

Commit

Permalink
Reorganize stuff about string concatenation
Browse files Browse the repository at this point in the history
  • Loading branch information
davewhipp committed Oct 2, 2024
1 parent 93522b6 commit 02316ee
Show file tree
Hide file tree
Showing 3 changed files with 242 additions and 113 deletions.
2 changes: 1 addition & 1 deletion source/back-matter/nb/appendix-6-question-solutions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@
"### Question 2.5\n",
"\n",
"```python\n",
"'Hot62.6'\n",
"ValueError\n",
"```"
]
},
Expand Down
192 changes: 82 additions & 110 deletions source/part1/chapter-02/nb/00-python-basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -908,7 +908,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's also check the type of `tempFahrenheit`. What happens if you try to combine `tempFahrenheit` and `weatherForecast` in a single math equation such as `tempFahrenheit = tempFahrenheit + 5.0 * weatherForecast`?"
"Let's also check the type of `tempFahrenheit`. What happens if you try to combine `tempFahrenheit` and `weatherForecast` in a single math equation such as `tempFahrenheit = tempFahrenheit + weatherForecast`?"
]
},
{
Expand All @@ -928,19 +928,19 @@
"outputs": [
{
"ename": "TypeError",
"evalue": "can't multiply sequence by non-int of type 'float'",
"evalue": "unsupported operand type(s) for +: 'float' and 'str'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[25], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28mtype\u001b[39m(tempFahrenheit)\n\u001b[0;32m----> 2\u001b[0m tempFahrenheit \u001b[38;5;241m=\u001b[39m tempFahrenheit \u001b[38;5;241m+\u001b[39m \u001b[38;5;241;43m5.0\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mweatherForecast\u001b[49m\n",
"\u001b[0;31mTypeError\u001b[0m: can't multiply sequence by non-int of type 'float'"
"Cell \u001b[0;32mIn[25], line 2\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28mtype\u001b[39m(tempFahrenheit)\n\u001b[0;32m----> 2\u001b[0m tempFahrenheit \u001b[38;5;241m=\u001b[39m \u001b[43mtempFahrenheit\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mweatherForecast\u001b[49m\n",
"\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'float' and 'str'"
]
}
],
"source": [
"type(tempFahrenheit)\n",
"tempFahrenheit = tempFahrenheit + 5.0 * weatherForecast"
"tempFahrenheit = tempFahrenheit + weatherForecast"
]
},
{
Expand All @@ -950,7 +950,7 @@
"editable": true
},
"source": [
"In this case we get at `TypeError` because we are trying to execute a math operation with data types that are not compatible. There is no way in Python to multpily decimal values with a character string."
"In this case we get at `TypeError` because we are trying to execute a math operation with data types that are not compatible. It is not possible to add a number directly to a character string in Python. In order for addition to work, the data types need to be compatible with one another."
]
},
{
Expand Down Expand Up @@ -1047,7 +1047,48 @@
"source": [
"## Making different data types work together\n",
"\n",
"In the previous section we saw that not all Python data types are directly compatible with others, which may result in a `TypeError` being raised. This means that (1) it is important to be aware of the data type of variables and (2) some additional steps may be needed to make different data compatible. In the example in the previous section we had `tempFahrenheit` (type `float`) and `weatherForecast` (type `str`) that were not compatible. How can we address this issue if we would like to work with those data together?"
"In the previous section we saw that not all Python data types are directly compatible with others, which may result in a `TypeError` being raised. This means that (1) it is important to be aware of the data type of variables and (2) some additional steps may be needed to make different data compatible. Let's consider an example where we try to combine `tempFahrenheit` (type `float`) with another temperature value. In this case, the other temperature is stored in a character string `forecastHighStr` (type `str`) with a value of `\"77.0\"`. As we know, data of type `float` and type `str` are not compatible for math operations. Let's start by defining `forecastHighStr` and then see how we can we address this issue to make these data work together."
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'77.0'"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"forecastHighStr = \"77.0\"\n",
"forecastHighStr"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"str"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(forecastHighStr)"
]
},
{
Expand All @@ -1062,12 +1103,12 @@
"source": [
"### Converting data from one type to another\n",
"\n",
"It is not the case that things like the `tempFahrenheit` and `weatherForecast` cannot be combined at all, but in order to combine a character string with a number we need to perform a *{term}`type conversion`* to make them compatible. Let's convert `tempFahrenheit` to a character string using the `str()` function. We can store the converted variable as `tempFahrenheitStr`."
"It is not the case that things like the `tempFahrenheit` and `forecastHighStr` cannot be combined at all, but in order to combine a character string with a number we need to perform a *{term}`type conversion`* to make them compatible. Let's convert `forecastHighStr` to a floating point number using the `float()` function. We can store the converted variable as `forecastHigh`."
]
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 30,
"metadata": {
"editable": true,
"slideshow": {
Expand All @@ -1077,7 +1118,7 @@
},
"outputs": [],
"source": [
"tempFahrenheitStr = str(tempFahrenheit)"
"forecastHigh = float(forecastHighStr)"
]
},
{
Expand All @@ -1090,12 +1131,12 @@
"tags": []
},
"source": [
"We can confirm the type has changed by checking the type of `tempFahrenheitStr` or by checking the output of a code cell with the variable."
"We can confirm the type has changed by checking the type of `forecastHigh` or by checking the output of a code cell with the variable."
]
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 31,
"metadata": {
"editable": true,
"slideshow": {
Expand All @@ -1107,21 +1148,21 @@
{
"data": {
"text/plain": [
"str"
"float"
]
},
"execution_count": 29,
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(tempFahrenheitStr)"
"type(forecastHigh)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 32,
"metadata": {
"editable": true,
"slideshow": {
Expand All @@ -1133,16 +1174,16 @@
{
"data": {
"text/plain": [
"'62.6'"
"77.0"
]
},
"execution_count": 30,
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tempFahrenheitStr"
"forecastHigh"
]
},
{
Expand All @@ -1155,12 +1196,12 @@
"tags": []
},
"source": [
"As you can see, `str()` converts a numerical value into a character string with the same numbers as before. Similar to using `str()` to convert numbers to character strings, `int()` can be used to convert strings or floating point numbers to integers, and `float()` can be used to convert strings or integers to floating point numbers. For example, we could convert `tempFahrenheit` to an integer as follows."
"As you can see, `float()` converts a character string to a decimal value representing the number stored in the string. `float()` can be used to convert strings or integers to floating point numbers, however `float()` can only convert strings that represent numerical values. For example, `float(\"Cold\")` will raise a `ValueError` because `\"Cold\"` cannot be converted to a number directly. Similar to `float()`, `str()` can convert numbers to character strings, and `int()` can be used to convert strings or floating point numbers to integers. For example, we could convert `tempFahrenheit` to an integer as follows."
]
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 33,
"metadata": {
"editable": true,
"slideshow": {
Expand Down Expand Up @@ -1188,7 +1229,7 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 34,
"metadata": {
"editable": true,
"slideshow": {
Expand All @@ -1203,7 +1244,7 @@
"int"
]
},
"execution_count": 32,
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -1214,7 +1255,7 @@
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": 35,
"metadata": {
"editable": true,
"slideshow": {
Expand All @@ -1229,7 +1270,7 @@
"62"
]
},
"execution_count": 33,
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -1270,7 +1311,7 @@
},
{
"cell_type": "code",
"execution_count": 34,
"execution_count": 36,
"metadata": {
"editable": true,
"slideshow": {
Expand All @@ -1287,7 +1328,7 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 37,
"metadata": {
"editable": true,
"slideshow": {
Expand Down Expand Up @@ -1327,12 +1368,12 @@
"source": [
"#### Question 2.5\n",
"\n",
"What output would you expect to see when you execute `print(weatherForecast + tempFahrenheitStr)`?"
"What output would you expect to see when you execute `float(weatherForecast)`?"
]
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 38,
"metadata": {
"editable": true,
"slideshow": {
Expand All @@ -1349,104 +1390,35 @@
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 39,
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": [
"remove_book_cell",
"hide-cell"
"hide-cell",
"raises-exception"
]
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hot62.6\n"
"ename": "ValueError",
"evalue": "could not convert string to float: 'Hot'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[39], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# Solution\u001b[39;00m\n\u001b[0;32m----> 3\u001b[0m \u001b[38;5;28;43mfloat\u001b[39;49m\u001b[43m(\u001b[49m\u001b[43mweatherForecast\u001b[49m\u001b[43m)\u001b[49m\n",
"\u001b[0;31mValueError\u001b[0m: could not convert string to float: 'Hot'"
]
}
],
"source": [
"# Solution\n",
"\n",
"print(weatherForecast + tempFahrenheitStr)"
]
},
{
"cell_type": "markdown",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
"### Combining text and numbers\n",
"\n",
"Although most mathematical operations are applied to numerical values, a common way to combine character strings is using the addition operator `+`. Let's create a text string in the variable `temp_and_forecast` that is the combination of the `tempFahrenheit` and `weatherForecast` variables. Once we define `temp_and_forecast`, we can print it to the screen to see the result."
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"outputs": [],
"source": [
"temp_and_forecast = (\n",
" \"The current temperature is \"\n",
" + str(tempFahrenheit)\n",
" + \" and the forecast for today is \"\n",
" + weatherForecast\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"'The current temperature is 62.6 and the forecast for today is Hot'"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temp_and_forecast"
]
},
{
"cell_type": "markdown",
"metadata": {
"editable": true,
"slideshow": {
"slide_type": ""
},
"tags": []
},
"source": [
"Note that here we are converting `tempFahrenheit` to a character string using the `str()` function within the assignment to the variable `temp_and_forecast`. Alternatively, we could have simply combined `tempFahrenheitStr` and `weatherForecast`."
"float(weatherForecast)"
]
}
],
Expand Down
Loading

0 comments on commit 02316ee

Please sign in to comment.