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
180 changes: 162 additions & 18 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,62 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"# Your code here"
"# Your code here\n",
"student_grades= []\n",
"for i in range(5):\n",
" grade = float(input(\"Enter grade for student {}: \".format(i+1)))\n",
" student_grades.append(grade)\n",
" sum_grades = sum(student_grades)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The sum of the grades is: 447.0\n"
]
}
],
"source": [
"print(\"The sum of the grades is: \", sum_grades)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The selected students are: [89.0, 89.0, 90.0]\n",
"Length of the list: 3\n",
"Number of times 5 appears: 0\n"
]
}
],
"source": [
"new_list = []\n",
"\n",
"for i in [0, 2, 4]:\n",
" new_list.append(student_grades[i])\n",
"\n",
"new_list.sort()\n",
"\n",
"print(\"The selected students are:\", new_list)\n",
"print(\"Length of the list:\", len(new_list))\n",
"print(\"Number of times 5 appears:\", new_list.count(5))"
]
},
{
Expand Down Expand Up @@ -95,11 +146,47 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The first fruit is: apple\n",
"The last fruit is: elderberry\n"
]
}
],
"source": [
"# Your code here"
"# Your code here\n",
"fruits = (\"apple\", \"banana\", \"cherry\", \"date\", \"elderberry\")\n",
"# Accessing elements\n",
"print(\"The first fruit is:\", fruits[0])\n",
"print(\"The last fruit is:\", fruits[-1])"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Combined fruits: ('apple', 'banana', 'cherry', 'elderberry', 'fig', 'grape')\n"
]
}
],
"source": [
"new_fruits=(\"apple\", \"grape\", \"cherry\", \"date\", \"elderberry\")\n",
"fruits3=(\"apple\", \"banana\", \"cherry\", \"date\", \"elderberry\", \"fig\", \"grape\")\n",
"long_fruits=fruits+fruits3\n",
"first_three=long_fruits[:3]\n",
"last_three=long_fruits[-3:]\n",
"combined_fruits=first_three+last_three\n",
"print(\"Combined fruits:\", combined_fruits)"
]
},
{
Expand Down Expand Up @@ -136,7 +223,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -163,11 +250,41 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"41\n",
"42\n",
"{'hold', 'fire', 'also', 'ice', 'perish', 'great', 'favor', 'destruction', 'twice', 'the', 'hate', 'suffice', 'would', 'in', 'tasted', 'desire', 'will', 'i’ve', 'world', 'for'}\n",
"{'test', 'we', 'are', \"it's\", 'see', 'side', 'seen', 'away', 'deem', 'still', 'dream', 'a', 'love', 'today', 'as', 'quest', 'though', 'life', 'made', 'fades', \"i've\"}\n",
"['and', 'but', 'end', 'enough', 'from', 'had', 'i', 'if', 'is', 'it', 'know', 'of', 'say', 'some', 'that', 'think', 'those', 'to', 'what', 'who', 'with']\n"
]
}
],
"source": [
"# Your code here"
"# Your code here\n",
"poem_clean = poem.lower().replace(\",\", \"\").replace(\".\", \"\")\n",
"new_poem_clean = new_poem.lower().replace(\",\", \"\").replace(\".\", \"\")\n",
"\n",
"poem_set = set(poem_clean.split())\n",
"new_poem_set = set(new_poem_clean.split())\n",
"\n",
"print(len(poem_set))\n",
"print(len(new_poem_set))\n",
"\n",
"poem_unique_words = poem_set.difference(new_poem_set)\n",
"new_poem_unique_words = new_poem_set.difference(poem_set)\n",
"common_words = poem_set.intersection(new_poem_set)\n",
"\n",
"print(poem_unique_words)\n",
"print(new_poem_unique_words)\n",
"print(sorted(common_words))\n",
"\n",
"\n"
]
},
{
Expand All @@ -193,7 +310,7 @@
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -202,11 +319,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 19,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'Alice': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}, 'Bob': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 100}}\n"
]
}
],
"source": [
"# Your code here"
"# Your code here\n",
"grades['Bob']['Philosophy'] = 100\n",
"print(grades)"
]
},
{
Expand Down Expand Up @@ -275,17 +402,34 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 20,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'name': 'Kyle', 'age': 35, 'city': 'Amsterdam'}\n"
]
}
],
"source": [
"# Your code here"
"# Your code here\n",
"# Given lists\n",
"list1 = [\"name\", \"age\", \"city\"]\n",
"list2 = [\"Kyle\", 35, \"Amsterdam\"]\n",
"\n",
"# Convert lists into a dictionary\n",
"result = dict(zip(list1, list2))\n",
"\n",
"# Print the dictionary\n",
"print(result)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -299,7 +443,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.9.10"
}
},
"nbformat": 4,
Expand Down