Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 161 additions & 20 deletions 1_variables_operators.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1308,7 +1308,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -1327,10 +1327,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"data": {
"text/plain": [
"int"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(x1)\n",
"type(x2)\n",
"type(x3)\n",
"type(x4)\n",
"type(x5)\n",
"type(x6)"
]
},
{
"cell_type": "markdown",
Expand All @@ -1341,10 +1359,12 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": []
"source": [
"# one is a string and one is a variable"
]
},
{
"cell_type": "markdown",
Expand All @@ -1355,10 +1375,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"ename": "TypeError",
"evalue": "unsupported operand type(s) for -: 'str' and 'float'",
"output_type": "error",
"traceback": [
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
"\u001b[31mTypeError\u001b[39m Traceback (most recent call last)",
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[9]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43mx3\u001b[49m\u001b[43m \u001b[49m\u001b[43m-\u001b[49m\u001b[43m \u001b[49m\u001b[43mx1\u001b[49m \u001b[38;5;66;03m# one is a float (1.1), the other is a string\u001b[39;00m\n",
"\u001b[31mTypeError\u001b[39m: unsupported operand type(s) for -: 'str' and 'float'"
]
}
],
"source": [
"x3 - x1 # one is a float (1.1), the other is a string"
]
},
{
"cell_type": "markdown",
Expand All @@ -1369,10 +1403,12 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": []
"source": [
"# true is logical, \"True\" is a string"
]
},
{
"cell_type": "markdown",
Expand All @@ -1385,8 +1421,22 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"ename": "TypeError",
"evalue": "unsupported operand type(s) for -: 'bool' and 'str'",
"output_type": "error",
"traceback": [
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
"\u001b[31mTypeError\u001b[39m Traceback (most recent call last)",
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[11]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43mx4\u001b[49m\u001b[43m \u001b[49m\u001b[43m-\u001b[49m\u001b[43m \u001b[49m\u001b[43mx5\u001b[49m\n",
"\u001b[31mTypeError\u001b[39m: unsupported operand type(s) for -: 'bool' and 'str'"
]
}
],
"source": [
"x4 - x5 # true is logical - boolean, \"True\" is a string"
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -1439,6 +1489,41 @@
"Even though we entered *integers*, the `input()` function converts them into *strings* **by default** (it's the way the function `input()` is defined). Now we will perform data type conversion from *string* to *integer*. Use the `int()` function to do so. Print again the values after performing data type conversion and the corresponding type."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"8\n",
"14\n"
]
},
{
"data": {
"text/plain": [
"int"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x1 = input(\"please enter your 1st number\")\n",
"x2 = input(\"please enter your 2nd number\")\n",
"x1 = int(x1)\n",
"x2 = int(x2)\n",
"print(x1)\n",
"type(x1)\n",
"print(x2)\n",
"type(x2)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down Expand Up @@ -1466,10 +1551,31 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": []
"source": [
"# x1 and x2 are vairiables\n",
"x1 = 20\n",
"x2 = 50\n",
"# equal?\n",
"equal = x1 == x2\n",
"# is x1 greater than x2\n",
"x1_greater = x1 > x2\n",
"# is x2 greater than x1\n",
"x2_greater = x2 > x1\n",
"# is x1 not equal to x2\n",
"aint_equal = x1 != x2\n",
"#the absolute difference (larger - smaller)?\n",
"x3 = abs(x1 - x2)\n",
"#increment the smaller by the difference\n",
"if x1 < x2:\n",
" x1 += x3\n",
"else:\n",
" x2 += x3\n",
"are_equal = x1 == x2\n",
"\n"
]
},
{
"cell_type": "markdown",
Expand All @@ -1482,10 +1588,27 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": []
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello there, Geo Smith!\n"
]
}
],
"source": [
"# ask for first name\n",
"first_name = input(\"Enter your first name, please: \")\n",
"# ask for last name\n",
"last_name = input(\"Enter your last name, please: \")\n",
"# concatenate with a space in between carefully\n",
"full_name = first_name + \" \" + last_name\n",
"# display the greeting\n",
"print(\"Hello there, \" + full_name + \"!\")"
]
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -1662,11 +1785,29 @@
"source": [
"Membership operators perform membership testing by checking if a value matches any element in the given sequence. They return a boolean value (True or False) based on the test result."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5\n"
]
}
],
"source": [
"print(5\n",
" )"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -1680,7 +1821,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.11.3"
}
},
"nbformat": 4,
Expand Down