-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
3,053 additions
and
15 deletions.
There are no files selected for viewing
199 changes: 197 additions & 2 deletions
199
reference_notebooks/.ipynb_checkpoints/task2-checkpoint.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,201 @@ | ||
{ | ||
"cells": [], | ||
"metadata": {}, | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Defaultdict and namedtuple\n", | ||
"\n", | ||
"Reference notebook for the second task of the *Python Tricks and Hacks for Productivity* course on Coursera.\n", | ||
"\n", | ||
"**Instructor**: *Danilo Lessa Bernardineli ([email protected])*" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Put deposits into the account balances" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"account_balances = {'Jonathan': 5.49,\n", | ||
" 'Markus': 1.39}\n", | ||
"\n", | ||
"deposits = {'Jonathan': 0.49,\n", | ||
" 'Markus': 0.39}\n", | ||
"\n", | ||
"for account, deposit in deposits.items():\n", | ||
" account_balances[account] += deposit\n", | ||
"print(account_balances)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"ename": "KeyError", | ||
"evalue": "'Jonathan'", | ||
"output_type": "error", | ||
"traceback": [ | ||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | ||
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", | ||
"\u001b[0;32m<ipython-input-2-f5ff5002477b>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0maccount\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdeposit\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mdeposits\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mitems\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0maccount_balances\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0maccount\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0mdeposit\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 10\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0maccount_balances\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | ||
"\u001b[0;31mKeyError\u001b[0m: 'Jonathan'" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"account_balances = {}\n", | ||
"\n", | ||
"deposits = {'Jonathan': 0.49,\n", | ||
" 'Markus': 0.39,\n", | ||
" 'Jamsheed': 100.00}\n", | ||
"\n", | ||
"\n", | ||
"for account, deposit in deposits.items():\n", | ||
" account_balances[account] += deposit\n", | ||
"print(account_balances)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"account_balances = {}\n", | ||
"\n", | ||
"deposits = {'Jonathan': 0.49,\n", | ||
" 'Markus': 0.39,\n", | ||
" 'Jamsheed': 100.00}\n", | ||
"\n", | ||
"for account, deposit in deposits.items():\n", | ||
" if account not in account_balances:\n", | ||
" account_balances[account] = 0.0\n", | ||
" account_balances[account] += deposit\n", | ||
"print(account_balances)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from collections import defaultdict\n", | ||
"\n", | ||
"account_balances = defaultdict(lambda: 15)\n", | ||
"\n", | ||
"deposits = {'Jonathan': 0.49,\n", | ||
" 'Markus': 0.39,\n", | ||
" 'Jamsheed': 100.00}\n", | ||
"\n", | ||
"for account, deposit in deposits.items():\n", | ||
" account_balances[account] += deposit\n", | ||
"print(dict(account_balances))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from typing import NamedTuple\n", | ||
"\n", | ||
"class Deposit(NamedTuple):\n", | ||
" receiver: str\n", | ||
" value: float\n", | ||
" \n", | ||
"Deposit(receiver='Markus', value=5.49)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"Deposit(sender='Anderson', value=4.99)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from typing import NamedTuple\n", | ||
"\n", | ||
"class Deposit(NamedTuple):\n", | ||
" receiver: str\n", | ||
" value: float\n", | ||
"\n", | ||
"account_balances = defaultdict(lambda: 15)\n", | ||
"\n", | ||
"deposits = [Deposit(receiver='Markus', value=5.49),\n", | ||
" Deposit(receiver='Jonathan', value=15.49),\n", | ||
" Deposit(receiver='Jamsheed', value=25.49)]\n", | ||
"\n", | ||
"for deposit in deposits:\n", | ||
" account_balances[deposit.receiver] += deposit.value\n", | ||
"print(dict(account_balances))" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"account_balances = {}\n", | ||
"\n", | ||
"deposits = {'Jonathan': 0.49,\n", | ||
" 'Markus': 0.39,\n", | ||
" 'Jamsheed': 100.00}\n", | ||
"\n", | ||
"for account, deposit in deposits.items():\n", | ||
" if account not in account_balances:\n", | ||
" account_balances[account] = 0.0\n", | ||
" account_balances[account] += deposit\n", | ||
"print(account_balances)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"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.7.6" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
Oops, something went wrong.