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
310 changes: 283 additions & 27 deletions lab-python-data-structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,99 @@
"- *[Python Built-in Functions](https://docs.python.org/3/library/functions.html)*\n"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'Pedro': 5, 'Juan': 10, 'Ines': 2, 'Julia': 9, 'Pablo': 5}\n",
"<class 'dict'>\n",
"Total grades are: 31\n",
"All grades are: [5, 10, 2, 9, 5]\n",
"Selected grades (sorted): [2, 5, 5]\n",
"Length of selected grades: 3\n",
"Occurrences of score 5: 2\n",
"<class 'list'>\n"
]
}
],
"source": [
"# Your code here\n",
"\n",
"students_grades = {}\n",
"\n",
"for i in range(5):\n",
" student_name = input(f\"Enter the student name: \")\n",
" grade = int(input(f\"Enter the grade for {student_name}: \"))\n",
" students_grades[student_name] = grade\n",
"\n",
"print(students_grades)\n",
"print(type(students_grades))\n",
"\n",
"total_grades = 0\n",
"grades_list = []\n",
"for grade in students_grades.values():\n",
" total_grades += int(grade)\n",
" grades_list.append(int(grade))\n",
"\n",
"print(f\"Total grades are: {total_grades}\")\n",
"print(f\"All grades are: {grades_list}\")\n",
"\n",
"selected_grades = (grades_list)[::2]\n",
"print(\"Selected grades (sorted):\", sorted(selected_grades))\n",
"print(\"Length of selected grades:\", len(selected_grades))\n",
"print(\"Occurrences of score 5:\", selected_grades.count(5))\n",
"print(type(selected_grades))\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'Pedro': 5, 'Juan': 2, 'Ines': 9, 'Sandra': 7, 'Pablo': 5}\n",
"<class 'dict'>\n",
"28\n"
]
}
],
"source": [
"# Your code here"
"# Your code here\n",
"\n",
"students_grades = {}\n",
"\n",
"for i in range(5):\n",
" \n",
" student_name = str(input(f\"Enter the student name: \"))\n",
" grade = int(input(f\"Enter the grade for {student_name}: \"))\n",
" students_grades[student_name] = grade\n",
"\n",
"print(students_grades)\n",
"print(type(students_grades))\n",
"\n",
"total_grades = 0\n",
"grades_list = []\n",
"\n",
"for student, grade in students_grades.items():\n",
" grade = int(grade) \n",
" total_grades += grade\n",
" grades_list.append(grade) \n",
"\n",
"selected_grades = (grades_list)[::2]\n",
"\n",
"#print(\"Selected grades (sorted):\", sorted(selected_grades))\n",
"print(\"Length of selected grades:\", len(selected_grades))\n",
"print(\"Occurrences of score 5:\", selected_grades.count(5))\n",
"print(type(selected_grades))"
]
},
{
Expand Down Expand Up @@ -95,11 +181,54 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 26,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['apple', 'banana', 'orange', 'pineapple', 'strawberry']\n",
"First fruit: apple\n",
"Last fruit: strawberry\n",
"After inserting kiwi: ['apple', 'kiwi', 'banana', 'orange', 'pineapple', 'strawberry']\n",
"After extending with fruits2: ['apple', 'kiwi', 'banana', 'orange', 'pineapple', 'strawberry', 'grape', 'mango']\n",
"First three elements: ['apple', 'kiwi', 'banana']\n",
"Last three elements: ['strawberry', 'grape', 'mango']\n",
"Fruits3: ['apple', 'kiwi', 'banana', 'orange', 'pineapple', 'strawberry', 'grape', 'mango', 'apple', 'kiwi', 'banana', 'strawberry', 'grape', 'mango']\n",
"Length of fruits3: 14\n"
]
}
],
"source": [
"# Your code here"
"# Your code here\n",
"\n",
"fruits = [\"apple\", \"banana\", \"orange\", \"pineapple\", \"strawberry\"]\n",
"\n",
"print(fruits)\n",
"\n",
"# Outputs the first and last elements of the tuple\n",
"print(\"First fruit:\", fruits[0])\n",
"print(\"Last fruit:\", fruits[-1])\n",
"\n",
"fruits.insert(1, \"kiwi\")\n",
"print(\"After inserting kiwi:\", fruits) \n",
"\n",
"fruits2 = [\"grape\", \"mango\"]\n",
"fruits.extend(fruits2)\n",
"\n",
"print(\"After extending with fruits2:\", fruits)\n",
"\n",
"first_3_elements = fruits[:3]\n",
"print(\"First three elements:\", first_3_elements)\n",
"last_3_elements = fruits[-3:]\n",
"print(\"Last three elements:\", last_3_elements)\n",
"\n",
"fruits3 = fruits.copy()\n",
"fruits3.extend(first_3_elements)\n",
"fruits3.extend(last_3_elements)\n",
"print(\"Fruits3:\", fruits3)\n",
"print(\"Length of fruits3:\", len(fruits3))"
]
},
{
Expand Down Expand Up @@ -136,7 +265,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -165,9 +294,102 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Cleaned poem: some say the world will end in firesome say in icefrom what i’ve tasted of desirei hold with those who favor firebut if it had to perish twicei think i know enough of hateto say that for destruction iceis also greatand would suffice\n",
"Unique words in cleaned poem: {'will', 'firebut', 'hold', 'world', 'desirei', 'those', 'icefrom', 'perish', 'also', 'in', 'destruction', 'the', 'for', 'end', 'that', 'iceis', 'with', 'favor', 'say', 'tasted', 'who', 'twicei', 'it', 'firesome', 'had', 'hateto', 'would', 'some', 'know', 'what', 'greatand', 'i', 'i’ve', 'of', 'to', 'think', 'enough', 'if', 'suffice'}\n",
"Number of unique words in cleaned poem: 39\n",
"Cleaned new poem: some say life is but a dreamsome say it's a testfrom what i've seen and what i deemi side with those who see it as a questbut if it had to end todayi think i know enough of loveto say that though it fades awayit's still what we are made of\n",
"Unique words in cleaned new poem: {'those', 'deemi', 'side', 'made', 'and', 'are', 'fades', 'dreamsome', 'end', 'is', 'though', 'that', 'with', 'say', 'who', 'life', 'a', 'loveto', 'still', 'it', 'as', 'had', 'seen', 'testfrom', 'some', 'we', 'questbut', 'todayi', \"i've\", 'know', 'what', 'i', 'see', 'of', 'to', 'think', 'enough', 'but', 'if', \"it's\", \"awayit's\"}\n",
"Number of unique words in cleaned new poem: 41\n",
"'will' is unique to the original poem.\n",
"'firebut' is unique to the original poem.\n",
"'hold' is unique to the original poem.\n",
"'world' is unique to the original poem.\n",
"'desirei' is unique to the original poem.\n",
"'icefrom' is unique to the original poem.\n",
"'perish' is unique to the original poem.\n",
"'also' is unique to the original poem.\n",
"'in' is unique to the original poem.\n",
"'destruction' is unique to the original poem.\n",
"'the' is unique to the original poem.\n",
"'for' is unique to the original poem.\n",
"'iceis' is unique to the original poem.\n",
"'favor' is unique to the original poem.\n",
"'tasted' is unique to the original poem.\n",
"'twicei' is unique to the original poem.\n",
"'firesome' is unique to the original poem.\n",
"'hateto' is unique to the original poem.\n",
"'would' is unique to the original poem.\n",
"'greatand' is unique to the original poem.\n",
"'i’ve' is unique to the original poem.\n",
"'suffice' is unique to the original poem.\n",
"'deemi' is unique to the new poem.\n",
"'side' is unique to the new poem.\n",
"'made' is unique to the new poem.\n",
"'and' is unique to the new poem.\n",
"'are' is unique to the new poem.\n",
"'fades' is unique to the new poem.\n",
"'dreamsome' is unique to the new poem.\n",
"'is' is unique to the new poem.\n",
"'though' is unique to the new poem.\n",
"'life' is unique to the new poem.\n",
"'a' is unique to the new poem.\n",
"'loveto' is unique to the new poem.\n",
"'still' is unique to the new poem.\n",
"'as' is unique to the new poem.\n",
"'seen' is unique to the new poem.\n",
"'testfrom' is unique to the new poem.\n",
"'we' is unique to the new poem.\n",
"'questbut' is unique to the new poem.\n",
"'todayi' is unique to the new poem.\n",
"'i've' is unique to the new poem.\n",
"'see' is unique to the new poem.\n",
"'but' is unique to the new poem.\n",
"'it's' is unique to the new poem.\n",
"'awayit's' is unique to the new poem.\n",
"Unique words in both poems and in alphabetical order: ['a', 'also', 'and', 'are', 'as', \"awayit's\", 'but', 'deemi', 'desirei', 'destruction', 'dreamsome', 'fades', 'favor', 'firebut', 'firesome', 'for', 'greatand', 'hateto', 'hold', \"i've\", 'icefrom', 'iceis', 'in', 'is', \"it's\", 'i’ve', 'life', 'loveto', 'made', 'perish', 'questbut', 'see', 'seen', 'side', 'still', 'suffice', 'tasted', 'testfrom', 'the', 'though', 'todayi', 'twicei', 'we', 'will', 'world', 'would']\n"
]
}
],
"source": [
"# Your code here"
"# Your code here\n",
"\n",
"# Create two sets, one for each poem, containing all unique words in both poems (ignoring case and punctuation).\n",
"\n",
"clean_poem = poem.lower().translate(str.maketrans('', '', '.,\\n'))\n",
"print(\"Cleaned poem:\", clean_poem)\n",
"clean_poem_words = set(clean_poem.split())\n",
"\n",
"print(\"Unique words in cleaned poem:\", clean_poem_words)\n",
"print(f\"Number of unique words in cleaned poem: {len(clean_poem_words)}\")\n",
"\n",
"clean_new_poem = new_poem.lower().translate(str.maketrans('', '', '.,\\n'))\n",
"print(\"Cleaned new poem:\", clean_new_poem)\n",
"clean_new_poem_words = set(clean_new_poem.split())\n",
"\n",
"print(\"Unique words in cleaned new poem:\", clean_new_poem_words)\n",
"print(f\"Number of unique words in cleaned new poem: {len(clean_new_poem_words)}\")\n",
"\n",
"unique_words_original = set()\n",
"unique_words_new = set()\n",
"\n",
"for word in clean_poem_words:\n",
" if word not in clean_new_poem_words:\n",
" print(f\"'{word}' is unique to the original poem.\")\n",
" unique_words_original.add(word)\n",
"\n",
"for word in clean_new_poem_words:\n",
" if word not in clean_poem_words:\n",
" print(f\"'{word}' is unique to the new poem.\")\n",
" unique_words_new.add(word)\n",
"\n",
"unique_words_combined = unique_words_original.union(unique_words_new)\n",
"\n",
"print(f\"Unique words in both poems and in alphabetical order: {sorted(unique_words_combined)}\")"
]
},
{
Expand Down Expand Up @@ -195,18 +417,28 @@
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"90\n",
"100\n",
"{'Alice': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}, 'Bob': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 100}}\n"
]
}
],
"source": [
"grades = {'Alice': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}, 'Bob': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Your code here"
"# Your code here\n",
"\n",
"grades = {'Alice': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}, 'Bob': {'Physics': 75, 'Math': 85, 'Chemistry': 60, 'Philosophy': 90}}\n",
"\n",
"print(grades['Bob']['Philosophy'])\n",
"\n",
"grades['Bob']['Philosophy'] = 100\n",
"print(grades['Bob']['Philosophy'])\n",
"\n",
"print(grades)"
]
},
{
Expand Down Expand Up @@ -239,14 +471,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 54,
"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"
"# new_dict = dict(zip(keys, values))\n",
"new_dict = {}\n",
"\n",
"for key, value in zip(keys, values):\n",
" new_dict[key] = value\n",
"\n",
"print(new_dict)"
]
},
{
Expand Down Expand Up @@ -275,17 +521,27 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 55,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"60 90\n"
]
}
],
"source": [
"# Your code here"
"# Your code here\n",
"\n",
"print(min(new_dict.values()), max(new_dict.values()))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -299,7 +555,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.6"
}
},
"nbformat": 4,
Expand Down