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
214 changes: 186 additions & 28 deletions lab-python-list-comprehension.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,25 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"id": "21625526-3fae-4c55-bab5-f91940070681",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"['Open', 'Open', 'Senior', 'Open', 'Open', 'Senior']"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"input = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]]\n",
"output = [\"Senior\" if age >=55 and handicap >=7 else \"Open\" for age, handicap in input]\n",
"output"
]
},
{
Expand All @@ -95,12 +108,26 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"id": "53061dce-7aa3-4476-8b56-71413c5e135c",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"45\n"
]
}
],
"source": [
"# your code goes here"
"def sum_of_multiples(n):\n",
" if n< 0:\n",
" return 0\n",
" else:\n",
" multiple = [num for num in range(1,15) if num %3 == 0 or num %5 == 0 ]\n",
" solution = sum(multiple)\n",
"print(solution)"
]
},
{
Expand All @@ -127,12 +154,22 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 16,
"id": "a8cb1579-7065-4fc0-bd53-f91c2ad1dad9",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
]
}
],
"source": [
"# your code goes here"
"def split_numbers(n):\n",
" return [int(char) for char in str(n)]\n",
"print(split_numbers(123456789))"
]
},
{
Expand All @@ -155,12 +192,23 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"id": "86ebe759-76d8-4012-8590-c48a473a6099",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[-1, -2, -3, 4, 5, 6]\n"
]
}
],
"source": [
"# your code goes here"
"def invert(numbers):\n",
" return[-num for num in numbers]\n",
"\n",
"print(invert([1,2,3,-4,-5,-6]))\n"
]
},
{
Expand Down Expand Up @@ -193,12 +241,38 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 23,
"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,\n",
" 15: 225}"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"dictionary = {key: key**2 for key in range(1,16)}\n",
"dictionary"
]
},
{
Expand All @@ -220,12 +294,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 31,
"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"
"from itertools import product\n",
"\n",
"def generate_pairs(list1, list2):\n",
" return list(product(list1, list2))\n",
"\n",
"list1 = [1,2]\n",
"list2 = [3,4] \n",
"output = generate_pairs(list1, list2)\n",
"print(output)"
]
},
{
Expand All @@ -249,9 +339,33 @@
"execution_count": null,
"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 generate_key_value_pairs(dict_of_lists):\n",
" # Create a list to hold all the key-value pairs\n",
" result = []\n",
" \n",
" # Iterate over dictionary items using key-value pairs\n",
" for key, value_list in dict_of_lists.items():\n",
" # Iterate over each value in the corresponding list\n",
" for value in value_list:\n",
" # Append the key-value pair to the result list\n",
" result.append((key, value))\n",
"\n",
" return result\n",
"\n",
"\n",
"input_data = {\"a\": [1, 2], \"b\": [3, 4]}\n",
"output = generate_key_value_pairs(input_data)\n",
"print(output) "
]
},
{
Expand Down Expand Up @@ -281,12 +395,32 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 33,
"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"
"def generate_pairs_from_tuples_of_lists(tuples_of_lists):\n",
" result = []\n",
" for list1, list2 in tuples_of_lists:\n",
" # Create pairs from the two lists using list comprehension\n",
" pairs = [(x, y) for x in list1 for y in list2]\n",
" # Extend the result list with these pairs\n",
" result.extend(pairs)\n",
" return result\n",
"\n",
"# Example usage:\n",
"input_data = [([1, 2], [3, 4]), ([5, 6], [7, 8])]\n",
"output = generate_pairs_from_tuples_of_lists(input_data)\n",
"print(output)"
]
},
{
Expand All @@ -308,18 +442,42 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 34,
"id": "c57d9e77-6859-45dd-a2e6-2c1898f1baa2",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{('b', 'd'), ('b', 'c'), ('a', 'c'), ('a', 'd')}\n"
]
}
],
"source": [
"# your code goes here"
"def generate_character_pairs(strings):\n",
" # Ensure the input contains exactly two strings\n",
" if len(strings) != 2:\n",
" raise ValueError(\"The input must contain exactly two strings.\")\n",
" \n",
" # Unpack the list into two strings\n",
" string1, string2 = strings\n",
" \n",
" # Use a set to store unique pairs\n",
" pairs = {(char1, char2) for char1 in string1 for char2 in string2}\n",
"\n",
" return pairs\n",
"\n",
"# Example usage:\n",
"input_data = [\"ab\", \"cd\"]\n",
"output = generate_character_pairs(input_data)\n",
"print(output)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -333,7 +491,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down