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
282 changes: 259 additions & 23 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,65 @@
"metadata": {},
"outputs": [],
"source": [
"# Your code here"
"# create list of students\n",
"students = ['Jenny', 'Kevin', 'Josh', 'Mary', 'Fleur']\n",
"# create empty list for the grades\n",
"grades = []\n",
"# ask for input\n",
"for student in students:\n",
" grade_input = int(input(f'Please enter {student} first grade:'))\n",
" #add grades to list grades\n",
" grades.append(grade_input)\n",
"\n",
"#caculate the sum of the grades\n",
"sum_grades = sum(grades)\n",
"print(sum_grades)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[7, 9, 9]\n"
]
}
],
"source": [
"#filter grade of student 1,3 and 5 and sort ascending\n",
"filtered_grades = [grades[0],grades[2],grades[4]]\n",
"filtered_grades.sort()\n",
"print(filtered_grades)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"There are 3 in filtered_grades, noone scored a 5\n"
]
}
],
"source": [
"# print out the new list, along with its length and the number of occurrences of the score 5 in the list.\n",
"grade_5 = 0\n",
"if filtered_grades == 5:\n",
" for grade in filtered_grades:\n",
" grade_5 = + 1\n",
" print(f'There are{grade_5} students that scored a 5')\n",
" print(f'There are {len(filtered_grades)} in filtered_grades')\n",
"else:\n",
" print(f'There are {len(filtered_grades)} in filtered_grades, noone scored a 5')\n"
]
},
{
Expand Down Expand Up @@ -95,11 +153,44 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 48,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The first fruit on the list is Grape, the last Fruit on the list is Soursop\n",
"('Grape', 'Mangostene', 'Apple', 'Pear', 'Soursop')\n",
"('Grape', 'Mangostene', 'Apple', 'Pear', 'Soursop', 'Manderin', 'Pineapple')\n",
"first tuple ('Grape', 'Mangostene', 'Apple')\n",
"second tuple ('Soursop', 'Manderin', 'Pineapple')\n",
"('Grape', 'Mango', 'Apple', 'Pear', 'Soursop', 'Grape', 'Mangostene', 'Apple', 'Soursop', 'Manderin', 'Pineapple') 11\n"
]
}
],
"source": [
"# Your code here"
"#create a tuple\n",
"Fruits = ('Grape', 'Mango', 'Apple', 'Pear', 'Soursop')\n",
"#output first and last element of tuple\n",
"print(f'The first fruit on the list is {Fruits[0]}, the last Fruit on the list is {Fruits[-1]}')\n",
"#convert to list to change tuple\n",
"Fruits2 = list(Fruits)\n",
"Fruits2[1] = 'Mangostene'\n",
"Fruits2 = tuple(Fruits2)\n",
"print(Fruits2)\n",
"#add old tuple to new tuple\n",
"Fruits3 = ('Manderin', 'Pineapple')\n",
"new_tuple = Fruits2 + Fruits3\n",
"print(new_tuple)\n",
"#split new tuple in 2. \n",
"tuple_1 = new_tuple[:3]\n",
"tuple_2 = new_tuple[-3:]\n",
"print('first tuple', tuple_1)\n",
"print('second tuple', tuple_2)\n",
"#combine tuples\n",
"last_tuple = Fruits + tuple_1 + tuple_2\n",
"print(last_tuple, len(last_tuple))"
]
},
{
Expand Down Expand Up @@ -136,13 +227,13 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"poem = \"\"\"Some say the world will end in fire,\n",
"Some say in ice.\n",
"From what Ive tasted of desire\n",
"From what I've tasted of desire\n",
"I hold with those who favor fire.\n",
"But if it had to perish twice,\n",
"I think I know enough of hate\n",
Expand All @@ -163,11 +254,129 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 19,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'desire', 'suffice', 'some', 'that', 'if', 'the', 'perish', 'i', 'say', 'know', 'twice', 'world', 'tasted', 'it', 'fire', 'but', 'destruction', 'enough', 'think', 'to', 'had', 'also', 'what', 'who', 'ice', 'and', 'will', 'hold', 'those', 'would', 'end', 'with', 'is', 'in', 'for', 'of', 'favor', 'ive', 'from', 'hate', 'great'}\n"
]
}
],
"source": [
"#Create two sets, one for each poem, containing all unique words in both poems (ignoring case and punctuation).\n",
"# remove the punctuation and make lowercase.\n",
"punctuation = \",.'\"\n",
"poem_lower = poem.lower()\n",
"#make words into clean string\n",
"poem_words = ''.join([char for char in poem_lower if char not in punctuation])\n",
"#make string into set split at the spaces\n",
"unique_words = set(poem_words.split())\n",
"print(unique_words)\n"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'are', 'we', 'some', 'that', 'if', 'away', 'i', 'say', 'know', 'it', 'but', 'enough', 'think', 'see', 'life', 'to', 'quest', 'had', 'test', 'what', 'who', 'fades', 'and', 'as', 'those', 'though', 'a', 'deem', 'with', 'its', 'is', 'end', 'made', 'side', 'of', 'seen', 'dream', 'ive', 'from', 'love', 'today', 'still'}\n"
]
}
],
"source": [
"# Your code here"
"#same for second poem\n",
"# remove the punctuation and make lowercase.\n",
"punctuation = \",.'\"\n",
"new_poem_lower = new_poem.lower()\n",
"#make words into clean string\n",
"new_poem_words = ''.join([char for char in new_poem_lower if char not in punctuation])\n",
"#make string into set split at the spaces\n",
"unique_words2 = set(new_poem_words.split())\n",
"print(unique_words2)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"41\n",
"43\n"
]
}
],
"source": [
"#Print the number of unique words in each set.\n",
"print(len(unique_words))\n",
"print(len(unique_words2))"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'desire', 'suffice', 'the', 'perish', 'twice', 'world', 'tasted', 'destruction', 'fire', 'also', 'ice', 'will', 'hold', 'would', 'in', 'for', 'favor', 'hate', 'great'}\n"
]
}
],
"source": [
"#Identify and print the unique words present in the first poem but not in the second one.\n",
"extra_unique_words = unique_words - unique_words2\n",
"print(extra_unique_words)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'are', 'we', 'away', 'see', 'life', 'quest', 'test', 'fades', 'as', 'though', 'a', 'deem', 'made', 'its', 'side', 'seen', 'dream', 'love', 'today', 'still'}\n"
]
}
],
"source": [
"#Identify and print the unique words present in the second poem but not in the first one.\n",
"extra_unique_words2 = unique_words2 - unique_words\n",
"print(extra_unique_words2)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['a', 'also', 'are', 'as', 'away', 'deem', 'desire', 'destruction', 'dream', 'fades', 'favor', 'fire', 'for', 'great', 'hate', 'hold', 'ice', 'in', 'its', 'life', 'love', 'made', 'perish', 'quest', 'see', 'seen', 'side', 'still', 'suffice', 'tasted', 'test', 'the', 'though', 'today', 'twice', 'we', 'will', 'world', 'would']\n"
]
}
],
"source": [
"#Identify and print the unique words present in both poems and print it in alphabetical order.\n",
"all_unique_words = sorted(extra_unique_words.union(extra_unique_words2))\n",
"print(all_unique_words)\n"
]
},
{
Expand All @@ -193,7 +402,7 @@
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -202,11 +411,20 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 28,
"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"
"grades['Bob']['Philosophy'] = 100\n",
"print(grades)"
]
},
{
Expand Down Expand Up @@ -239,14 +457,22 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 41,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}\n"
]
}
],
"source": [
"keys = ['Physics', 'Math', 'Chemistry', 'Philosophy']\n",
"values = [75, 85, 60,90]\n",
"\n",
"# Your code here"
"values = [75, 85, 60, 90]\n",
"grades = {key: value for key, value in zip(keys, values)}\n",
"print(grades)\n"
]
},
{
Expand Down Expand Up @@ -275,17 +501,27 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 43,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" the subject with the lowest score is Chemistry: 60\n"
]
}
],
"source": [
"# Your code here"
"min_subject = min(grades, key=grades.get)\n",
"min_score = grades[min_subject]\n",
"print(f' the subject with the lowest score is {min_subject}: {min_score}')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -299,7 +535,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.14.2"
}
},
"nbformat": 4,
Expand Down