Skip to content
Open
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
170 changes: 139 additions & 31 deletions lab-python-list-comprehension.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,33 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "21625526-3fae-4c55-bab5-f91940070681",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"['Open', 'Open', 'Senior', 'Open', 'Open', 'Senior']"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"\n",
"def category(list_of_members):\n",
" return [\n",
" \"Senior\" if member[0] >= 55 and member[1] > 7 else \"Open\"\n",
" for member in list_of_members\n",
" ]\n",
"\n",
"list_of_members = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]]\n",
"\n",
"category(list_of_members)"
]
},
{
Expand All @@ -95,12 +116,30 @@
},
{
"cell_type": "code",
"execution_count": null,
"id": "53061dce-7aa3-4476-8b56-71413c5e135c",
"execution_count": 2,
"id": "6b983545-9057-4a9c-8b37-15a1c48a80fa",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"23"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"def sum_of_multiples(n):\n",
" if n <0:\n",
" return 0\n",
" multiples = [i for i in range(1,n) if i%3 == 0 or i%5 == 0]\n",
" return sum(multiples)\n",
"\n",
"sum_of_multiples(10)\n",
" "
]
},
{
Expand All @@ -127,12 +166,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "a8cb1579-7065-4fc0-bd53-f91c2ad1dad9",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 4, 5, 6]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"def create_array(n):\n",
" n_string = str(n)\n",
" new_list = [int(i) for i in n_string]\n",
" return new_list\n",
"\n",
"create_array(123456)"
]
},
{
Expand All @@ -155,12 +210,26 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"id": "86ebe759-76d8-4012-8590-c48a473a6099",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"{-5, -4, -3, -2, -1}"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"def invert(given_set):\n",
" return {-i for i in given_set}\n",
"\n",
"invert([1,2,3,4,5])"
]
},
{
Expand Down Expand Up @@ -193,12 +262,22 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"id": "6711881b-450a-44cb-a3d0-367b2c6a4464",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15: 225}\n"
]
}
],
"source": [
"# your code goes here"
"d = {i:i ** 2 for i in range (0, 16)}\n",
"\n",
"print(d)"
]
},
{
Expand All @@ -220,12 +299,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"id": "a1d55cea-96c3-4853-8220-17c0904a8816",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[(1, 3), (1, 4), (2, 3), (2, 4)]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"def pairs(list1, list2):\n",
" new_list = [(d, i) for d in list1 for i in list2]\n",
" return new_list\n",
"\n",
"\n",
"\n",
"pairs([1, 2], [3, 4])"
]
},
{
Expand All @@ -246,12 +342,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"id": "0175239c-87fa-4ba0-8776-d62567256db7",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('a', 1), ('a', 2), ('b', 3), ('b', 4)]\n"
]
}
],
"source": [
"# your code goes here"
"def kv_pairs(dic):\n",
" return [(key, value) for key in dic for value in dic[key]]\n",
"\n",
"d = {\"a\": [1, 2], \"b\": [3, 4]}\n",
"print(kv_pairs(d)) \n",
" "
]
},
{
Expand Down Expand Up @@ -282,12 +391,10 @@
{
"cell_type": "code",
"execution_count": null,
"id": "248918d0-f082-40a8-9d5c-c7b4ffd2bfca",
"id": "c830a369-f20a-4efa-a1a2-dfc70122cd50",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
]
"source": []
},
{
"cell_type": "markdown",
Expand All @@ -308,20 +415,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"id": "c57d9e77-6859-45dd-a2e6-2c1898f1baa2",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"# your code goes here\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python [conda env:base] *",
"language": "python",
"name": "python3"
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -333,7 +441,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.5"
}
},
"nbformat": 4,
Expand Down