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
166 changes: 141 additions & 25 deletions 2022/python_workshop/notebooks/02_basic_data_types.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,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 = 1716000483920\n",
"y_initial_identity = 1716000483920\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; 1716000483920\n",
"x_final_identity: 1716000483952\n"
]
}
],
"source": [
"# assign x a value and then assign x to y\n",
"x_int = 10\n",
Expand Down Expand Up @@ -154,17 +174,31 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Este ok\n",
"Si aici este ok\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",
" x = 1\n",
"except ZeroDivisionError:\n",
" print(\"you can't divide by zero!\")"
" print(\"you can't divide by zero!\")\n",
" raise ZeroDivisionError\n",
"else:\n",
" print('Este ok')\n",
"finally:\n",
" print(\"Si aici este ok\")\n"
]
},
{
Expand Down Expand Up @@ -265,12 +299,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Peter Pan is a terrible boxer.\n",
"Whenever he throws a punch, it Neverlands\n",
"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",
"new_str = one_liner.split(\" \")\n",
"print(new_str[-1])\n",
"\n",
"multi_liner = \"\"\"\n",
"I was wondering why the frisbee kept getting bigger and bigger.\n",
Expand All @@ -282,9 +333,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['']\n",
"Ellipsis\n"
]
}
],
"source": [
"# get the last word of that one-liner\n",
"\n",
Expand All @@ -307,24 +367,55 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 19,
"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": [
"# replace the wildcard in the string with a value\n",
"statement = \"\"\"\n",
"SELECT * FROM my_table WHERE date >= {};\n",
"\"\"\"\n",
"statement_v2 = \"\"\"\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 = statement.format(start_date)\n",
"replaced_statement_v2 = statement_v2.replace(\"$$start_date$$\", start_date)\n",
"\n",
"print(replaced_statement)\n",
"print(replaced_statement_v2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 22,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"This is a string that needs to be split,\n",
"based in the comma character,\n",
"so it should result in three strings.\n",
"\n"
]
}
],
"source": [
"# split the following string based on the comma\n",
"s = \"\"\"\n",
Expand All @@ -333,7 +424,7 @@
"so it should result in three strings.\n",
"\"\"\"\n",
"\n",
"split_strings = ...\n",
"split_strings = s.strip('strings')\n",
"print(split_strings)"
]
},
Expand All @@ -359,9 +450,22 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"x evaluated to False\n",
"False\n",
"True\n",
"True\n",
"False\n"
]
}
],
"source": [
"# an empty (None) variable evaluates to False\n",
"x = int()\n",
Expand All @@ -370,19 +474,31 @@
" print('x evaluated to False')\n",
"\n",
"# what does 0 evaluate to?\n",
"zero = bool(0)\n",
"print(zero)\n",
"\n",
"# what does 1 evaluate to?\n",
"\n",
"one = bool(1)\n",
"print(one)\n",
"# what does a random string evaluate to?\n",
"\n",
"string = bool('fjghggf')\n",
"print(string)\n",
"# what does an empty string evaluate to?\n",
"\n"
"empty_string = bool('')\n",
"print(empty_string)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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 +512,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