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
278 changes: 250 additions & 28 deletions lab-python-list-comprehension.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,64 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"id": "21625526-3fae-4c55-bab5-f91940070681",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Open', 'Open', 'Senior', 'Open', 'Open', 'Senior']\n"
]
}
],
"source": [
"# your code goes here\n",
"\n",
"# inventory = []\n",
"# for i in range(6):\n",
"# age, handicap = map(int, input(\"Enter age and handicap (comma-separated): \").split(\",\"))\n",
"# inventory.append([age, handicap])\n",
"\n",
"# print(inventory)\n",
"\n",
"input = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]]\n",
"\n",
"category = ['Senior' if member[1] >= 7 and member[0] >= 55 else 'Open' for member in input]\n",
"\n",
"print(category)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ec8dda3f",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"list_members = [\"Juan\", \"Pedro\", \"Maria\"]\n",
"inventory = {}\n",
"\n",
"for member in list_members:\n",
" age_handicap = input(f\"Enter age and handicap for {member} (comma-separated): \")\n",
" age, handicap = map(int, age_handicap.split(\",\"))\n",
" inventory[member] = {\"age\": age, \"handicap\": handicap}\n",
"\n",
"print(inventory)\n",
"\n",
"Categories = {}\n",
"for member in inventory:\n",
" if inventory[member]['handicap'] >= 7 and inventory[member]['age'] >= 55:\n",
" category ='Senior'\n",
" Categories[member] = category\n",
" else:\n",
" category = 'Open'\n",
" Categories[member] = category\n",
" \n",
"print(Categories)\n",
"\n",
"category = ['Senior' if inventory[member]['handicap'] >= 7 and inventory[member]['age'] >= 55 else 'Open' for member in inventory]"
]
},
{
Expand All @@ -95,12 +147,37 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 21,
"id": "53061dce-7aa3-4476-8b56-71413c5e135c",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"60\n"
]
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"ref = 15\n",
"\n",
"# total = 0\n",
"# new_array = set()\n",
"\n",
"# for num in range(ref+1):\n",
"# if num < 0:\n",
"# total += 0\n",
"# elif (num % 3 == 0 or num % 5 == 0) and num > 0:\n",
"# total += num\n",
"# new_array.add(num)\n",
"\n",
"# print(sorted(new_array))\n",
"# print(total)\n",
"\n",
"total = int(sum(num if num > 0 and (num % 3 == 0 or num % 5 == 0) else 0 for num in range(ref+1)))\n",
"print(total)\n"
]
},
{
Expand All @@ -127,12 +204,26 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "a8cb1579-7065-4fc0-bd53-f91c2ad1dad9",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[8, 6, 7, 5, 3, 0, 9]\n"
]
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"\n",
"input = 8675309\n",
"\n",
"list_digits = [int(digit) for digit in str(input)]\n",
"\n",
"print(list_digits)"
]
},
{
Expand All @@ -155,12 +246,36 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 20,
"id": "86ebe759-76d8-4012-8590-c48a473a6099",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[-1, 2, -3, 4, -5]\n"
]
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"\n",
"list = ([1,-2,3,-4,5])\n",
"invert_numbers = []\n",
"\n",
"# for digit in list:\n",
"# if (digit) > 0:\n",
"# invert_numbers.append(int(digit) * -1)\n",
"# elif int(digit) < 0:\n",
"# invert_numbers.append(int(digit) * -1)\n",
"# elif int(digit) == 0:\n",
"# invert_numbers.append(0)\n",
"\n",
"# print(invert_numbers)\n",
"\n",
"invert_numbers = [int(digit) * -1 if digit != 0 else 0 for digit in list]\n",
"print(invert_numbers)\n"
]
},
{
Expand Down Expand Up @@ -193,12 +308,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 27,
"id": "6711881b-450a-44cb-a3d0-367b2c6a4464",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{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"
"# your code goes here\n",
"\n",
"dict = {}\n",
"\n",
"# for i in range(1, 16):\n",
"# dict[i] = i ** 2\n",
"\n",
"dict = {i: i ** 2 for i in range(1, 16)}\n",
"\n",
"print(dict)"
]
},
{
Expand All @@ -220,12 +352,35 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 23,
"id": "a1d55cea-96c3-4853-8220-17c0904a8816",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'tuple'>\n",
"[(2, 4), (2, 6), (5, 4), (5, 6)]\n"
]
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"\n",
"list_of_lists = ([2, 5], [4, 6])\n",
"\n",
"print(type(list_of_lists))\n",
"# output = []\n",
"\n",
"# for num in list_of_lists[0]:\n",
"# for num1 in list_of_lists[1]:\n",
"# output.append((num,num1))\n",
"\n",
"output = [(num, num1) for num in list_of_lists[0] for num1 in list_of_lists[1]]\n",
"# ...existing code...\n",
"\n",
"print(output)"
]
},
{
Expand All @@ -246,12 +401,31 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 49,
"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"
"# your code goes here\n",
"\n",
"Input = {\"a\": [1, 2], \"b\": [3, 4]}\n",
"output = []\n",
"\n",
"# for key, values in Input.items():\n",
"# for num in values:\n",
"# print(key, num)\n",
" \n",
"output = [(key,num) for key, values in Input.items() for num in values]\n",
"\n",
"print(output)\n"
]
},
{
Expand Down Expand Up @@ -281,12 +455,41 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 37,
"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"
"# your code goes here\n",
"exercise = [([1, 2], [3, 4]), ([5, 6], [7, 8])]\n",
"\n",
"# print(type(exercise[0]))\n",
"# print(exercise[0])\n",
"# print(exercise[0][1])\n",
"\n",
"# output = []\n",
"\n",
"# for num in exercise[0][0]:\n",
"# for num1 in exercise[0][1]:\n",
"# output.append((num, num1))\n",
" \n",
"# for num in exercise[1][0]:\n",
"# for num1 in exercise[1][1]:\n",
"# output.append((num, num1))\n",
"\n",
"# print(output)\n",
"\n",
"output = [(num, num1) for num in exercise[0][0] for num1 in exercise[0][1]] + [(num, num1) for num in exercise[1][0] for num1 in exercise[1][1]]\n",
"\n",
"print(output)\n"
]
},
{
Expand All @@ -308,18 +511,37 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 82,
"id": "c57d9e77-6859-45dd-a2e6-2c1898f1baa2",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd')]\n"
]
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"\n",
"input_strings = [\"ab\", \"cd\"]\n",
"\n",
"pair1 = input_strings[0]\n",
"pair2 = input_strings[1]\n",
"\n",
"output = []\n",
"\n",
"output = [(s1, s2) for s1 in input_strings[0] for s2 in input_strings[1]]\n",
"\n",
"print(output)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -333,7 +555,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.6"
}
},
"nbformat": 4,
Expand Down