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
211 changes: 182 additions & 29 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,
"id": "21625526-3fae-4c55-bab5-f91940070681",
"execution_count": 2,
"id": "ffa7d97f",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"result=[\"senior\" if int(input(\"enter your age\"))>=55 and int(input(\"enter your handicaped year\"))>7 else \"open\" for i in range(5)]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "4b93a7b9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['senior', 'open', 'senior', 'open', 'open']"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result"
]
},
{
Expand All @@ -95,12 +116,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"id": "53061dce-7aa3-4476-8b56-71413c5e135c",
"execution_count": 18,
"id": "1a82b5c8",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[23]"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"\n",
"[sum([i for i in range(1,10) if i % 3 == 0 or i % 5 == 0]) if i > 0 else 0]\n"
]
},
{
Expand All @@ -127,12 +160,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 25,
"id": "a8cb1579-7065-4fc0-bd53-f91c2ad1dad9",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[7, 8, 9]"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"[int(i) for i in (input(\"enter a number\"))]\n",
"\n"
]
},
{
Expand All @@ -155,12 +200,46 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 43,
"id": "86ebe759-76d8-4012-8590-c48a473a6099",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[-1, -2, -3, -4, -5]"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"invert=[1,2,3,4,5]\n",
"[-i for i in invert]"
]
},
{
"cell_type": "code",
"execution_count": 45,
"id": "58ad6681",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, -3, -4, 5]"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"invert2=[-1,-2,3,4,-5]\n",
"[-i for i in invert2]"
]
},
{
Expand Down Expand Up @@ -193,12 +272,36 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 50,
"id": "6711881b-450a-44cb-a3d0-367b2c6a4464",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"{1: 1,\n",
" 2: 4,\n",
" 3: 9,\n",
" 4: 16,\n",
" 5: 25,\n",
" 6: 36,\n",
" 7: 49,\n",
" 8: 64,\n",
" 9: 81,\n",
" 10: 100,\n",
" 11: 121,\n",
" 12: 144,\n",
" 13: 169,\n",
" 14: 196}"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"{i:i**2 for i in range(1,15)}"
]
},
{
Expand All @@ -220,12 +323,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 51,
"id": "a1d55cea-96c3-4853-8220-17c0904a8816",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[(1, 3), (1, 4), (2, 3), (2, 4)]\n"
]
}
],
"source": [
"# your code goes here"
"list1 = [1, 2]\n",
"list2 = [3, 4]\n",
"\n",
"pairs = [(a, b) for a in list1 for b in list2]\n",
"print(pairs)\n"
]
},
{
Expand All @@ -249,9 +364,25 @@
"execution_count": null,
"id": "0175239c-87fa-4ba0-8776-d62567256db7",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[('a', 1), ('a', 2), ('b', 3), ('b', 4)]"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"input_dict = {\"a\": [1, 2], \"b\": [3, 4]}\n",
"[(key, value) for key, values in input_dict.items() for value in values]\n",
"\n",
"\n",
"\n",
"\n"
]
},
{
Expand Down Expand Up @@ -281,12 +412,23 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 66,
"id": "248918d0-f082-40a8-9d5c-c7b4ffd2bfca",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[(1, 3), (1, 4), (2, 3), (2, 4), (5, 7), (5, 8), (6, 7), (6, 8)]\n"
]
}
],
"source": [
"# your code goes here"
"input_data = [([1, 2], [3, 4]), ([5, 6], [7, 8])]\n",
"\n",
"output = [(a, b) for list1, list2 in input_data for a in list1 for b in list2]\n",
"print(output)\n"
]
},
{
Expand All @@ -308,18 +450,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 67,
"id": "c57d9e77-6859-45dd-a2e6-2c1898f1baa2",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{('a', 'd'), ('b', 'd'), ('b', 'c'), ('a', 'c')}\n"
]
}
],
"source": [
"# your code goes here"
"input_data = [\"ab\", \"cd\"]\n",
"\n",
"output = set([(a, b) for a in input_data[0] for b in input_data[1]])\n",
"print(output)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -333,7 +486,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.11.9"
}
},
"nbformat": 4,
Expand Down