Files
learn-python3/notebooks/beginner/exercises/06_for_loops_exercise.ipynb
2023-04-24 13:13:43 +03:00

152 lines
3.1 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 1. Fill the missing pieces\n",
"Fill the `____` parts in the code below."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"words = [\"PYTHON\", \"JOHN\", \"chEEse\", \"hAm\", \"DOE\", \"123\"]\n",
"upper_case_words = []\n",
"\n",
"for ____ in words:\n",
" if ____.isupper():\n",
" ____.append(____)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"editable": false
},
"outputs": [],
"source": [
"assert upper_case_words == [\"PYTHON\", \"JOHN\", \"DOE\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 2. Calculate the sum of dict values\n",
"Calculate the sum of the values in `magic_dict` by taking only into account numeric values (hint: see [isinstance](https://docs.python.org/3/library/functions.html#isinstance)). "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"editable": false
},
"outputs": [],
"source": [
"magic_dict = dict(val1=44, val2=\"secret value\", val3=55.0, val4=1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Your implementation\n",
"sum_of_values = "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"editable": false
},
"outputs": [],
"source": [
"assert sum_of_values == 100"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3. Create a list of strings based on a list of numbers\n",
"The rules:\n",
"* If the number is a multiple of five and odd, the string should be `'five odd'`\n",
"* If the number is a multiple of five and even, the string should be `'five even'`\n",
"* If the number is odd, the string is `'odd'`\n",
"* If the number is even, the string is `'even'`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"editable": false
},
"outputs": [],
"source": [
"numbers = [1, 3, 4, 6, 81, 80, 100, 95]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Your implementation\n",
"my_list = "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"editable": false
},
"outputs": [],
"source": [
"assert my_list == [\n",
" \"odd\",\n",
" \"odd\",\n",
" \"even\",\n",
" \"even\",\n",
" \"odd\",\n",
" \"five even\",\n",
" \"five even\",\n",
" \"five odd\",\n",
"]"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}