summaryrefslogtreecommitdiff
path: root/tutorials/module_1/basics_of_python.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'tutorials/module_1/basics_of_python.ipynb')
-rw-r--r--tutorials/module_1/basics_of_python.ipynb128
1 files changed, 86 insertions, 42 deletions
diff --git a/tutorials/module_1/basics_of_python.ipynb b/tutorials/module_1/basics_of_python.ipynb
index 1776291..fc98569 100644
--- a/tutorials/module_1/basics_of_python.ipynb
+++ b/tutorials/module_1/basics_of_python.ipynb
@@ -2,14 +2,6 @@
"cells": [
{
"cell_type": "markdown",
- "id": "d9cee0e6-c132-44cd-acd4-1208721e2007",
- "metadata": {},
- "source": [
- "# "
- ]
- },
- {
- "cell_type": "markdown",
"id": "a5c32509-d4c7-4fd5-92cf-1a923b78f443",
"metadata": {},
"source": [
@@ -17,12 +9,6 @@
"\n",
"This page contains important fundamental concepts used in Python such as syntax, operators, order or precedence and more.\n",
"\n",
- "[Syntax](1_05_basics_of_python#Syntax)\n",
- "[Operators](1_05_basics_of_python#Operators)\n",
- "[Order of operation](1_05_basics_of_python#Order%20of%20Operation)\n",
- "[Data types](1_05_basics_of_python#data%20types)\n",
- "[Variables](1_05_basics_of_python#Variables)\n",
- "\n",
"---"
]
},
@@ -34,8 +20,10 @@
},
"source": [
"## Syntax\n",
+ "\n",
"### Indentations and blocks\n",
"In python *indentations* or the space at the start of each line, signifies a block of code. This becomes important when we start working with function and loops. We will talk more about this in the controls structures tutorial. \n",
+ "\n",
"### Comments\n",
"Comments can be added to your code using the hash operator (#). Any text behind the comment operator till the end of the line will be rendered as a comment.\n",
"If you have an entire block of text or code that needs to be commented out, the triple quotation marks (\"\"\") can be used. Once used all the code after it will be considered a comment until the comment is ended with the triple quotation marks.\n",
@@ -53,6 +41,7 @@
"source": [
"## Operators\n",
"In python, operators are special symbols or keywords that perform operations on values or variables. This section covers some of the most common operator that you will see in this course.\n",
+ "\n",
"### Arithmetic operators\n",
"| Operator | Name |\n",
"| --- | --- |\n",
@@ -168,21 +157,8 @@
},
{
"cell_type": "code",
- "execution_count": 1,
- "id": "a9bc3bda-e55d-4f5a-813d-8f6316f12c64",
- "metadata": {},
- "outputs": [],
- "source": [
- "x = 33 # Integer\n",
- "y = 3.14 # Float\n",
- "name = \"Joe\" # String\n",
- "is_valid = True # Boolean"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "id": "f6af843b-159f-4d10-9c84-abebde7e7bbc",
+ "execution_count": 19,
+ "id": "125e385b-27dd-4139-8904-10996b536331",
"metadata": {},
"outputs": [
{
@@ -194,27 +170,44 @@
}
],
"source": [
+ "x = 33 # Integer\n",
+ "y = 3.14 # Float\n",
+ "name = \"Joe\" # String\n",
+ "is_valid = True # Boolean\n",
+ "\n",
"print(x**2+3*y)"
]
},
{
"cell_type": "markdown",
- "id": "bcc9a481-2421-4df7-82a8-ee1e76c7f4a1",
+ "id": "98b37bca-8821-4866-8973-80a3315aba0b",
"metadata": {},
"source": [
- "Change the x and y values above, re-run the notebook and see what happens.\n",
- "\n",
- "You can assign multiple variables at once:\n",
+ "Change the x and y values above, re-run the cell to see what happens.\n",
"\n",
- "```python\n",
- "a, b, c = 1, 2, 3\n",
- "```\n",
+ "You can assign multiple variables at once or on the same line."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 21,
+ "id": "6d9e59c3-1f38-444a-b09b-6914dba7233a",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "2 7.3 5.3 2.71828 2.71828 2.71828\n"
+ ]
+ }
+ ],
+ "source": [
+ "a, b, c = 2, 7.3, 5.3\n",
"\n",
- "Similarly we can assign the same value to multiple variables:\n",
+ "x = y = z = 2.71828\n",
"\n",
- "```python\n",
- "x = y = z = 100\n",
- "```"
+ "print(a,b,c,x,y,z)"
]
},
{
@@ -251,7 +244,7 @@
},
{
"cell_type": "code",
- "execution_count": 3,
+ "execution_count": 22,
"id": "a7e71be9-dd9a-42c9-84e2-cb5f8f8939b1",
"metadata": {},
"outputs": [
@@ -271,6 +264,57 @@
"y = \"Hello\"\n",
"print(f' Variable y is type: {type(y)}')"
]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "fcdca3e3-4386-4515-abbe-13242b55c2d7",
+ "metadata": {},
+ "source": [
+ "# Exercise\n",
+ "\n",
+ "You're analyzing a cylindrical steel rod under axial load for a design. The goal is to calculate the stress in the rod and determine if it is within safe limits.\n",
+ "\n",
+ "Given an applied force of 15,000 N on a rod with the diameter 22 mm and a yield strength of steel 250 MPa.\n",
+ "\n",
+ "Hint:\n",
+ "\n",
+ "$Stress=\\frac{Force}{Area}$ , $Area=\\frac{π d^2}{4}$"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 15,
+ "id": "cdcc3fde-b63e-4443-a8a1-b59396d3560e",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Stress in rod: 39.5 MPa\n",
+ "The design is SAFE under the given load.\n"
+ ]
+ }
+ ],
+ "source": [
+ "# Given\n",
+ "force_N = 15000\n",
+ "D_mm = 22\n",
+ "sigma_MPa = 250\n",
+ "\n",
+ "# Equations\n",
+ "area = 3.14* D_mm**2 / 4\n",
+ "stress = force_N / area\n",
+ "\n",
+ "# Print output\n",
+ "print(f\"Stress in rod: {stress:.1f} MPa\")\n",
+ "\n",
+ "# Check safety\n",
+ "if stress < sigma_MPa:\n",
+ " print(\"The design is SAFE under the given load.\")\n",
+ "else:\n",
+ " print(\"The design FAILED. Consider redesigning.\")"
+ ]
}
],
"metadata": {
@@ -289,7 +333,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
- "version": "3.12.9"
+ "version": "3.13.2"
}
},
"nbformat": 4,