Skip to content
Open

File #48

Show file tree
Hide file tree
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
1,129 changes: 1,129 additions & 0 deletions Exercises/Intro2Python/Module1-DataTypes.ipynb

Large diffs are not rendered by default.

281 changes: 281 additions & 0 deletions Exercises/Intro2Python/Module2-Conditionals.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Conditionals and code indendation\n",
"In Python, code blocks are separated by use of indentation. See the defintion of an `if`-statement below:\n",
"\n",
"### Syntax of conditional blocks\n",
" \n",
"```python\n",
"if condition:\n",
" # Code goes here (must be indented!)\n",
" # Otherwise, IndentationError will be thrown\n",
"\n",
"# Code placed here is outside of the if-statement \n",
"```\n",
"\n",
"Where evaluation of `condition` must return a boolean (`True` or `False`).\n",
"\n",
"\n",
"> **Remember:**\n",
"> 1. The `:` ***must*** be present after `condition`.\n",
"> 2. The line immediately after `:` ***must*** be indented. \n",
"> 3. The `if`-statement is ***exited by reverting the indentation*** as shown above.\n",
"\n",
"This is how Python interprets the code as a block. \n",
"\n",
"The same indentation rules are required for all types of code blocks, the `if`-block above is just an example. Examples of other types of code blocks are `for` and `while` loops, functions etc.\n",
"\n",
"All editors will automatically make the indentation upon hitting enter after the `:`, so it doesn't take long to get used to this. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Conditional statements - examples\n",
"\n",
"### `if`-statements\n",
"An `if`-statement has the following syntax:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x is larger than 1\n"
]
}
],
"source": [
"x = 2 # Example variable assignment\n",
"if x > 1: # Check if x is greater than 1\n",
" print('x is larger than 1') # Print message if condition is true"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### `if` / `else`-statements"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"y is less than or equal to 1\n"
]
}
],
"source": [
"#an if-else statement example. This will execute one block of code if the condition is true, and another block if it is false.\n",
"\n",
"y = 1 # Another example variable assignment, make example where we set the if value to a variable...\n",
"if y > 1: # Check if y is greater than 1\n",
" print('y is larger than 1') # Print message if condition is true\n",
"else:\n",
" print('y is less than or equal to 1') # Print message if condition is false"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### `if` / `elif` / `else`"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"z is less than 1\n"
]
}
],
"source": [
"#an if-elif-else statement example. This will check multiple conditions in sequence.\n",
"\n",
"z = 0 # Another example variable assignment\n",
"if z > 1: # Check if z is greater than 1\n",
" print('z is larger than 1') # Print message if condition is true\n",
"elif z < 1: # Check if z is less than 1 using the elif statement\n",
" print('z is less than 1') # Print message if the elif condition is true\n",
"else: # This block executes if none of the above conditions are true\n",
" print('z is equal to 1') # Print message if z is equal to 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"An unlimited number of `elif` blocks can be used in between `if` and `else`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise 1\n",
"If the string below has more than 100 characters, print *\"String has more than 100 characters\"*, otherwise print *\"String has less than or exactly 100 characters\"*.\n",
"~~~python\n",
"string = 'There are approximately 114 SNOTEL sites in Utah, as a part of the Natural Resouces Conservation Service (NRCS) and ~150 USGS NWIS streamflow monitoring gages.'\n",
"~~~"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise 2\n",
"Print the number of space characters in `string` from above. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise 3\n",
"Create the variables\n",
"\n",
"```python\n",
"letter1 = 'USGS sites'\n",
"letter2 = 'SNOTEL sites'\n",
"```\n",
"\n",
"Convert this pseudo code to a Python program:\n",
"\n",
"```python\n",
"if there are more occurrences of letter1 than letter2 in string:\n",
" print(\"There are more {insert_letter1} than {insert_letter2} \")\n",
" \n",
"elif there are more occurrences of letter2 than letter1 in string:\n",
" print(\"There are more {insert_letter2} than {insert_letter1} \")\n",
"\n",
"else:\n",
" print(\"There are exactly the same number {insert_letter1} and {insert_letter2}\")\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Exercise 4\n",
"Test the program you wrote above with different combinations of letters for the variables `letter1` and `letter2`. \n",
"Implement a print message of the actual number of occurrences."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next [Module](./Module3-DataStructures.ipynb)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
"hide_input": false,
"kernelspec": {
"display_name": "p310env",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.19"
},
"latex_envs": {
"LaTeX_envs_menu_present": true,
"autoclose": false,
"autocomplete": true,
"bibliofile": "biblio.bib",
"cite_by": "apalike",
"current_citInitial": 1,
"eqLabelWithNumbers": true,
"eqNumInitial": 1,
"hotkeys": {
"equation": "Ctrl-E",
"itemize": "Ctrl-I"
},
"labels_anchors": false,
"latex_user_defs": false,
"report_style_numbering": false,
"user_envs_cfg": false
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": false,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Table of Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": true
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading