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
80 changes: 65 additions & 15 deletions lab-python-list-comprehension.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,30 @@
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"ages = []\n",
"handicaps = []\n",
"valid_age = False\n",
"valid_handicap = False\n",
"while valid_age == False:\n",
" try:\n",
" age = int(input(\"Enter your age: \"))\n",
" if age < 0:\n",
" raise ValueError(\"Age cannot be negative.\")\n",
" else:\n",
" valid_age = True\n",
" while valid_handicap == False:\n",
" handicap = int(input(\"Enter your handicap: \"))\n",
" if handicap < -2 or handicap > 26:\n",
" raise ValueError(\"Handicap must be between -2 and 26.\")\n",
" else:\n",
" valid_handicap = True\n",
" ages.append(age)\n",
" handicaps.append(handicap)\n",
" categories = [\"Senior\" if age >= 55 and handicap > 7 else \"Open\" for age, handicap in zip(ages, handicaps)]\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter valid age and handicap.\")\n",
"print([(age, handicap)])\n",
"print(categories)"
]
},
{
Expand All @@ -100,7 +123,18 @@
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"def sum_multiples_of_3_and_5():\n",
" valid_number = False\n",
" while valid_number == False:\n",
" try:\n",
" number = int(input(\"Enter a number to find the sum of all multiples of 3 or 5 below it: \")) \n",
" if number < 0:\n",
" raise ValueError(\"Number cannot be negative.\")\n",
" else:\n",
" valid_number = True\n",
" return sum(i for i in range(number) if i % 3 == 0 or i % 5 == 0)\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a valid integer.\")"
]
},
{
Expand All @@ -127,12 +161,24 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 68,
"id": "a8cb1579-7065-4fc0-bd53-f91c2ad1dad9",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"def digits_in_order():\n",
" valid_number = False\n",
" while valid_number == False:\n",
" try:\n",
" number = int(input(\"Enter a number: \"))\n",
" if number < 0:\n",
" raise ValueError(\"Number cannot be negative.\")\n",
" else:\n",
" valid_number = True\n",
" lst = [int(i) for i in str(number)]\n",
" return lst\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a valid integer.\")"
]
},
{
Expand All @@ -155,12 +201,14 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 75,
"id": "86ebe759-76d8-4012-8590-c48a473a6099",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"def reverse_list(lst):\n",
" inverse_list = [i*-1 for i in lst]\n",
" return inverse_list"
]
},
{
Expand Down Expand Up @@ -193,12 +241,12 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 83,
"id": "6711881b-450a-44cb-a3d0-367b2c6a4464",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"square_dict = {i: i**2 for i in range(1, 16)}"
]
},
{
Expand All @@ -220,12 +268,13 @@
},
{
"cell_type": "code",
"execution_count": null,
"id": "a1d55cea-96c3-4853-8220-17c0904a8816",
"execution_count": 93,
"id": "d693c3d2",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"def all_pairs(lst1, lst2):\n",
" return [(x, y) for x in lst1 for y in lst2]"
]
},
{
Expand All @@ -246,12 +295,13 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 95,
"id": "0175239c-87fa-4ba0-8776-d62567256db7",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
"def dict_pairs(d):\n",
" return [(key, value) for key, values in d.items() for value in values]"
]
},
{
Expand Down Expand Up @@ -319,7 +369,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -333,7 +383,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.9"
}
},
"nbformat": 4,
Expand Down