Answer To: My assignment is due tommorow
Vibhav answered on Oct 24 2021
New folder/GNPythonAssign.ipynb
{
"cells": [
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"import math\n",
"def solve_eqn(a, b, c):\n",
" '''\n",
" Parameters: Coeffcients for equation of the form a*x^2 + b*x + c = 0\n",
" '''\n",
" disc = b**2 - 4*a*c\n",
" if disc < 0:\n",
" print('Error! Discriminant < 0. Could not solve. ')\n",
" return None, None\n",
" x1 = (-b + math.sqrt(disc))/(2*a)\n",
" x2 = (-b - math.sqrt(disc))/(2*a)\n",
" \n",
" return x1, x2"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Input coefficients of the quadratic equation of the form ax^2+bx+c=0\n",
"Enter a: 2\n",
"Enber b:5\n",
"Enter c: 3\n",
"Soluction: (-1.0, -1.5)\n"
]
}
],
"source": [
"print('Input coefficients of the quadratic equation of the form ax^2+bx+c=0')\n",
"a = int(input('Enter a: '))\n",
"b = int(input('Enber b:'))\n",
"c = int(input('Enter c: '))\n",
"print('Soluction: ', solve_eqn(a, b, c))"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Input coefficients of the quadratic equation of the form ax^2+bx+c=0\n",
"Enter a: 5\n",
"Enber b:2\n",
"Enter c: 1\n",
"Error! Discriminant < 0. Could not solve. \n",
"Soluction: (None, None)\n"
]
}
],
"source": [
"print('Input coefficients of the quadratic equation of the form ax^2+bx+c=0')\n",
"a = int(input('Enter a: '))\n",
"b = int(input('Enber b:'))\n",
"c = int(input('Enter c: '))\n",
"print('Soluction: ', solve_eqn(a, b, c))"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[5, 3]\n"
]
}
],
"source": [
"lst = []\n",
"lst.append(5)\n",
"lst.append(3)\n",
"print(lst)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[]\n"
]
}
],
"source": [
"lst.clear()\n",
"print(lst)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[10, 20]\n",
"[]\n"
]
}
],
"source": [
"lst.append(10)\n",
"lst.append(20)\n",
"print(lst)\n",
"lst.clear()\n",
"print(lst)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 5]\n",
"[1, 2, 3, 5]\n"
]
}
],
"source": [
"lst = [1, 2, 3, 5]\n",
"print(lst)\n",
"new_lst = lst.copy()\n",
"print(new_lst)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 5]\n"
]
}
],
"source": [
"second_list = new_lst.copy()\n",
"print(second_list)"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Count of 1: 1\n",
"Count of 2: 1\n"
]
}
],
"source": [
"print('Count of 1:', second_list.count(3))\n",
"print('Count of 2:', second_list.count(2))"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 5]\n",
"[1, 2, 3, 5, 1, 2, 3, 5]\n"
]
}
],
"source": [
"print(lst)\n",
"lst.extend(second_list)\n",
"print(lst)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Alex', 'Bob', 'BS', 'MS']\n"
]
}
],
"source": [
"names = ['Alex', 'Bob']\n",
"courses = ['BS', 'MS']\n",
"names.extend(courses)\n",
"print(names)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Index of Bob: 1\n",
"Index of MS: 3\n"
]
}
],
"source": [
"print('Index of Bob: ', names.index('Bob'))\n",
"print('Index of MS: ', names.index(\"MS\"))"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Alex', 'Bob', 'Jack', 'BS', 'MS']\n",
"['Alex', 'Bob', 'Jack', 'Diploma', 'BS', 'MS']\n"
]
}
],
"source": [
"names.insert(2, 'Jack')\n",
"print(names)\n",
"names.insert(3, 'Diploma')\n",
"print(names)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Alex', 'Bob', 'Jack', 'Diploma', 'BS']\n",
"['Alex', 'Jack', 'Diploma', 'BS']\n"
]
}
],
"source": [
"names.pop()\n",
"print(names)\n",
"names.pop(1)\n",
"print(names)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Alex', 'Diploma', 'BS']\n",
"['Alex', 'BS']\n"
]
}
],
"source": [
"names.remove('Jack')\n",
"print(names)\n",
"names.remove('Diploma')\n",
"print(names)"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['BS', 'Alex']\n"
]
}
],
"source": [
"names.reverse()\n",
"print(names)"
...