Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment completed. #5

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 8,
"id": "c0986d3a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Original list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
"Squared list: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]\n",
"Even numbers: [2, 4, 6, 8, 10]\n",
"Product of even numbers: 3840\n"
]
}
],
"source": [
"# Write a Python script to implement the map(), filter(), and reduce() functions on a list of integers.\n",
"# The map() function should square each element of the list,\n",
"# The filter() function should remove all odd numbers from the list.\n",
"# The reduce() function should find the product of all the even numbers in the list.\n",
"\n",
"from functools import reduce\n",
"\n",
"\n",
"numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
"\n",
"squared_numbers = list(map(lambda x: x**2, numbers))\n",
"\n",
"even_numbers = list(filter(lambda x: x%2 == 0, numbers))\n",
"\n",
"# Use reduce() to find the product of all the even numbers in the list\n",
"product = reduce(lambda x, y: x*y, even_numbers)\n",
"\n",
"print(\"Original list:\", numbers)\n",
"print(\"Squared list:\", squared_numbers)\n",
"print(\"Even numbers:\", even_numbers)\n",
"print(\"Product of even numbers:\", product)\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
153 changes: 153 additions & 0 deletions .ipynb_checkpoints/OOP_assignment5-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 7,
"id": "b46a568f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class 'int'> <class 'str'>\n",
"\n",
"<class 'type'>\n",
"<class 'type'>\n",
"<class 'type'>\n",
"<class 'type'>\n",
"<class 'type'>\n",
"<class 'builtin_function_or_method'>\n",
"<class 'function'>\n",
"<class 'type'>\n",
"**********************\n",
"<class 'int'>\n",
"<class 'str'>\n",
"<class 'tuple'>\n",
"<class 'dict'>\n",
"<class 'list'>\n",
"<class 'NoneType'>\n",
"<class 'NoneType'>\n",
"<class '__main__.Someclass'>\n"
]
}
],
"source": [
"a = 5\n",
"b = 'some string'\n",
"print(type(a),type(b))\n",
"\n",
"def somefunction():\n",
" return\n",
"\n",
"class Someclass:\n",
" pass\n",
"\n",
"someList = [int, str, tuple, dict, list, print, somefunction, Someclass]\n",
"anotherList = [int(), str(), tuple(), dict(), list(), print(), somefunction(), Someclass()]\n",
"\n",
"for item in someList:\n",
" print(type(item))\n",
"\n",
"print('**********************') \n",
"\n",
"for item in anotherList:\n",
" print(type(item))"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "1513574d",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n",
"3\n",
"Max\n",
"Kitty\n",
"Buddy\n",
"10\n",
"9\n",
"12\n",
"Labrador Retriever does woof woof\n"
]
}
],
"source": [
"class Pet:\n",
" pet_population = 0\n",
" pet_instance = []\n",
" current_year = 2023\n",
"\n",
" def __init__(self, name, category, age):\n",
" self.name = name\n",
" self.category = category\n",
" self.age = age\n",
" Pet.pet_population += 1\n",
" Pet.pet_instance.append(self)\n",
"\n",
" def hellofrompet(self):\n",
" print(\"Hello from\", self.name)\n",
"\n",
" @classmethod\n",
" def timepassedby(cls, years):\n",
" for pet in cls.pet_instance:\n",
" pet.age += years\n",
"\n",
"class Dog(Pet):\n",
" def __init__(self, name, breed, age):\n",
" super().__init__(name, 'dog', age)\n",
" self.breed = breed\n",
"\n",
" def barks(self):\n",
" print(self.breed, \"does woof woof\")\n",
"\n",
" \n",
"p1 = Pet(\"Max\", \"dog\", 3)\n",
"p2 = Pet(\"Kitty\", \"cat\", 2)\n",
"\n",
"\n",
"d1 = Dog(\"Buddy\", \"Labrador Retriever\", 5)\n",
"\n",
"Pet.timepassedby(2)\n",
"\n",
"print(Pet.pet_population)\n",
"print(Dog.pet_population)\n",
"\n",
"for item in Pet.pet_instance:\n",
" print(item.name)\n",
"Pet.timepassedby(5)\n",
"print(p1.age)\n",
"print(p2.age)\n",
"print(d1.age)\n",
"\n",
"d1.barks()\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
61 changes: 61 additions & 0 deletions .ipynb_checkpoints/flow_control_assignment2-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "ebf44052",
"metadata": {},
"outputs": [],
"source": [
"# Conditionals:\n",
"# Get the radius value as input and calculate the area of the circle. \n",
"# If the input is numeric, display the result otherwise display any other message.\n",
"# If the user enters numeric value then ask user for either perimeter or area calculation and display accordingly.\n",
"\n",
"def circle_calc():\n",
" radius_input = input(\"Enter the radius of the circle: \")\n",
" \n",
"\n",
" if radius_input.isnumeric():\n",
"\n",
" radius = float(radius_input)\n",
"\n",
"\n",
" calculation_type = input(\"Do you want to calculate area or perimeter? (a/p): \")\n",
"\n",
" if calculation_type == 'a':\n",
"\n",
" area = math.pi * radius ** 2\n",
" print(f\"The area of the circle with radius {radius} is {area:.2f}.\")\n",
" elif calculation_type == 'p':\n",
" perimeter = 2 * math.pi * radius\n",
" print(f\"The perimeter of the circle with radius {radius} is {perimeter:.2f}.\")\n",
" else:\n",
" print(\"Invalid input. Please enter 'a' for area or 'p' for perimeter.\")\n",
" else:\n",
" print(\"Invalid input. Please enter a numeric value for the radius.\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 5
}
Loading