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
106 changes: 94 additions & 12 deletions 02_activities/assignments/assignment_1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -56,32 +56,75 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 28,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# For testing purposes, we will write our code in the function\n",
"def anagram_checker(word_a, word_b):\n",
" # Your code here\n",
" #word_a = word_a.lower()\n",
" #word_b = word_b.lower()\n",
" a = sorted(word_a.lower())\n",
" b = sorted(word_b.lower())\n",
" if a == b:\n",
" return True\n",
" else:\n",
" return False\n",
"\n",
"\n",
"\n",
"# Run your code to check using the words below:\n",
"anagram_checker(\"Silent\", \"listen\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 29,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"Silent\", \"Night\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 30,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"night\", \"Thing\")"
]
Expand All @@ -97,22 +140,61 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 31,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def anagram_checker(word_a, word_b, is_case_sensitive):\n",
" # Modify your existing code here\n",
" a = sorted(word_a)\n",
" b = sorted(word_b)\n",
" if is_case_sensitive:\n",
" if a == b:\n",
" return True\n",
" else:\n",
" return False\n",
" elif is_case_sensitive == False:\n",
" a = sorted(word_a.lower())\n",
" b = sorted(word_b.lower())\n",
" if a == b:\n",
" return True\n",
" else:\n",
" return False\n",
"\n",
" \n",
"\n",
"\n",
"# Run your code to check using the words below:\n",
"anagram_checker(\"Silent\", \"listen\", False) # True"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 32,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"anagram_checker(\"Silent\", \"Listen\", True) # False"
]
Expand All @@ -130,7 +212,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "new-learner",
"display_name": "python-env",
"language": "python",
"name": "python3"
},
Expand All @@ -144,7 +226,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
"version": "3.11.14"
}
},
"nbformat": 4,
Expand Down
214 changes: 214 additions & 0 deletions 1_Functions.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 3,
"id": "e8686024",
"metadata": {},
"outputs": [],
"source": [
"def divide(divident,divisor):\n",
" result = divident / divisor\n",
" return result\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "dca148b0",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2.0"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"divide(10,5)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "92d87af4",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.65\n",
"0.4\n"
]
}
],
"source": [
"# default value assignment\n",
"\n",
"def calc_sales_tax(price, tax_rate=0.13):\n",
" sales_tax = price * tax_rate\n",
" return sales_tax\n",
"\n",
"print(calc_sales_tax(5))\n",
"print(calc_sales_tax(5,0.08)) # this overrides the default value\n"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "16cb9446",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"256.0\n",
"262.0\n"
]
}
],
"source": [
"# keyword arguments, position of the supplied parameters while calling the function is important\n",
"# docstring using 3 single quote \n",
"def calc_total_bill(price, tax_rate=0.13,tip_rate=0.15):\n",
" '''Calculate total bill with tax and tip'''\n",
" tax = price * tax_rate\n",
" tip = price * tip_rate\n",
" total = price + tax + tip\n",
" return total\n",
"\n",
"print(calc_total_bill(200))\n",
"\n",
"print(calc_total_bill(200,tip_rate=0.18)) # position of argument matter so kerword argument is used"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "8b5db084",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on function calc_total_bill in module __main__:\n",
"\n",
"calc_total_bill(price, tax_rate=0.13, tip_rate=0.15)\n",
" Calculate total bill with tax and tip\n",
"\n"
]
}
],
"source": [
"# docstring - creating help text for functions\n",
"\n",
"help(calc_total_bill)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c127b2b6",
"metadata": {},
"outputs": [],
"source": [
"# variable scope\n",
"# variables within functions are local - only access it within that function and not outside\n",
"# You can explicitely define global variables within function using the keyword global"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "75086155",
"metadata": {},
"outputs": [],
"source": [
"def calc_total_bill(price, tax_rate=0.13, tip_rate=\"standard\"):\n",
"\n",
" tip_presets = {\n",
"\n",
" \"low\": 0.10,\n",
"\n",
" \"standard\": 0.15,\n",
"\n",
" \"high\": 0.20\n",
"\n",
" }\n",
"\n",
" \n",
"\n",
" # only convert if it's one of the known presets\n",
"\n",
" if tip_rate in tip_presets:\n",
"\n",
" tip_rate = tip_presets[tip_rate]\n",
"\n",
" \n",
"\n",
" tax = price * tax_rate\n",
"\n",
" tip = price * tip_rate\n",
"\n",
" total = price + tax + tip\n",
"\n",
" return total"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "4ce3079b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"50\n",
"51\n"
]
}
],
"source": [
"# input function - always takes in value as string\n",
"\n",
"age = input('How old are you?')\n",
"\n",
"print(age)\n",
"\n",
"age_next_bday = (int(age) + 1)\n",
"print(age_next_bday)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "python-env",
"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.11.14"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading