Skip to content
Open
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
204 changes: 164 additions & 40 deletions 2022/python_workshop/notebooks/02_basic_data_types.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
"- Built-in Functions"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -80,9 +87,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x_int = 10, y_int = 10\n",
"type of x_int: <class 'int'>\n",
"type of y_int: <class 'int'>\n",
"x_initial_identity = 2018793187920\n",
"y_initial_identity = 2018793187920\n",
"are they the same? True\n",
"x_int is y_int? True\n",
"does x_int have the same value as y_int? True\n",
"assigning new value to x\n",
"x_int = 11, y_int = 10\n",
"does x_int have the same value as y_int? False\n",
"x_initial_identity; 2018793187920\n",
"x_final_identity: 2018793187952\n"
]
}
],
"source": [
"# assign x a value and then assign x to y\n",
"x_int = 10\n",
Expand Down Expand Up @@ -136,9 +163,17 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"sys.float_info(max=1.7976931348623157e+308, max_exp=1024, max_10_exp=308, min=2.2250738585072014e-308, min_exp=-1021, min_10_exp=-307, dig=15, mant_dig=53, epsilon=2.220446049250313e-16, radix=2, rounds=1)\n"
]
}
],
"source": [
"import sys\n",
"print(sys.float_info)"
Expand All @@ -154,24 +189,43 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 9,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"you can't divide by zero!\n"
]
}
],
"source": [
"# let's try to divide by 0\n",
"# we will use a try-except block\n",
"\n",
"try:\n",
" x = 1 / 0\n",
"except ZeroDivisionError:\n",
" print(\"you can't divide by zero!\")"
" print(\"you can't divide by zero!\")\n",
"#raise Exception\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2\n",
"2\n",
"result: 1.4; integer part: 1.0\n"
]
}
],
"source": [
"import math\n",
"# let's try an integer division - when the result is always an integer\n",
Expand All @@ -185,9 +239,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"7.839999999999999\n"
]
}
],
"source": [
"# powers of x\n",
"print(0**0)\n",
Expand All @@ -196,9 +259,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'int'>\n",
"<class 'float'>\n"
]
}
],
"source": [
"# type default conversion\n",
"print(type(2*2))\n",
Expand Down Expand Up @@ -265,9 +337,23 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Peter Pan is a terrible boxer.\n",
"Whenever he throws a punch, it Neverlands\n",
"\n",
"I was wondering why the frisbee kept getting bigger and bigger.\n",
"But then it hit me.\n",
" Ba-dum tss.\n",
"\n"
]
}
],
"source": [
"one_liner = \"Peter Pan is a terrible boxer.\\nWhenever he throws a punch, it Neverlands\"\n",
"print(one_liner)\n",
Expand All @@ -282,42 +368,71 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 20,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Neverlands\n"
]
}
],
"source": [
"# get the last word of that one-liner\n",
"one_liner = \"Peter Pan is a terrible boxer.\\nWhenever he throws a punch, it Neverlands\"\n",
"\n",
"# hint: look for the last space position\n",
"last_space_at = ...\n",
"\n",
"if last_space_at != -1:\n",
" # if there is a last space, get to the next character - that's where the last word starts\n",
" last_word_starts_at = ...\n",
" last_word = ...\n",
" print(last_word)\n",
"# but what of there is no space in this one_liner??\n",
"else:\n",
" # what would that mean?\n",
" print(...)\n",
"\n",
"# what if this one-liner was empty?\n",
"one_liner = \"\""
"last_space_at = one_liner.split(\" \")[-1]\n",
"print(last_space_at)\n",
"\n",
"# if last_space_at != -1:\n",
"# # if there is a last space, get to the next character - that's where the last word starts\n",
"# last_word_starts_at = ...\n",
"# last_word = ...\n",
"# print(last_word)\n",
"# # but what of there is no space in this one_liner??\n",
"# else:\n",
"# # what would that mean?\n",
"# print(...)\n",
"\n",
"# # what if this one-liner was empty?\n",
"# one_liner = \"\""
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 24,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"SELECT * FROM my_table WHERE date >= '2022-07-01 20:22:07';\n",
"\n",
"\n",
"SELECT * FROM my_table WHERE date >= '2022-07-01 20:22:07';\n",
"\n"
]
}
],
"source": [
"#import fnmatch\n",
"# replace the wildcard in the string with a value\n",
"statement = \"\"\"\n",
"SELECT * FROM my_table WHERE date >= $$start_date$$;\n",
"\"\"\"\n",
"start_date = \"'2022-07-01 20:22:07'\"\n",
"replaced_statement = ...\n",
"print(replaced_statement)"
"replaced_statement = \"\"\"\n",
"SELECT * FROM my_table WHERE date >= {};\n",
"\"\"\".format(start_date)\n",
"#fnmatch.translate( start_date)\n",
"replaced_statement2 = statement.replace(\"$$start_date$$\", start_date)\n",
"print(replaced_statement)\n",
"print(replaced_statement2)"
]
},
{
Expand Down Expand Up @@ -359,9 +474,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 25,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"x evaluated to False\n"
]
}
],
"source": [
"# an empty (None) variable evaluates to False\n",
"x = int()\n",
Expand All @@ -382,7 +506,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.10.5 64-bit",
"display_name": "Python 3.9.12 ('base')",
"language": "python",
"name": "python3"
},
Expand All @@ -396,12 +520,12 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.5"
"version": "3.9.12"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
"hash": "d31b83e9610685068a0fe73b54051d44dc1110027bef1c52e64b671e72faac45"
}
}
},
Expand Down
Loading