diff --git a/#Count_Positives.py b/#Count_Positives.py new file mode 100644 index 0000000..9e377b4 --- /dev/null +++ b/#Count_Positives.py @@ -0,0 +1,13 @@ +#Count Positives - Given a list of numbers, create a function to replace last value with number of positive values. Example, count_positives([-1,1,1,1]) changes list #to [-1,1,1,3] and returns it. (Note that zero is not considered to be a positive number). +def count_positives(array): + count = 0 + for val in array: + if val > 0: + count += 1 + array[len(array)-1] = count + return array +print(count_positives([-1,1,1,1])) + + + + diff --git a/APIconnectionMySQL.ipynb b/APIconnectionMySQL.ipynb new file mode 100644 index 0000000..b4b270a --- /dev/null +++ b/APIconnectionMySQL.ipynb @@ -0,0 +1,108 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import mysql.connector as mariadb" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Open database connection using connect method" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "connecting to database...\n" + ] + } + ], + "source": [ + "con = mariadb.connect(\n", + " host = \"localhost\",\n", + " user = \"root\",\n", + " password = \"password\",\n", + " database = \"classicmodels\"\n", + ")\n", + "print(\"connecting to database...\")" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('Motorcycles', 13)]\n" + ] + } + ], + "source": [ + "cur = con.cursor()\n", + "st = \"Select productLine, count(*) from products where productLine = '{}'\"\n", + "cur.execute(st.format('Motorcycles'))\n", + "result = cur.fetchall()\n", + "print(result)" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [], + "source": [ + "con.close()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)]" + }, + "orig_nbformat": 4, + "vscode": { + "interpreter": { + "hash": "8b70a4ea831fd5b9b770fab33cac3f0a285469b7491720cba6f6d634f19e8405" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/APIfunctionsMySQL.ipynb b/APIfunctionsMySQL.ipynb new file mode 100644 index 0000000..c1c071a --- /dev/null +++ b/APIfunctionsMySQL.ipynb @@ -0,0 +1,244 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "import mysql.connector as mdb1" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "connected to database...\n", + "User_table created\n" + ] + } + ], + "source": [ + "def generate_user_table():\n", + " con = mdb1.connect(\n", + " host = \"localhost\",\n", + " user = \"root\",\n", + " password = \"password\",\n", + " database = \"test_db\"\n", + " \n", + " )\n", + " print(\"connected to database...\")\n", + " cur = con.cursor()\n", + " st1 = \"Create table user_table2(email varchar(100), name varchar(50), password varchar(30))\"\n", + " st2 = \"Insert into user_table2 \\\n", + " (email, name, password) VALUES \\\n", + " ('ywbaek@perscholas.org', 'young', 'letsgomets'), \\\n", + " ('mcordon@perscholas.org', 'marcial', 'perscholas'), \\\n", + " ('mhaseeb@perscholas.org', 'haseeb', 'platform') \"\n", + " \n", + " cur.execute(st1)\n", + " cur.execute(st2)\n", + " print('User_table created')\n", + " con.commit()\n", + " con.close()\n", + "\n", + " \n", + "\n", + "\n", + "generate_user_table()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "connected to database...\n", + "[('ywbaek@perscholas.org', 'young', 'letsgomets'), ('mcordon@perscholas.org', 'marcial', 'perscholas'), ('mhaseeb@perscholas.org', 'haseeb', 'platform')]\n" + ] + } + ], + "source": [ + "def get_all_users():\n", + " con = mdb1.connect(\n", + " host = \"localhost\",\n", + " user = \"root\",\n", + " password = \"password\",\n", + " database = \"test_db\"\n", + " \n", + " )\n", + " print(\"connected to database...\")\n", + " cur = con.cursor()\n", + " st = \"Select * from user_table2\"\n", + " cur.execute(st)\n", + " result = cur.fetchall()\n", + " print(result)\n", + " con.close()\n", + "\n", + "get_all_users()" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "connected to database...\n", + "[('ywbaek@perscholas.org', 'letsgomets')]\n" + ] + } + ], + "source": [ + "def get_user_by_name(name):\n", + " con = mdb1.connect(\n", + " host = \"localhost\",\n", + " user = \"root\",\n", + " password = \"password\",\n", + " database = \"test_db\"\n", + " \n", + " )\n", + " print(\"connected to database...\")\n", + " cur = con.cursor()\n", + " st = \"SELECT email, password FROM user_table2 WHERE name = '{}'\"\n", + " cur.execute(st.format(name))\n", + "\n", + " result = cur.fetchall()\n", + " print(result)\n", + " con.close()\n", + "\n", + "get_user_by_name('young')" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "connected to database...\n", + "True\n" + ] + } + ], + "source": [ + "def validate_user(email, password):\n", + " con = mdb1.connect(\n", + " host = \"localhost\",\n", + " user = \"root\",\n", + " password = \"password\",\n", + " database = \"test_db\"\n", + " \n", + " )\n", + " print(\"connected to database...\")\n", + " cur = con.cursor()\n", + " st = \"SELECT email, password FROM user_table2 WHERE email = '{}' and password = '{}'\"\n", + " cur.execute(st.format(email, password))\n", + " result = cur.fetchall()\n", + " if result != []:\n", + " return True\n", + " if result == []:\n", + " return False\n", + " \n", + " con.close()\n", + " #return result \n", + " \n", + " \n", + "\n", + "a = print(validate_user('ywbaek@perscholas.org', 'letsgomets'))" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "connected to database...\n" + ] + }, + { + "ename": "KeyError", + "evalue": "'name'", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mKeyError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[10], line 19\u001b[0m\n\u001b[0;32m 16\u001b[0m con\u001b[39m.\u001b[39mclose()\n\u001b[0;32m 17\u001b[0m \u001b[39mreturn\u001b[39;00m result\n\u001b[1;32m---> 19\u001b[0m \u001b[39mprint\u001b[39m(update_user(\u001b[39m'\u001b[39;49m\u001b[39myoung\u001b[39;49m\u001b[39m'\u001b[39;49m, \u001b[39m'\u001b[39;49m\u001b[39mletsgomets\u001b[39;49m\u001b[39m'\u001b[39;49m, \u001b[39m'\u001b[39;49m\u001b[39mywbaek@perscholas.org\u001b[39;49m\u001b[39m'\u001b[39;49m))\n", + "Cell \u001b[1;32mIn[10], line 12\u001b[0m, in \u001b[0;36mupdate_user\u001b[1;34m(name, password, email)\u001b[0m\n\u001b[0;32m 10\u001b[0m cur \u001b[39m=\u001b[39m con\u001b[39m.\u001b[39mcursor()\n\u001b[0;32m 11\u001b[0m st \u001b[39m=\u001b[39m \u001b[39m\"\u001b[39m\u001b[39mUpdate user_table2 SET name = \u001b[39m\u001b[39m'\u001b[39m\u001b[39m{name}\u001b[39;00m\u001b[39m'\u001b[39m\u001b[39m and password = \u001b[39m\u001b[39m'\u001b[39m\u001b[39m{password}\u001b[39;00m\u001b[39m'\u001b[39m\u001b[39m where email = \u001b[39m\u001b[39m'\u001b[39m\u001b[39m{email}\u001b[39;00m\u001b[39m'\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[1;32m---> 12\u001b[0m result \u001b[39m=\u001b[39m cur\u001b[39m.\u001b[39mexecute(st\u001b[39m.\u001b[39;49mformat(name, password, email))\n\u001b[0;32m 13\u001b[0m \u001b[39m#result = cur.fetchall()\u001b[39;00m\n\u001b[0;32m 14\u001b[0m \u001b[39mif\u001b[39;00m result:\n", + "\u001b[1;31mKeyError\u001b[0m: 'name'" + ] + } + ], + "source": [ + "def update_user(name, password, email):\n", + " con = mdb1.connect(\n", + " host = \"localhost\",\n", + " user = \"root\",\n", + " password = \"password\",\n", + " database = \"test_db\"\n", + " \n", + " )\n", + " print(\"connected to database...\")\n", + " cur = con.cursor()\n", + " st = \"Update user_table2 SET name = '{name}' and password = '{password}' where email = '{email}'\"\n", + " cur.execute(st.format(name, password, email))\n", + " result = cur.fetchall()\n", + " \n", + " if result != []:\n", + " return True\n", + " \n", + " else:\n", + " return False\n", + " \n", + "\n", + "print(update_user('young', 'letsgomets', 'ywbaek@perscholas.org'))\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)]" + }, + "orig_nbformat": 4, + "vscode": { + "interpreter": { + "hash": "8b70a4ea831fd5b9b770fab33cac3f0a285469b7491720cba6f6d634f19e8405" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/APIpractice.ipynb b/APIpractice.ipynb new file mode 100644 index 0000000..1b45731 --- /dev/null +++ b/APIpractice.ipynb @@ -0,0 +1,176 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import requests\n", + "import json\n", + "from pprint import pp" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3\n", + "4\n", + "5\n", + "6\n", + "7\n", + "8\n", + "9\n", + "10\n", + "11\n", + "12\n", + "13\n", + "14\n", + "15\n", + "16\n", + "17\n", + "18\n", + "19\n", + "20\n", + "21\n", + "22\n", + "23\n", + "24\n", + "25\n", + "26\n", + "27\n", + "28\n", + "29\n", + "30\n", + "31\n", + "32\n", + "33\n", + "34\n", + "35\n", + "36\n", + "37\n", + "38\n", + "39\n", + "40\n", + "41\n", + "42\n", + "840\n" + ] + } + ], + "source": [ + "baseurl = 'https://rickandmortyapi.com/api/'\n", + "endpoint = 'character'\n", + "\n", + "def main_request(base_url, endpoint, x):\n", + " r = requests.get(baseurl + endpoint + f'?page = {x}')\n", + " data = r.json()\n", + " return data\n", + "\n", + "def get_pages(response):\n", + " pages = response['info']['pages']\n", + " return pages\n", + "\n", + "def parse_json(response):\n", + " charlist = []\n", + " for item in response['results']:\n", + " char = {\n", + " 'name':item['name'],\n", + " 'no_ep': len(item['episode']),\n", + " }\n", + " charlist.append(char)\n", + " return charlist\n", + "\n", + "data = main_request(baseurl, endpoint, 1)\n", + "mainlist = []\n", + "for x in range(1, get_pages(data)+1):\n", + " print(x)\n", + " mainlist.extend(parse_json(main_request(baseurl, endpoint, x)))\n", + "print(len(mainlist))\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[{'name': 'Rick Sanchez', 'no_ep': 51}, {'name': 'Morty Smith', 'no_ep': 51}, {'name': 'Summer Smith', 'no_ep': 42}, {'name': 'Beth Smith', 'no_ep': 42}, {'name': 'Jerry Smith', 'no_ep': 39}, {'name': 'Abadango Cluster Princess', 'no_ep': 1}, {'name': 'Abradolf Lincler', 'no_ep': 2}, {'name': 'Adjudicator Rick', 'no_ep': 1}, {'name': 'Agency Director', 'no_ep': 1}, {'name': 'Alan Rails', 'no_ep': 1}, {'name': 'Albert Einstein', 'no_ep': 1}, {'name': 'Alexander', 'no_ep': 1}, {'name': 'Alien Googah', 'no_ep': 1}, {'name': 'Alien Morty', 'no_ep': 1}, {'name': 'Alien Rick', 'no_ep': 1}, {'name': 'Amish Cyborg', 'no_ep': 1}, {'name': 'Annie', 'no_ep': 1}, {'name': 'Antenna Morty', 'no_ep': 2}, {'name': 'Antenna Rick', 'no_ep': 1}, {'name': 'Ants in my Eyes Johnson', 'no_ep': 1}]\n" + ] + } + ], + "source": [ + "baseurl = 'https://rickandmortyapi.com/api/'\n", + "endpoint = 'character'\n", + "\n", + "def main_request(base_url, endpoint, x):\n", + " r = requests.get(baseurl + endpoint + f'?page = {x}')\n", + " data = r.json()\n", + " return data\n", + "\n", + "def get_pages(data):\n", + " pages = data['info']['pages']\n", + " return pages\n", + "\n", + "def parse_json(data):\n", + " charlist = []\n", + " for item in data['results']:\n", + " char = {\n", + " 'name':item['name'],\n", + " 'no_ep': len(item['episode']),\n", + " }\n", + " charlist.append(char)\n", + " return charlist\n", + "\n", + "data1 = main_request(baseurl, endpoint, 1)\n", + "a = parse_json(data1)\n", + "print(a)\n", + "#mainlist = []\n", + "#for x in range(1, get_pages(data)+1):\n", + " #print(x)\n", + " #mainlist.extend(parse_json(main_request(baseurl, endpoint, x)))\n", + "#print(len(mainlist))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)]" + }, + "orig_nbformat": 4, + "vscode": { + "interpreter": { + "hash": "8b70a4ea831fd5b9b770fab33cac3f0a285469b7491720cba6f6d634f19e8405" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/PythonRestAPI.ipynb b/PythonRestAPI.ipynb new file mode 100644 index 0000000..abfcbdc --- /dev/null +++ b/PythonRestAPI.ipynb @@ -0,0 +1,237 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "import requests\n", + "import json\n", + "from pprint import pp" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'userId': 1, 'id': 1, 'title': 'delectus aut autem', 'completed': False}\n", + "200\n", + "application/json; charset=utf-8\n" + ] + } + ], + "source": [ + "api_url = \"https://jsonplaceholder.typicode.com/todos/1\"\n", + "response = requests.get(api_url)\n", + "r = response.json()\n", + "print(response.json())\n", + "print(response.status_code)\n", + "print(response.headers[\"Content-Type\"])" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "201\n", + "{'userId': 1, 'title': 'Buy milk', 'completed': False, 'id': 201}\n", + ">\n" + ] + } + ], + "source": [ + "api_url = \"https://jsonplaceholder.typicode.com/todos\"\n", + "todo = {\"userId\": 1, \"title\": \"Buy milk\", \"completed\": False}\n", + "response = requests.post(api_url, json=todo)\n", + "print(response.status_code)\n", + "print(response.json())\n", + "#OR\n", + "headers = {\"Content-Type\": \"application-json\"}\n", + "response = requests.post(api_url, data = json.dumps(todo), headers = headers)\n", + "print(response.json)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'userId': 1, 'id': 10, 'title': 'illo est ratione doloremque quia maiores aut', 'completed': True}\n", + "200\n", + "{'userId': 1, 'title': 'Buy milk', 'completed': False, 'id': 10}\n" + ] + } + ], + "source": [ + "#Put\n", + "api_url = \"https://jsonplaceholder.typicode.com/todos/10\"\n", + "response = requests.get(api_url)\n", + "print(response.json())\n", + "todo = {\"userId\": 1, \"title\": \"Buy milk\", \"completed\": False}\n", + "response = requests.put(api_url, json=todo)\n", + "print(response.status_code)\n", + "print(response.json())\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "200\n", + "{'userId': 1, 'id': 10, 'title': 'Mow lawn', 'completed': True}\n" + ] + } + ], + "source": [ + "#patch request\n", + "api_url = \"https://jsonplaceholder.typicode.com/todos/10\"\n", + "todo = {\"title\": \"Mow lawn\"}\n", + "response = requests.patch(api_url, json=todo)\n", + "print(response.status_code)\n", + "print(response.json())" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{}\n", + "200\n" + ] + } + ], + "source": [ + "api_url = \"https://jsonplaceholder.typicode.com/todos/10\"\n", + "response = requests.delete(api_url)\n", + "print(response.json())\n", + "print(response.status_code)" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "200\n", + "{'message': 'success',\n", + " 'people': [{'name': 'Sergey Prokopyev', 'craft': 'ISS'},\n", + " {'name': 'Dmitry Petelin', 'craft': 'ISS'},\n", + " {'name': 'Frank Rubio', 'craft': 'ISS'},\n", + " {'name': 'Nicole Mann', 'craft': 'ISS'},\n", + " {'name': 'Josh Cassada', 'craft': 'ISS'},\n", + " {'name': 'Koichi Wakata', 'craft': 'ISS'},\n", + " {'name': 'Anna Kikina', 'craft': 'ISS'},\n", + " {'name': 'Fei Junlong', 'craft': 'Shenzhou 15'},\n", + " {'name': 'Deng Qingming', 'craft': 'Shenzhou 15'},\n", + " {'name': 'Zhang Lu', 'craft': 'Shenzhou 15'}],\n", + " 'number': 10}\n", + "[{'name': 'Sergey Prokopyev', 'craft': 'ISS'}, {'name': 'Dmitry Petelin', 'craft': 'ISS'}, {'name': 'Frank Rubio', 'craft': 'ISS'}, {'name': 'Nicole Mann', 'craft': 'ISS'}, {'name': 'Josh Cassada', 'craft': 'ISS'}, {'name': 'Koichi Wakata', 'craft': 'ISS'}, {'name': 'Anna Kikina', 'craft': 'ISS'}, {'name': 'Fei Junlong', 'craft': 'Shenzhou 15'}, {'name': 'Deng Qingming', 'craft': 'Shenzhou 15'}, {'name': 'Zhang Lu', 'craft': 'Shenzhou 15'}]\n", + "['Sergey Prokopyev', 'Dmitry Petelin', 'Frank Rubio', 'Nicole Mann', 'Josh Cassada', 'Koichi Wakata', 'Anna Kikina', 'Fei Junlong', 'Deng Qingming', 'Zhang Lu']\n" + ] + } + ], + "source": [ + "api_url = \"http://api.open-notify.org/astros.json\"\n", + "response = requests.get(api_url)\n", + "print(response.status_code)\n", + "pp(response.json())\n", + "r = response.json()\n", + "\n", + "people_subset_dict = r[\"people\"]#saving \"people\" key in \"people_subset_dict\" variable \n", + "print(people_subset_dict)\n", + "\n", + "plist = []\n", + "\n", + "def get_names(): \n", + " for i in people_subset_dict:\n", + " plist.append(i[\"name\"])\n", + " #print(i[\"name\"])\n", + " return plist\n", + "\n", + "new_list = get_names()\n", + "print(new_list)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "200\n", + "{'view': {'id': 'f6w7-q2d2', 'name': 'Electric Vehicle Population Data', 'assetType': 'dataset', 'attribution': 'Washington State Department of Licensing', 'averageRating': 0, 'category': 'Transportation', 'createdAt': 1555435581, 'description': 'This dataset shows the Battery Electric Vehicles (BEVs) and Plug-in Hybrid Electric Vehicles (PHEVs) that are currently registered through Washington State Department of Licensing (DOL).', 'displayType': 'table', 'downloadCount': 39142, 'hideFromCatalog': False, 'hideFromDataJson': False, 'newBackend': True, 'numberOfComments': 0, 'oid': 38591322, 'provenance': 'official', 'publicationAppendEnabled': False, 'publicationDate': 1655411270, 'publicationGroup': 16109923, 'publicationStage': 'published', 'rowsUpdatedAt': 1670541235, 'rowsUpdatedBy': 'dphz-szpx', 'tableId': 18748445, 'totalTimesRated': 0, 'viewCount': 20234, 'viewLastModified': 1670541193, 'viewType': 'tabular', 'approvals': [{'reviewedAt': 1559931329, 'reviewedAutomatically': True, 'state': 'approved', 'submissionId': 4774840, 'submissionObject': 'public_audience_request', 'submissionOutcome': 'change_audience', 'submittedAt': 1559931329, 'workflowId': 2106, 'submissionDetails': {'permissionType': 'READ'}, 'submissionOutcomeApplication': {'failureCount': 0, 'status': 'success'}, 'submitter': {'id': 'eagg-6py7', 'displayName': 'Department of Licensing'}}], 'clientContext': {'clientContextVariables': [], 'inheritedVariables': {}}, 'columns': [{'id': -1, 'name': 'sid', 'dataTypeName': 'meta_data', 'fieldName': ':sid', 'position': 0, 'renderTypeName': 'meta_data', 'format': {}, 'flags': ['hidden']}, {'id': -1, 'name': 'id', 'dataTypeName': 'meta_data', 'fieldName': ':id', 'position': 0, 'renderTypeName': 'meta_data', 'format': {}, 'flags': ['hidden']}, {'id': -1, 'name': 'position', 'dataTypeName': 'meta_data', 'fieldName': ':position', 'position': 0, 'renderTypeName': 'meta_data', 'format': {}, 'flags': ['hidden']}, {'id': -1, 'name': 'created_at', 'dataTypeName': 'meta_data', 'fieldName': ':created_at', 'position': 0, 'renderTypeName': 'meta_data', 'format': {}, 'flags': ['hidden']}, {'id': -1, 'name': 'created_meta', 'dataTypeName': 'meta_data', 'fieldName': ':created_meta', 'position': 0, 'renderTypeName': 'meta_data', 'format': {}, 'flags': ['hidden']}, {'id': -1, 'name': 'updated_at', 'dataTypeName': 'meta_data', 'fieldName': ':updated_at', 'position': 0, 'renderTypeName': 'meta_data', 'format': {}, 'flags': ['hidden']}, {'id': -1, 'name': 'updated_meta', 'dataTypeName': 'meta_data', 'fieldName': ':updated_meta', 'position': 0, 'renderTypeName': 'meta_data', 'format': {}, 'flags': ['hidden']}, {'id': -1, 'name': 'meta', 'dataTypeName': 'meta_data', 'fieldName': ':meta', 'position': 0, 'renderTypeName': 'meta_data', 'format': {}, 'flags': ['hidden']}, {'id': 561974342, 'name': 'VIN (1-10)', 'dataTypeName': 'text', 'description': \"The 1st 10 characters of each vehicle's Vehicle Identification Number (VIN).\", 'fieldName': 'vin_1_10', 'position': 1, 'renderTypeName': 'text', 'tableColumnId': 79309982, 'format': {}}, {'id': 561974343, 'name': 'County', 'dataTypeName': 'text', 'description': 'The county in which the registered owner resides.', 'fieldName': 'county', 'position': 2, 'renderTypeName': 'text', 'tableColumnId': 79309981, 'format': {}}, {'id': 561974344, 'name': 'City', 'dataTypeName': 'text', 'description': 'The city in which the registered owner resides.', 'fieldName': 'city', 'position': 3, 'renderTypeName': 'text', 'tableColumnId': 79309980, 'format': {}}, {'id': 561974345, 'name': 'State', 'dataTypeName': 'text', 'description': 'The state in which the registered owner resides.', 'fieldName': 'state', 'position': 4, 'renderTypeName': 'text', 'tableColumnId': 80218820, 'format': {}}, {'id': 561974346, 'name': 'Postal Code', 'dataTypeName': 'text', 'description': 'The 5 digit zip code in which the registered owner resides.', 'fieldName': 'zip_code', 'position': 5, 'renderTypeName': 'text', 'tableColumnId': 79309971, 'format': {}}, {'id': 561974347, 'name': 'Model Year', 'dataTypeName': 'text', 'description': 'The model year of the vehicle, determined by decoding the Vehicle Identification Number (VIN).', 'fieldName': 'model_year', 'position': 6, 'renderTypeName': 'text', 'tableColumnId': 79309970, 'format': {}}, {'id': 561974348, 'name': 'Make', 'dataTypeName': 'text', 'description': 'The manufacturer of the vehicle, determined by decoding the Vehicle Identification Number (VIN).', 'fieldName': 'make', 'position': 7, 'renderTypeName': 'text', 'tableColumnId': 79309979, 'format': {}}, {'id': 561974349, 'name': 'Model', 'dataTypeName': 'text', 'description': 'The model of the vehicle, determined by decoding the Vehicle Identification Number (VIN).', 'fieldName': 'model', 'position': 8, 'renderTypeName': 'text', 'tableColumnId': 79309978, 'format': {}}, {'id': 561974350, 'name': 'Electric Vehicle Type', 'dataTypeName': 'text', 'description': 'This distinguishes the vehicle as all electric or a plug-in hybrid.', 'fieldName': 'ev_type', 'position': 9, 'renderTypeName': 'text', 'tableColumnId': 79309976, 'format': {'align': 'right'}}, {'id': 561974351, 'name': 'Clean Alternative Fuel Vehicle (CAFV) Eligibility', 'dataTypeName': 'text', 'description': 'This categorizes vehicle as Clean Alternative Fuel Vehicles (CAFVs) based on the fuel requirement and electric-only range requirement in House Bill 2042 as passed in the 2019 legislative session.', 'fieldName': 'cafv_type', 'position': 10, 'renderTypeName': 'text', 'tableColumnId': 79309975, 'format': {'align': 'right'}}, {'id': 561974352, 'name': 'Electric Range', 'dataTypeName': 'number', 'description': 'Describes how far a vehicle can travel purely on its electric charge.', 'fieldName': 'electric_range', 'position': 11, 'renderTypeName': 'number', 'tableColumnId': 79309974, 'format': {'align': 'right'}}, {'id': 561974353, 'name': 'Base MSRP', 'dataTypeName': 'number', 'description': \"This is the lowest Manufacturer's Suggested Retail Price (MSRP) for any trim level of the model in question.\", 'fieldName': 'base_msrp', 'position': 12, 'renderTypeName': 'number', 'tableColumnId': 79310342, 'format': {'align': 'right'}}, {'id': 561974354, 'name': 'Legislative District', 'dataTypeName': 'number', 'description': \"The specific section of Washington State that the vehicle's owner resides in, as represented in the state legislature.\", 'fieldName': 'legislative_district', 'position': 13, 'renderTypeName': 'number', 'tableColumnId': 140620854, 'format': {'align': 'center'}}, {'id': 561974355, 'name': 'DOL Vehicle ID', 'dataTypeName': 'text', 'description': 'Unique number assigned to each vehicle by Department of Licensing for identification purposes.', 'fieldName': 'dol_vehicle_id', 'position': 14, 'renderTypeName': 'text', 'tableColumnId': 82051518, 'format': {}}, {'id': 561974356, 'name': 'Vehicle Location', 'dataTypeName': 'point', 'description': 'The center of the ZIP Code for the registered vehicle.', 'fieldName': 'geocoded_column', 'position': 15, 'renderTypeName': 'point', 'tableColumnId': 87270515, 'format': {}}, {'id': 561974358, 'name': 'Electric Utility', 'dataTypeName': 'text', 'description': 'This is the electric power retail service territories serving the address of the registered vehicle. All ownership types for areas in Washington are included: federal, investor owned, municipal, political subdivision, and cooperative. If the address for the registered vehicle falls into an area with overlapping electric power retail service territories then a single pipe | delimits utilities of same TYPE and a double pipe || delimits utilities of different types. We combined vehicle address and Homeland Infrastructure Foundation Level Database (HIFLD) (https://gii.dhs.gov/HIFLD) Retail_Service_Territories feature layer using a geographic information system to assign values for this field. Blanks occur for vehicles with addresses outside of Washington or for addresses falling into areas in Washington not containing a mapped electric power retail service territory in the source data.', 'fieldName': 'electric_utility', 'position': 17, 'renderTypeName': 'text', 'tableColumnId': 141400529, 'format': {}}, {'id': 561974362, 'name': '2020 Census Tract', 'dataTypeName': 'text', 'description': 'The census tract identifier is a combination of the state, county, and census tract codes as assigned by the United States Census Bureau in the 2020 census, also known as Geographic Identifier (GEOID). More information can be found here: https://www.census.gov/programs-surveys/geography/about/glossary.html#par_textimage_13 https://www.census.gov/programs-surveys/geography/guidance/geo-identifiers.html ', 'fieldName': '_2020_census_tract', 'position': 18, 'renderTypeName': 'text', 'tableColumnId': 142655072, 'format': {}}, {'id': 561974360, 'name': 'Counties', 'dataTypeName': 'number', 'description': '', 'fieldName': ':@computed_region_x4ys_rtnd', 'position': 19, 'renderTypeName': 'number', 'tableColumnId': 87270516, 'computationStrategy': {'source_columns': ['geocoded_column'], 'type': 'georegion_match_on_point', 'parameters': {'region': '_x4ys-rtnd', 'primary_key': '_feature_id'}}, 'format': {}}, {'id': 561974361, 'name': 'Congressional Districts', 'dataTypeName': 'number', 'description': '', 'fieldName': ':@computed_region_fny7_vc3j', 'position': 20, 'renderTypeName': 'number', 'tableColumnId': 87270517, 'computationStrategy': {'source_columns': ['geocoded_column'], 'type': 'georegion_match_on_point', 'parameters': {'region': '_fny7-vc3j', 'primary_key': '_feature_id'}}, 'format': {}}, {'id': 561974359, 'name': 'WAOFM - GIS - Legislative District Boundary', 'dataTypeName': 'number', 'description': '', 'fieldName': ':@computed_region_8ddd_yn5v', 'position': 21, 'renderTypeName': 'number', 'tableColumnId': 87270518, 'computationStrategy': {'source_columns': ['geocoded_column'], 'type': 'georegion_match_on_point', 'parameters': {'region': '_8ddd-yn5v', 'primary_key': '_feature_id'}}, 'format': {}}], 'grants': [{'inherited': False, 'type': 'viewer', 'flags': ['public']}], 'metadata': {'custom_fields': {'Temporal': {'Posting Frequency': 'Monthly', 'Period of Time': 'Data includes Battery Electric Vehicles and Plug-in Hybrid Electric Vehicles registered as of November 30, 2022.'}, 'Identification': {'Originator': 'Research and Analysis Office, Department of Licensing', 'Metadata Language': 'English'}, 'Notes': {'1. ': 'A Battery Electric Vehicle (BEV) is an all-electric vehicle using one or more batteries to store the electrical energy that powers the motor and is charged by plugging the vehicle in to an electric power source. A Plug-in Hybrid Electric Vehicle (PHEV) is a vehicle that uses one or more batteries to power an electric motor; uses another fuel, such as gasoline or diesel, to power an internal combustion engine or other propulsion source; and is charged by plugging the vehicle in to an electric power source.', '2. ': 'Clean Alternative Fuel Vehicle (CAFV) Eligibility is based on the fuel requirement and electric-only range requirement as outlined in RCW 82.08.809 and RCW 82.12.809 to be eligible for Alternative Fuel Vehicles retail sales and Washington State use tax exemptions. Sales or leases of these vehicles must occur on or after 8/1/2019 and meet the purchase price requirements to be eligible for Alternative Fuel Vehicles retail sales and Washington State use tax exemptions.', '3. ': 'Monthly count of vehicles for a county may change from this report and prior reports. Processes were implemented to more accurately assign county at the time of registration.', '4. ': 'Electric Range is no longer maintained for Battery Electric Vehicles (BEV) because new BEVs have an electric range of 30 miles or more. Zero (0) will be entered where the electric range has not been researched.', '5. ': 'Field Electric Utility was added starting with the publication in March 2022.', '6. ': \"Field '2020 Census Tract' was added starting with the publication in June 2022.\"}}, 'rowLabel': 'Vehicle', 'availableDisplayTypes': ['table', 'fatrow', 'page']}, 'owner': {'id': 'eagg-6py7', 'displayName': 'Department of Licensing', 'profileImageUrlLarge': '/api/users/eagg-6py7/profile_images/LARGE', 'profileImageUrlMedium': '/api/users/eagg-6py7/profile_images/THUMB', 'profileImageUrlSmall': '/api/users/eagg-6py7/profile_images/TINY', 'screenName': 'Department of Licensing', 'type': 'interactive', 'flags': ['acceptedEula', 'mayBeStoriesCoOwner']}, 'query': {}, 'rights': ['read'], 'tableAuthor': {'id': 'eagg-6py7', 'displayName': 'Department of Licensing', 'profileImageUrlLarge': '/api/users/eagg-6py7/profile_images/LARGE', 'profileImageUrlMedium': '/api/users/eagg-6py7/profile_images/THUMB', 'profileImageUrlSmall': '/api/users/eagg-6py7/profile_images/TINY', 'screenName': 'Department of Licensing', 'type': 'interactive', 'flags': ['acceptedEula', 'mayBeStoriesCoOwner']}, 'tags': ['tesla', 'leaf', 'nissan', 'model 3', 'dol', 'department of licensing', 'green report', 'ev', 'evs', 'phev', 'phevs', 'bev', 'bevs', 'electric', 'hybrid', 'vehicle', 'plug-in', 'volt', 'bolt', 'chevy', 'chevrolet', 'car', 'environment', 'clean energy', 'population', 'hybrids', 'plug-ins', 'vehicles', 'cars', 'energy', 'nhtsa', 'rao_open_data', 'dol_open_data', 'rao_ev', 'rao_veh'], 'flags': ['default', 'ownerMayBeContacted', 'restorable', 'restorePossibleForType']}}\n" + ] + } + ], + "source": [ + "api_url1 = \"https://data.wa.gov/api/views/f6w7-q2d2/rows.json?accessType=DOWNLOAD\"\n", + "r1 = requests.get(api_url1)\n", + "data = r1.json()\n", + "#print(response.json())\n", + "print(response.status_code)\n", + "#print(response.headers[\"Content-Type\"])\n", + "#pp(response.json())\n", + "print(data['meta'])" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)]" + }, + "orig_nbformat": 4, + "vscode": { + "interpreter": { + "hash": "8b70a4ea831fd5b9b770fab33cac3f0a285469b7491720cba6f6d634f19e8405" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Python_Practice2.py b/Python_Practice2.py new file mode 100644 index 0000000..de1116d --- /dev/null +++ b/Python_Practice2.py @@ -0,0 +1,139 @@ +import re +#Without List Comprehension +my_list = [] +for e in range(1, 10, 2): + my_list.append(e**2) +print(my_list) +#With List Comprehension +my_list = [e**2 for e in range(1, 10, 2)] +print(my_list) + +#Without List Comprehension +my_list = [] +for e in range(10): + if e%2 == 1: + my_list.append(e**2) +print(my_list) +#With comprehension +my_list = [e**2 for e in range(10) if e%2 == 1] +print(my_list) + +#multiple conditional statements using ternary operators: +grades = [95, 55, 83, 75, 91] +grades_l = [('A' if g>=90 else ('B' if g>=75 else 'C')) for g in grades] +print(grades_l) +# OR + +grades_l = [] +for g in grades: + if g >= 90: + grades_l.append('A') + elif g >= 75: + grades_l.append('B') + else: + grades_l.append('C') +print(grades_l) + +#nested for loops +pairs = [] +for i in range(3): + for j in range(2): + pairs.append((i, j)) +print(pairs) + +#Generator Example1: +def powers_of_two(): + n = 1 + while True: + n *= 2 + yield n + +powers = powers_of_two() +for _ in range(20): + print(next(powers)) + squares = (i**2 for i in range(20)) +for s in squares: + print(s) + +#Generator Example2 +def even_n(max_n = 1): + n =1 + while n <= max_n: + yield n*2 + n +=1 + +i = even_n(3) +print(next(i)) +print(next(i)) +print(next(i)) + +#Prime_number Generator +def primes_gen(): + for n in range(2, 100): # n starts from 2 to end + for x in range(2, n): # check if x can be divided by n + if n % x == 0: # if true then n is not prime + break + else: # if x is found after exhausting all values of x + yield n # generate the prime + +gen = primes_gen() +for _ in range(10): + print(next(gen), end=' ') + +#Unique Letters generator +def unique_str(my_str): + char_seen = [] + for char in my_str: + if char not in char_seen: + char_seen.append(char) + yield (''.join(char_seen)) + + +# For loop to reverse the string +for char in unique_str("Hello"): + print(char, end = ' ') + +#Sort the list by each language's version in ascending order. +prog_lang = [('Python', 3.8), ('Java', 13), ('JavaScript', 2019), ('Scala', 2.13)] +new_list = sorted(prog_lang, key = lambda x : x[0], reverse = True) +print(new_list) + +#Sort the list by the length of the name of each language in descending order. +prog_lang = [('Python', 3.8), ('Java', 13), ('JavaScript', 2019), ('Scala', 2.13)] +new_list2 = sorted(prog_lang, key = lambda x : x[0], reverse = False) +print(new_list2) + +#Filter the list so that it only contains languages with 'a' in it. +search_key = 'a' +new_list3 = list(filter(lambda x: search_key in x[0], prog_lang)) +print(new_list3) + +#Filter the list so that it only contains languages whose version is in integer +#form. +new_list4 = list(filter(lambda x: isinstance(x[1], int), prog_lang)) +print(new_list4) + +#Transform the list so that it contains the tuples in the form, +#("language in all lower case", length of the language string) +lowername = list(map(lambda x: x[0].lower(), prog_lang)) +lengthname = list(map(lambda x: len(x[0]), prog_lang)) +i = iter(lowername) +j = iter(lengthname) +final_list = tuple(zip(i,j)) +print(final_list) + +#Generate a tuple in the form, +#("All languages separated by commas", +#"All versions separated by commas") +lang_name = [] +lang_name = ' '.join(map(lambda x: x[0], prog_lang)) +version_no = [] +version_no = ' '.join(map(lambda x: str(x[1]), prog_lang)) +lang_name1 = tuple(map(str, lang_name.split(', '))) +version_no2 = tuple(map(str, version_no.split(', '))) +result = lang_name1 + version_no2 +print(lang_name1) +print(version_no2) +print(result) + + diff --git a/12_09_practice.py b/Python_Practice_Assignment.py similarity index 50% rename from 12_09_practice.py rename to Python_Practice_Assignment.py index 7914563..00b24d0 100644 --- a/12_09_practice.py +++ b/Python_Practice_Assignment.py @@ -1,29 +1,149 @@ -#Biggie Size - Given a list, write a function that changes all positive numbers in the list to "big". Example: make_it_big([-1, 3, 5, -5]) returns that same list, #changed to [-1, "big", "big", -5]. - -#Count Positives - Given a list of numbers, create a function to replace last value with number of positive values. Example, count_positives([-1,1,1,1]) changes list #to [-1,1,1,3] and returns it. (Note that zero is not considered to be a positive number). - -#SumTotal - Create a function that takes a list as an argument and returns the sum of all the values in the list. For example sum_total([1,2,3,4]) should return 10 - -#Average - Create a function that takes a list as an argument and returns the average of all the values in the list. For example multiples([1,2,3,4]) should return #2.5 - -#Length - Create a function that takes a list as an argument and returns the length of the list. For example length([1,2,3,4]) should return 4 - -#Minimum - Create a function that takes a list as an argument and returns the minimum value in the list. If the passed list is empty, have the function return false. #For example minimum([1,2,3,4]) should return 1; minimum([-1,-2,-3]) should return -3. -# -#Maximum - Create a function that takes a list as an argument and returns the maximum value in the list. If the passed list is empty, have the function return false. #For example maximum([1,2,3,4]) should return 4; maximum([-1,-2,-3]) should return -1. - -#Ultimateaalyze - Create a function that takes a list as an argument and returns a dictionary that has the sumTotal, average, minimum, maximum ad length of the list. - -#ReverseList - Create a function that takes a list as a argument and return a list in a reversed order. Do this without creating a empty temporary list. For example #reverse([1,2,3,4]) should return [4,3,2,1]. This challenge is known to appear during basic technical interviews. - -#Ispalindrome- Given a string, write a python function to check if it is palindrome or not. A string is said to be palindrome if the reverse of the string is the same as string. For example, “radar” is a palindrome, but “radix” is not a palindrome. - -#Fizzbuzz- Create a function that will print numbers from 1 to 100, with certain exceptions: - #If the number is a multiple of 3, print “Fizz” instead of the number. - #If the number is a multiple of 5, print “Buzz” instead of the number. - #If the number is a multiple of 3 and 5, print “FizzBuzz” instead of the number. - -#Fibonacci- The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, #starting from 0 and 1. That is, - #F(0) = 0, F(1) = 1 - #F(n) = F(n - 1) + F(n - 2), for n > 1. - #Create a function that accepts any number and will create a sequence based on the fibonacci sequence. \ No newline at end of file + +#Python Practice Problems Assignment +#Biggie Size - Given a list, write a function that changes all positive numbers in the list to "big". Example: make_it_big([-1, 3, 5, -5]) returns that same list, #changed to [-1, "big", "big", -5]. +def make_it_big(array): + for i in range(len(array)): + if array[i]>0: + array[i]= "big" + return array +print(make_it_big([-1, 3, 5, -5])) + +#Count Positives - Given a list of numbers, create a function to replace last value with number of positive values. Example, count_positives([-1,1,1,1]) changes list #to [-1,1,1,3] and returns it. (Note that zero is not considered to be a positive number). +def count_positives(array): + count = 0 + for val in array: + if val > 0: + count += 1 + array[len(array)-1] = count + return array +print(count_positives([-1,1,1,1])) + +#SumTotal - Create a function that takes a list as an argument and returns the sum of all the values in the list. For example sum_total([1,2,3,4]) should return 10 +def sumlist(lista): + sumtotal = 0 + for i in lista: + sumtotal = sumtotal + i + return sumtotal +a = print(sumlist([1, 2, 3,4])) + +#Average - Create a function that takes a list as an argument and returns the average of all the values in the list. For example multiples([1,2,3,4]) should return #2.5 +def avglist(lista): + sumvalue = 0 + avgvalue = 0 + for i in lista: + sumvalue = sumvalue + i + avgvalue = sumvalue/len(lista) + return avgvalue + +avg1 = print(avglist([1,2,3,4])) + +#Length - Create a function that takes a list as an argument and returns the length of the list. For example length([1,2,3,4]) should return 4 +def lenlist(list1): + return len(list1) +len1 = print(lenlist([1,2,3,4])) + +#Minimum - Create a function that takes a list as an argument and returns the minimum value in the list. If the passed list is empty, have the function return false. #For example minimum([1,2,3,4]) should return 1; minimum([-1,-2,-3]) should return -3. +def min_list(list1): + if list1 == []: + return False + else: + return min(list1) + +min1 = print(min_list([1,2,3,4])) +min2 = print(min_list([-1,-2,-3])) +min3 = print(min_list([])) + +#Maximum - Create a function that takes a list as an argument and returns the maximum value in the list. If the passed list is empty, have the function return false. #For example maximum([1,2,3,4]) should return 4; maximum([-1,-2,-3]) should return -1. +def max_list(list1): + if list1 == []: + return False + else: + return max(list1) + +max1 = print(max_list([1,2,3,4])) +max2 = print(max_list([-1,-2,-3])) +max3 = print(max_list([])) + +#Ultimateaalyze - Create a function that takes a list as an argument and returns a dictionary that has the sumTotal, average, minimum, maximum ad length of the list. +def d_suavmima(list1): + sumlist = sum(list1) + avglist = sum(list1)/len(list1) + minlist = min(list1) + maxlist = max(list1) + lenlist = len(list1) + list2 = ['sumlist', 'avglist', 'minlist', 'maxlist', 'lenlist'] + list3 = [sum(list1), sum(list1)/len(list1), min(list1), max(list1), len(list1)] + i = iter(list2) + j = iter(list3) + dic1 = dict(zip(i, j)) + return dic1 + +dict1 = print(d_suavmima([1,2,3,4])) + +#ReverseList - Create a function that takes a list as a argument and return a list in a reversed order. Do this without creating a empty temporary list. For example #reverse([1,2,3,4]) should return [4,3,2,1]. This challenge is known to appear during basic technical interviews. +def reverse_list(list1): + left = 0 + right = len(list1) - 1 + while (left < right): + temp = list1[left] + list1[left] = list1[right] + list1[right] = temp + left+=1 + right-=1 + return list1 + +revlist1 = print(reverse_list([1,2,3,4,5])) + +#Ispalindrome- Given a string, write a python function to check if it is palindrome or not. A string is said to be palindrome if the reverse of the string is the same as string. For example, “radar” is a palindrome, but “radix” is not a palindrome. +def palindromchk(str1): + revstr1 = "" + for i in str1: + revstr1 = i + revstr1 + if (str1 == revstr1): + return 'Yes' + else: + return 'No' + +chk1 = print(palindromchk('12321')) + +#Fizzbuzz- Create a function that will print numbers from 1 to 100, with certain exceptions: + #If the number is a multiple of 3, print “Fizz” instead of the number. + #If the number is a multiple of 5, print “Buzz” instead of the number. + #If the number is a multiple of 3 and 5, print “FizzBuzz” instead of the number. +def fizzbuzz(x): + i =1 + for i in range(x+1): + if i%3 == 0 and i%5 != 0: + i = 'Fizz' + print(i) + elif (i%5 == 0 and i%3 != 0): + i = 'Buzz' + print(i) + elif i%3 == 0 and i%5 == 0: + i = 'FizzBuzz' + print(i) + else: + i = i + print(i) + +fizzbuzz(100) + +#Fibonacci- The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, #starting from 0 and 1. That is, + #F(0) = 0, F(1) = 1 + #F(n) = F(n - 1) + F(n - 2), for n > 1. + #Create a function that accepts any number and will create a sequence based on the fibonacci sequence. +def fibchk(n): + n1 = 0 + n2 = 1 + sum = 0 + for i in range(n): + sum = n1 + n2 + n1 = n2 + n2 = sum + i+=1 + print(sum) + + +fibchk(50) + + diff --git a/Python_xml.py b/Python_xml.py new file mode 100644 index 0000000..e32b227 --- /dev/null +++ b/Python_xml.py @@ -0,0 +1,104 @@ + +import xml.etree.ElementTree as ET +import re + +tree = ET.parse('movies.xml') +root = tree.getroot() + +print(tree) +print(root) +print(root.tag) +print(len(root)) + +#for child in root: +# print(child) + +print(len(root)) + +for child in root: + print(child.tag, child.attrib) + +print([i.tag for i in root.iter()]) + +print(ET.tostring(root, encoding = 'utf8').decode('utf8')) + +for movie in root.iter('movie'): + print(movie.attrib) + +for description in root.iter('description'): + print(description.text) + +for movie in root.findall("./genre/decade/movie/[year='1992']"): + print(movie.attrib) + +for movie in root.findall("./genre/decade/movie/format/[@multiple = 'Yes']"): + print(movie.attrib) + +#back = root.find("./genre/decade/movie[@title = 'Back to the Future']") +#print(back) + +#back.attrib['title'] = "Back 2 the Future" +#print(back.attrib) + +tree.write('movies.xml') +tree = ET.parse('movies.xml') +root = tree.getroot() + +for movie in root.iter('movie'): + print(movie.attrib) + +for form in root.findall("./genre/decade/movie/format"): + print(form.attrib, form.text) + +for form in root.findall("./genre/decade/movie/format"): + match = re.search(',', form.text) + if match: + form.set('multiple', 'Yes') + else: + form.set('multiple', 'No') + + print(form.attrib, form.text) + +for decade in root.findall("./genre/decade"): + print(decade.attrib) + for year in decade.findall("./movie/year"): + print(year.text, '\n') + +for movie in root.findall("./genre/decade/movie/[year = '2000']"): + print(movie.attrib) + +add = root.find("./genre[@category = 'Action']") +new_dec = ET.SubElement(add, 'decade') +print(new_dec) + +for genre in root.findall("./genre"): + print(genre.attrib) + +print(ET.tostring(root, encoding = 'utf8').decode('utf8')) +new_dec.attrib['years'] = '2000s' + +#xmen = root.find("./genre/decade/movie/[@title = 'X-Men']") +#dec2000 = root.find("./genre[@category = 'Action']/decade[@years = '2000s']") +#dec2000.append(xmen) +#dec1990 = root.find("./genre/[@category = 'Action']/decade[@years = '1990s']") +#dec1990.remove(xmen) +#tree.write('movies.xml') +#print(ET.tostring(root, encoding = 'utf8').decode('utf8')) + +new_anime1 = ET.SubElement(root, 'genre') +print(new_anime1) + +new_anime1.attrib['category'] = 'Anime' +print(ET.tostring(root, encoding='utf8').decode('utf8')) + +add1 = root.find("./genre[@category = 'Anime']") +new_dec2 = ET.SubElement(add1, 'decade') +print(new_dec2) + +new_dec2.attrib['years'] = '2000s' +btman = root.find("./genre/decade/movie/[@title = 'Batman Returns']") +dec_2000 = root.find("./genre[@category = 'Anime']/decade[@years = '2000s']") +dec_2000.append(btman) +dec_1990 = root.find("./genre/[@category = 'Action']/decade[@years = '1990s']") +dec_1990.remove(btman) + diff --git a/StarwarsapiStarshipsJSON.ipynb b/StarwarsapiStarshipsJSON.ipynb new file mode 100644 index 0000000..5c30baa --- /dev/null +++ b/StarwarsapiStarshipsJSON.ipynb @@ -0,0 +1,1014 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "import requests\n", + "import json\n", + "from pprint import pp" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "baseurl = 'https://swapi.dev/api/starships/?page='\n", + "new_list = []\n", + "\n", + "for i in range(1,5):\n", + " response1 = requests.get(baseurl + str(i)) \n", + " r = response1.json()\n", + " new_list.append(r)\n", + "#pp(new_list)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[{'name': 'CR90 corvette', 'model': 'CR90 corvette', 'manufacturer': 'Corellian Engineering Corporation', 'cost_in_credits': '3500000', 'length': '150', 'max_atmosphering_speed': '950', 'crew': '30-165', 'passengers': '600', 'cargo_capacity': '3000000', 'consumables': '1 year', 'hyperdrive_rating': '2.0', 'MGLT': '60', 'starship_class': 'corvette', 'pilots': [], 'films': ['https://swapi.dev/api/films/1/', 'https://swapi.dev/api/films/3/', 'https://swapi.dev/api/films/6/'], 'created': '2014-12-10T14:20:33.369000Z', 'edited': '2014-12-20T21:23:49.867000Z', 'url': 'https://swapi.dev/api/starships/2/'}, {'name': 'Star Destroyer', 'model': 'Imperial I-class Star Destroyer', 'manufacturer': 'Kuat Drive Yards', 'cost_in_credits': '150000000', 'length': '1,600', 'max_atmosphering_speed': '975', 'crew': '47,060', 'passengers': 'n/a', 'cargo_capacity': '36000000', 'consumables': '2 years', 'hyperdrive_rating': '2.0', 'MGLT': '60', 'starship_class': 'Star Destroyer', 'pilots': [], 'films': ['https://swapi.dev/api/films/1/', 'https://swapi.dev/api/films/2/', 'https://swapi.dev/api/films/3/'], 'created': '2014-12-10T15:08:19.848000Z', 'edited': '2014-12-20T21:23:49.870000Z', 'url': 'https://swapi.dev/api/starships/3/'}, {'name': 'Sentinel-class landing craft', 'model': 'Sentinel-class landing craft', 'manufacturer': 'Sienar Fleet Systems, Cyngus Spaceworks', 'cost_in_credits': '240000', 'length': '38', 'max_atmosphering_speed': '1000', 'crew': '5', 'passengers': '75', 'cargo_capacity': '180000', 'consumables': '1 month', 'hyperdrive_rating': '1.0', 'MGLT': '70', 'starship_class': 'landing craft', 'pilots': [], 'films': ['https://swapi.dev/api/films/1/'], 'created': '2014-12-10T15:48:00.586000Z', 'edited': '2014-12-20T21:23:49.873000Z', 'url': 'https://swapi.dev/api/starships/5/'}, {'name': 'Death Star', 'model': 'DS-1 Orbital Battle Station', 'manufacturer': 'Imperial Department of Military Research, Sienar Fleet Systems', 'cost_in_credits': '1000000000000', 'length': '120000', 'max_atmosphering_speed': 'n/a', 'crew': '342,953', 'passengers': '843,342', 'cargo_capacity': '1000000000000', 'consumables': '3 years', 'hyperdrive_rating': '4.0', 'MGLT': '10', 'starship_class': 'Deep Space Mobile Battlestation', 'pilots': [], 'films': ['https://swapi.dev/api/films/1/'], 'created': '2014-12-10T16:36:50.509000Z', 'edited': '2014-12-20T21:26:24.783000Z', 'url': 'https://swapi.dev/api/starships/9/'}, {'name': 'Millennium Falcon', 'model': 'YT-1300 light freighter', 'manufacturer': 'Corellian Engineering Corporation', 'cost_in_credits': '100000', 'length': '34.37', 'max_atmosphering_speed': '1050', 'crew': '4', 'passengers': '6', 'cargo_capacity': '100000', 'consumables': '2 months', 'hyperdrive_rating': '0.5', 'MGLT': '75', 'starship_class': 'Light freighter', 'pilots': ['https://swapi.dev/api/people/13/', 'https://swapi.dev/api/people/14/', 'https://swapi.dev/api/people/25/', 'https://swapi.dev/api/people/31/'], 'films': ['https://swapi.dev/api/films/1/', 'https://swapi.dev/api/films/2/', 'https://swapi.dev/api/films/3/'], 'created': '2014-12-10T16:59:45.094000Z', 'edited': '2014-12-20T21:23:49.880000Z', 'url': 'https://swapi.dev/api/starships/10/'}, {'name': 'Y-wing', 'model': 'BTL Y-wing', 'manufacturer': 'Koensayr Manufacturing', 'cost_in_credits': '134999', 'length': '14', 'max_atmosphering_speed': '1000km', 'crew': '2', 'passengers': '0', 'cargo_capacity': '110', 'consumables': '1 week', 'hyperdrive_rating': '1.0', 'MGLT': '80', 'starship_class': 'assault starfighter', 'pilots': [], 'films': ['https://swapi.dev/api/films/1/', 'https://swapi.dev/api/films/2/', 'https://swapi.dev/api/films/3/'], 'created': '2014-12-12T11:00:39.817000Z', 'edited': '2014-12-20T21:23:49.883000Z', 'url': 'https://swapi.dev/api/starships/11/'}, {'name': 'X-wing', 'model': 'T-65 X-wing', 'manufacturer': 'Incom Corporation', 'cost_in_credits': '149999', 'length': '12.5', 'max_atmosphering_speed': '1050', 'crew': '1', 'passengers': '0', 'cargo_capacity': '110', 'consumables': '1 week', 'hyperdrive_rating': '1.0', 'MGLT': '100', 'starship_class': 'Starfighter', 'pilots': ['https://swapi.dev/api/people/1/', 'https://swapi.dev/api/people/9/', 'https://swapi.dev/api/people/18/', 'https://swapi.dev/api/people/19/'], 'films': ['https://swapi.dev/api/films/1/', 'https://swapi.dev/api/films/2/', 'https://swapi.dev/api/films/3/'], 'created': '2014-12-12T11:19:05.340000Z', 'edited': '2014-12-20T21:23:49.886000Z', 'url': 'https://swapi.dev/api/starships/12/'}, {'name': 'TIE Advanced x1', 'model': 'Twin Ion Engine Advanced x1', 'manufacturer': 'Sienar Fleet Systems', 'cost_in_credits': 'unknown', 'length': '9.2', 'max_atmosphering_speed': '1200', 'crew': '1', 'passengers': '0', 'cargo_capacity': '150', 'consumables': '5 days', 'hyperdrive_rating': '1.0', 'MGLT': '105', 'starship_class': 'Starfighter', 'pilots': ['https://swapi.dev/api/people/4/'], 'films': ['https://swapi.dev/api/films/1/'], 'created': '2014-12-12T11:21:32.991000Z', 'edited': '2014-12-20T21:23:49.889000Z', 'url': 'https://swapi.dev/api/starships/13/'}, {'name': 'Executor', 'model': 'Executor-class star dreadnought', 'manufacturer': 'Kuat Drive Yards, Fondor Shipyards', 'cost_in_credits': '1143350000', 'length': '19000', 'max_atmosphering_speed': 'n/a', 'crew': '279,144', 'passengers': '38000', 'cargo_capacity': '250000000', 'consumables': '6 years', 'hyperdrive_rating': '2.0', 'MGLT': '40', 'starship_class': 'Star dreadnought', 'pilots': [], 'films': ['https://swapi.dev/api/films/2/', 'https://swapi.dev/api/films/3/'], 'created': '2014-12-15T12:31:42.547000Z', 'edited': '2014-12-20T21:23:49.893000Z', 'url': 'https://swapi.dev/api/starships/15/'}, {'name': 'Rebel transport', 'model': 'GR-75 medium transport', 'manufacturer': 'Gallofree Yards, Inc.', 'cost_in_credits': 'unknown', 'length': '90', 'max_atmosphering_speed': '650', 'crew': '6', 'passengers': '90', 'cargo_capacity': '19000000', 'consumables': '6 months', 'hyperdrive_rating': '4.0', 'MGLT': '20', 'starship_class': 'Medium transport', 'pilots': [], 'films': ['https://swapi.dev/api/films/2/', 'https://swapi.dev/api/films/3/'], 'created': '2014-12-15T12:34:52.264000Z', 'edited': '2014-12-20T21:23:49.895000Z', 'url': 'https://swapi.dev/api/starships/17/'}, {'name': 'Slave 1', 'model': 'Firespray-31-class patrol and attack', 'manufacturer': 'Kuat Systems Engineering', 'cost_in_credits': 'unknown', 'length': '21.5', 'max_atmosphering_speed': '1000', 'crew': '1', 'passengers': '6', 'cargo_capacity': '70000', 'consumables': '1 month', 'hyperdrive_rating': '3.0', 'MGLT': '70', 'starship_class': 'Patrol craft', 'pilots': ['https://swapi.dev/api/people/22/'], 'films': ['https://swapi.dev/api/films/2/', 'https://swapi.dev/api/films/5/'], 'created': '2014-12-15T13:00:56.332000Z', 'edited': '2014-12-20T21:23:49.897000Z', 'url': 'https://swapi.dev/api/starships/21/'}, {'name': 'Imperial shuttle', 'model': 'Lambda-class T-4a shuttle', 'manufacturer': 'Sienar Fleet Systems', 'cost_in_credits': '240000', 'length': '20', 'max_atmosphering_speed': '850', 'crew': '6', 'passengers': '20', 'cargo_capacity': '80000', 'consumables': '2 months', 'hyperdrive_rating': '1.0', 'MGLT': '50', 'starship_class': 'Armed government transport', 'pilots': ['https://swapi.dev/api/people/1/', 'https://swapi.dev/api/people/13/', 'https://swapi.dev/api/people/14/'], 'films': ['https://swapi.dev/api/films/2/', 'https://swapi.dev/api/films/3/'], 'created': '2014-12-15T13:04:47.235000Z', 'edited': '2014-12-20T21:23:49.900000Z', 'url': 'https://swapi.dev/api/starships/22/'}, {'name': 'EF76 Nebulon-B escort frigate', 'model': 'EF76 Nebulon-B escort frigate', 'manufacturer': 'Kuat Drive Yards', 'cost_in_credits': '8500000', 'length': '300', 'max_atmosphering_speed': '800', 'crew': '854', 'passengers': '75', 'cargo_capacity': '6000000', 'consumables': '2 years', 'hyperdrive_rating': '2.0', 'MGLT': '40', 'starship_class': 'Escort ship', 'pilots': [], 'films': ['https://swapi.dev/api/films/2/', 'https://swapi.dev/api/films/3/'], 'created': '2014-12-15T13:06:30.813000Z', 'edited': '2014-12-20T21:23:49.902000Z', 'url': 'https://swapi.dev/api/starships/23/'}, {'name': 'Calamari Cruiser', 'model': 'MC80 Liberty type Star Cruiser', 'manufacturer': 'Mon Calamari shipyards', 'cost_in_credits': '104000000', 'length': '1200', 'max_atmosphering_speed': 'n/a', 'crew': '5400', 'passengers': '1200', 'cargo_capacity': 'unknown', 'consumables': '2 years', 'hyperdrive_rating': '1.0', 'MGLT': '60', 'starship_class': 'Star Cruiser', 'pilots': [], 'films': ['https://swapi.dev/api/films/3/'], 'created': '2014-12-18T10:54:57.804000Z', 'edited': '2014-12-20T21:23:49.904000Z', 'url': 'https://swapi.dev/api/starships/27/'}, {'name': 'A-wing', 'model': 'RZ-1 A-wing Interceptor', 'manufacturer': 'Alliance Underground Engineering, Incom Corporation', 'cost_in_credits': '175000', 'length': '9.6', 'max_atmosphering_speed': '1300', 'crew': '1', 'passengers': '0', 'cargo_capacity': '40', 'consumables': '1 week', 'hyperdrive_rating': '1.0', 'MGLT': '120', 'starship_class': 'Starfighter', 'pilots': ['https://swapi.dev/api/people/29/'], 'films': ['https://swapi.dev/api/films/3/'], 'created': '2014-12-18T11:16:34.542000Z', 'edited': '2014-12-20T21:23:49.907000Z', 'url': 'https://swapi.dev/api/starships/28/'}, {'name': 'B-wing', 'model': 'A/SF-01 B-wing starfighter', 'manufacturer': 'Slayn & Korpil', 'cost_in_credits': '220000', 'length': '16.9', 'max_atmosphering_speed': '950', 'crew': '1', 'passengers': '0', 'cargo_capacity': '45', 'consumables': '1 week', 'hyperdrive_rating': '2.0', 'MGLT': '91', 'starship_class': 'Assault Starfighter', 'pilots': [], 'films': ['https://swapi.dev/api/films/3/'], 'created': '2014-12-18T11:18:04.763000Z', 'edited': '2014-12-20T21:23:49.909000Z', 'url': 'https://swapi.dev/api/starships/29/'}, {'name': 'Republic Cruiser', 'model': 'Consular-class cruiser', 'manufacturer': 'Corellian Engineering Corporation', 'cost_in_credits': 'unknown', 'length': '115', 'max_atmosphering_speed': '900', 'crew': '9', 'passengers': '16', 'cargo_capacity': 'unknown', 'consumables': 'unknown', 'hyperdrive_rating': '2.0', 'MGLT': 'unknown', 'starship_class': 'Space cruiser', 'pilots': [], 'films': ['https://swapi.dev/api/films/4/'], 'created': '2014-12-19T17:01:31.488000Z', 'edited': '2014-12-20T21:23:49.912000Z', 'url': 'https://swapi.dev/api/starships/31/'}, {'name': 'Droid control ship', 'model': 'Lucrehulk-class Droid Control Ship', 'manufacturer': 'Hoersch-Kessel Drive, Inc.', 'cost_in_credits': 'unknown', 'length': '3170', 'max_atmosphering_speed': 'n/a', 'crew': '175', 'passengers': '139000', 'cargo_capacity': '4000000000', 'consumables': '500 days', 'hyperdrive_rating': '2.0', 'MGLT': 'unknown', 'starship_class': 'Droid control ship', 'pilots': [], 'films': ['https://swapi.dev/api/films/4/', 'https://swapi.dev/api/films/5/', 'https://swapi.dev/api/films/6/'], 'created': '2014-12-19T17:04:06.323000Z', 'edited': '2014-12-20T21:23:49.915000Z', 'url': 'https://swapi.dev/api/starships/32/'}, {'name': 'Naboo fighter', 'model': 'N-1 starfighter', 'manufacturer': 'Theed Palace Space Vessel Engineering Corps', 'cost_in_credits': '200000', 'length': '11', 'max_atmosphering_speed': '1100', 'crew': '1', 'passengers': '0', 'cargo_capacity': '65', 'consumables': '7 days', 'hyperdrive_rating': '1.0', 'MGLT': 'unknown', 'starship_class': 'Starfighter', 'pilots': ['https://swapi.dev/api/people/11/', 'https://swapi.dev/api/people/35/', 'https://swapi.dev/api/people/60/'], 'films': ['https://swapi.dev/api/films/4/', 'https://swapi.dev/api/films/5/'], 'created': '2014-12-19T17:39:17.582000Z', 'edited': '2014-12-20T21:23:49.917000Z', 'url': 'https://swapi.dev/api/starships/39/'}, {'name': 'Naboo Royal Starship', 'model': 'J-type 327 Nubian royal starship', 'manufacturer': 'Theed Palace Space Vessel Engineering Corps, Nubia Star Drives', 'cost_in_credits': 'unknown', 'length': '76', 'max_atmosphering_speed': '920', 'crew': '8', 'passengers': 'unknown', 'cargo_capacity': 'unknown', 'consumables': 'unknown', 'hyperdrive_rating': '1.8', 'MGLT': 'unknown', 'starship_class': 'yacht', 'pilots': ['https://swapi.dev/api/people/39/'], 'films': ['https://swapi.dev/api/films/4/'], 'created': '2014-12-19T17:45:03.506000Z', 'edited': '2014-12-20T21:23:49.919000Z', 'url': 'https://swapi.dev/api/starships/40/'}, {'name': 'Scimitar', 'model': 'Star Courier', 'manufacturer': 'Republic Sienar Systems', 'cost_in_credits': '55000000', 'length': '26.5', 'max_atmosphering_speed': '1180', 'crew': '1', 'passengers': '6', 'cargo_capacity': '2500000', 'consumables': '30 days', 'hyperdrive_rating': '1.5', 'MGLT': 'unknown', 'starship_class': 'Space Transport', 'pilots': ['https://swapi.dev/api/people/44/'], 'films': ['https://swapi.dev/api/films/4/'], 'created': '2014-12-20T09:39:56.116000Z', 'edited': '2014-12-20T21:23:49.922000Z', 'url': 'https://swapi.dev/api/starships/41/'}, {'name': 'J-type diplomatic barge', 'model': 'J-type diplomatic barge', 'manufacturer': 'Theed Palace Space Vessel Engineering Corps, Nubia Star Drives', 'cost_in_credits': '2000000', 'length': '39', 'max_atmosphering_speed': '2000', 'crew': '5', 'passengers': '10', 'cargo_capacity': 'unknown', 'consumables': '1 year', 'hyperdrive_rating': '0.7', 'MGLT': 'unknown', 'starship_class': 'Diplomatic barge', 'pilots': [], 'films': ['https://swapi.dev/api/films/5/'], 'created': '2014-12-20T11:05:51.237000Z', 'edited': '2014-12-20T21:23:49.925000Z', 'url': 'https://swapi.dev/api/starships/43/'}, {'name': 'AA-9 Coruscant freighter', 'model': 'Botajef AA-9 Freighter-Liner', 'manufacturer': 'Botajef Shipyards', 'cost_in_credits': 'unknown', 'length': '390', 'max_atmosphering_speed': 'unknown', 'crew': 'unknown', 'passengers': '30000', 'cargo_capacity': 'unknown', 'consumables': 'unknown', 'hyperdrive_rating': 'unknown', 'MGLT': 'unknown', 'starship_class': 'freighter', 'pilots': [], 'films': ['https://swapi.dev/api/films/5/'], 'created': '2014-12-20T17:24:23.509000Z', 'edited': '2014-12-20T21:23:49.928000Z', 'url': 'https://swapi.dev/api/starships/47/'}, {'name': 'Jedi starfighter', 'model': 'Delta-7 Aethersprite-class interceptor', 'manufacturer': 'Kuat Systems Engineering', 'cost_in_credits': '180000', 'length': '8', 'max_atmosphering_speed': '1150', 'crew': '1', 'passengers': '0', 'cargo_capacity': '60', 'consumables': '7 days', 'hyperdrive_rating': '1.0', 'MGLT': 'unknown', 'starship_class': 'Starfighter', 'pilots': ['https://swapi.dev/api/people/10/', 'https://swapi.dev/api/people/58/'], 'films': ['https://swapi.dev/api/films/5/', 'https://swapi.dev/api/films/6/'], 'created': '2014-12-20T17:35:23.906000Z', 'edited': '2014-12-20T21:23:49.930000Z', 'url': 'https://swapi.dev/api/starships/48/'}, {'name': 'H-type Nubian yacht', 'model': 'H-type Nubian yacht', 'manufacturer': 'Theed Palace Space Vessel Engineering Corps', 'cost_in_credits': 'unknown', 'length': '47.9', 'max_atmosphering_speed': '8000', 'crew': '4', 'passengers': 'unknown', 'cargo_capacity': 'unknown', 'consumables': 'unknown', 'hyperdrive_rating': '0.9', 'MGLT': 'unknown', 'starship_class': 'yacht', 'pilots': ['https://swapi.dev/api/people/35/'], 'films': ['https://swapi.dev/api/films/5/'], 'created': '2014-12-20T17:46:46.847000Z', 'edited': '2014-12-20T21:23:49.932000Z', 'url': 'https://swapi.dev/api/starships/49/'}, {'name': 'Republic Assault ship', 'model': 'Acclamator I-class assault ship', 'manufacturer': 'Rothana Heavy Engineering', 'cost_in_credits': 'unknown', 'length': '752', 'max_atmosphering_speed': 'unknown', 'crew': '700', 'passengers': '16000', 'cargo_capacity': '11250000', 'consumables': '2 years', 'hyperdrive_rating': '0.6', 'MGLT': 'unknown', 'starship_class': 'assault ship', 'pilots': [], 'films': ['https://swapi.dev/api/films/5/'], 'created': '2014-12-20T18:08:42.926000Z', 'edited': '2014-12-20T21:23:49.935000Z', 'url': 'https://swapi.dev/api/starships/52/'}, {'name': 'Solar Sailer', 'model': 'Punworcca 116-class interstellar sloop', 'manufacturer': 'Huppla Pasa Tisc Shipwrights Collective', 'cost_in_credits': '35700', 'length': '15.2', 'max_atmosphering_speed': '1600', 'crew': '3', 'passengers': '11', 'cargo_capacity': '240', 'consumables': '7 days', 'hyperdrive_rating': '1.5', 'MGLT': 'unknown', 'starship_class': 'yacht', 'pilots': [], 'films': ['https://swapi.dev/api/films/5/'], 'created': '2014-12-20T18:37:56.969000Z', 'edited': '2014-12-20T21:23:49.937000Z', 'url': 'https://swapi.dev/api/starships/58/'}, {'name': 'Trade Federation cruiser', 'model': 'Providence-class carrier/destroyer', 'manufacturer': 'Rendili StarDrive, Free Dac Volunteers Engineering corps.', 'cost_in_credits': '125000000', 'length': '1088', 'max_atmosphering_speed': '1050', 'crew': '600', 'passengers': '48247', 'cargo_capacity': '50000000', 'consumables': '4 years', 'hyperdrive_rating': '1.5', 'MGLT': 'unknown', 'starship_class': 'capital ship', 'pilots': ['https://swapi.dev/api/people/10/', 'https://swapi.dev/api/people/11/'], 'films': ['https://swapi.dev/api/films/6/'], 'created': '2014-12-20T19:40:21.902000Z', 'edited': '2014-12-20T21:23:49.941000Z', 'url': 'https://swapi.dev/api/starships/59/'}, {'name': 'Theta-class T-2c shuttle', 'model': 'Theta-class T-2c shuttle', 'manufacturer': 'Cygnus Spaceworks', 'cost_in_credits': '1000000', 'length': '18.5', 'max_atmosphering_speed': '2000', 'crew': '5', 'passengers': '16', 'cargo_capacity': '50000', 'consumables': '56 days', 'hyperdrive_rating': '1.0', 'MGLT': 'unknown', 'starship_class': 'transport', 'pilots': [], 'films': ['https://swapi.dev/api/films/6/'], 'created': '2014-12-20T19:48:40.409000Z', 'edited': '2014-12-20T21:23:49.944000Z', 'url': 'https://swapi.dev/api/starships/61/'}, {'name': 'Republic attack cruiser', 'model': 'Senator-class Star Destroyer', 'manufacturer': 'Kuat Drive Yards, Allanteen Six shipyards', 'cost_in_credits': '59000000', 'length': '1137', 'max_atmosphering_speed': '975', 'crew': '7400', 'passengers': '2000', 'cargo_capacity': '20000000', 'consumables': '2 years', 'hyperdrive_rating': '1.0', 'MGLT': 'unknown', 'starship_class': 'star destroyer', 'pilots': [], 'films': ['https://swapi.dev/api/films/6/'], 'created': '2014-12-20T19:52:56.232000Z', 'edited': '2014-12-20T21:23:49.946000Z', 'url': 'https://swapi.dev/api/starships/63/'}]\n", + "['CR90 corvette', 'Star Destroyer', 'Sentinel-class landing craft', 'Death Star', 'Millennium Falcon', 'Y-wing', 'X-wing', 'TIE Advanced x1', 'Executor', 'Rebel transport', 'Slave 1', 'Imperial shuttle', 'EF76 Nebulon-B escort frigate', 'Calamari Cruiser', 'A-wing', 'B-wing', 'Republic Cruiser', 'Droid control ship', 'Naboo fighter', 'Naboo Royal Starship', 'Scimitar', 'J-type diplomatic barge', 'AA-9 Coruscant freighter', 'Jedi starfighter', 'H-type Nubian yacht', 'Republic Assault ship', 'Solar Sailer', 'Trade Federation cruiser', 'Theta-class T-2c shuttle', 'Republic attack cruiser']\n", + "Death Star\n", + "Name and crew of the {ship_name}: ('CR90 corvette', '30-165')\n", + "35700\n", + "('1000000000000', 'Death Star')\n", + "CR90 corvette\n", + "Star Destroyer\n", + "Sentinel-class landing craft\n", + "Death Star\n", + "Millennium Falcon\n", + "Y-wing\n", + "X-wing\n", + "Executor\n", + "Imperial shuttle\n", + "EF76 Nebulon-B escort frigate\n", + "Calamari Cruiser\n", + "A-wing\n", + "B-wing\n", + "Naboo fighter\n", + "Scimitar\n", + "J-type diplomatic barge\n", + "Jedi starfighter\n", + "Solar Sailer\n", + "Trade Federation cruiser\n", + "Theta-class T-2c shuttle\n", + "Republic attack cruiser\n", + "[{'title': 'The Empire Strikes Back',\n", + " 'episode_id': 5,\n", + " 'opening_crawl': 'It is a dark time for the\\r\\n'\n", + " 'Rebellion. Although the Death\\r\\n'\n", + " 'Star has been destroyed,\\r\\n'\n", + " 'Imperial troops have driven the\\r\\n'\n", + " 'Rebel forces from their hidden\\r\\n'\n", + " 'base and pursued them across\\r\\n'\n", + " 'the galaxy.\\r\\n'\n", + " '\\r\\n'\n", + " 'Evading the dreaded Imperial\\r\\n'\n", + " 'Starfleet, a group of freedom\\r\\n'\n", + " 'fighters led by Luke Skywalker\\r\\n'\n", + " 'has established a new secret\\r\\n'\n", + " 'base on the remote ice world\\r\\n'\n", + " 'of Hoth.\\r\\n'\n", + " '\\r\\n'\n", + " 'The evil lord Darth Vader,\\r\\n'\n", + " 'obsessed with finding young\\r\\n'\n", + " 'Skywalker, has dispatched\\r\\n'\n", + " 'thousands of remote probes into\\r\\n'\n", + " 'the far reaches of space....',\n", + " 'director': 'Irvin Kershner',\n", + " 'producer': 'Gary Kurtz, Rick McCallum',\n", + " 'release_date': '1980-05-17',\n", + " 'characters': ['https://swapi.dev/api/people/1/',\n", + " 'https://swapi.dev/api/people/2/',\n", + " 'https://swapi.dev/api/people/3/',\n", + " 'https://swapi.dev/api/people/4/',\n", + " 'https://swapi.dev/api/people/5/',\n", + " 'https://swapi.dev/api/people/10/',\n", + " 'https://swapi.dev/api/people/13/',\n", + " 'https://swapi.dev/api/people/14/',\n", + " 'https://swapi.dev/api/people/18/',\n", + " 'https://swapi.dev/api/people/20/',\n", + " 'https://swapi.dev/api/people/21/',\n", + " 'https://swapi.dev/api/people/22/',\n", + " 'https://swapi.dev/api/people/23/',\n", + " 'https://swapi.dev/api/people/24/',\n", + " 'https://swapi.dev/api/people/25/',\n", + " 'https://swapi.dev/api/people/26/'],\n", + " 'planets': ['https://swapi.dev/api/planets/4/',\n", + " 'https://swapi.dev/api/planets/5/',\n", + " 'https://swapi.dev/api/planets/6/',\n", + " 'https://swapi.dev/api/planets/27/'],\n", + " 'starships': ['https://swapi.dev/api/starships/3/',\n", + " 'https://swapi.dev/api/starships/10/',\n", + " 'https://swapi.dev/api/starships/11/',\n", + " 'https://swapi.dev/api/starships/12/',\n", + " 'https://swapi.dev/api/starships/15/',\n", + " 'https://swapi.dev/api/starships/17/',\n", + " 'https://swapi.dev/api/starships/21/',\n", + " 'https://swapi.dev/api/starships/22/',\n", + " 'https://swapi.dev/api/starships/23/'],\n", + " 'vehicles': ['https://swapi.dev/api/vehicles/8/',\n", + " 'https://swapi.dev/api/vehicles/14/',\n", + " 'https://swapi.dev/api/vehicles/16/',\n", + " 'https://swapi.dev/api/vehicles/18/',\n", + " 'https://swapi.dev/api/vehicles/19/',\n", + " 'https://swapi.dev/api/vehicles/20/'],\n", + " 'species': ['https://swapi.dev/api/species/1/',\n", + " 'https://swapi.dev/api/species/2/',\n", + " 'https://swapi.dev/api/species/3/',\n", + " 'https://swapi.dev/api/species/6/',\n", + " 'https://swapi.dev/api/species/7/'],\n", + " 'created': '2014-12-12T11:26:24.656000Z',\n", + " 'edited': '2014-12-15T13:07:53.386000Z',\n", + " 'url': 'https://swapi.dev/api/films/2/'}]\n", + "starship_list: ['https://swapi.dev/api/starships/3/', 'https://swapi.dev/api/starships/10/', 'https://swapi.dev/api/starships/11/', 'https://swapi.dev/api/starships/12/', 'https://swapi.dev/api/starships/15/', 'https://swapi.dev/api/starships/17/', 'https://swapi.dev/api/starships/21/', 'https://swapi.dev/api/starships/22/', 'https://swapi.dev/api/starships/23/']\n", + "number of starships: 9\n" + ] + } + ], + "source": [ + "result_list = []\n", + "model_names = []\n", + "cargo_list = []\n", + "crew_list = []\n", + "cost_list = []\n", + "films_list = []\n", + "\n", + "\n", + "\n", + "for a in new_list:\n", + " for b in a['results']:\n", + " result_list.append(b)\n", + "print(result_list)\n", + "\n", + "#problem1\n", + "def get_allnames():\n", + " for c in result_list:\n", + " model_names.append(c['name'])\n", + " return model_names\n", + "\n", + "print(get_allnames())\n", + "\n", + "\n", + "#problem2\n", + "def most_cargo():\n", + " for d in result_list:\n", + " if d['cargo_capacity'].isnumeric(): \n", + " cargo_list.append(int((d['cargo_capacity'])))\n", + "\n", + " for e in result_list:\n", + " if e['cargo_capacity'].isnumeric() and int(e['cargo_capacity']) == max(cargo_list):\n", + " print(e['name'])\n", + "\n", + "print(most_cargo())\n", + "\n", + "#problem3\n", + "def name_crew(shipname):\n", + " for f in result_list:\n", + " if f['starship_class'] == shipname:\n", + " print('Name and crew of the {ship_name}:', (f['name'], f['crew']))\n", + "\n", + "print(name_crew())\n", + "\n", + "\n", + "#problem4\n", + "def result_list():\n", + " for g in result_list:\n", + " if g['cost_in_credits'].isnumeric():\n", + " cost_list.append(int(g['cost_in_credits']))\n", + "print(min(cost_list))\n", + "\n", + "for h in result_list:\n", + " if h['cost_in_credits'].isnumeric() and int(h['cost_in_credits']) == max(cost_list):\n", + " print((h['cost_in_credits'], h['name'] ))\n", + "\n", + "#problem5\n", + "cost = 100\n", + "for i in result_list:\n", + " if i['cost_in_credits'].isnumeric() and int(i['cost_in_credits']) > cost:\n", + " print(i['name'])\n", + "\n", + "\n", + "#problem6\n", + "film_list = []\n", + "x = 2\n", + "starship_list = []\n", + "for i in result_list:\n", + " if len(i['films']) == x:\n", + " starship_list.append(i['name'])\n", + " \n", + "print('starship_list:', starship_list)\n", + "print('number of starships:', len(starship_list))\n", + "\n", + "#problem7\n", + "length_list = []\n", + "def ShipMaxMinLength():\n", + " for i in result_list:\n", + " length_list.append(i['length'])\n", + "print(length_list)\n", + "\n", + " \n", + "\n", + "\n", + " \n", + "\n", + " \n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[{'name': 'CR90 corvette',\n", + " 'model': 'CR90 corvette',\n", + " 'manufacturer': 'Corellian Engineering Corporation',\n", + " 'cost_in_credits': '3500000',\n", + " 'length': '150',\n", + " 'max_atmosphering_speed': '950',\n", + " 'crew': '30-165',\n", + " 'passengers': '600',\n", + " 'cargo_capacity': '3000000',\n", + " 'consumables': '1 year',\n", + " 'hyperdrive_rating': '2.0',\n", + " 'MGLT': '60',\n", + " 'starship_class': 'corvette',\n", + " 'pilots': [],\n", + " 'films': ['https://swapi.dev/api/films/1/',\n", + " 'https://swapi.dev/api/films/3/',\n", + " 'https://swapi.dev/api/films/6/'],\n", + " 'created': '2014-12-10T14:20:33.369000Z',\n", + " 'edited': '2014-12-20T21:23:49.867000Z',\n", + " 'url': 'https://swapi.dev/api/starships/2/'},\n", + " {'name': 'Star Destroyer',\n", + " 'model': 'Imperial I-class Star Destroyer',\n", + " 'manufacturer': 'Kuat Drive Yards',\n", + " 'cost_in_credits': '150000000',\n", + " 'length': '1,600',\n", + " 'max_atmosphering_speed': '975',\n", + " 'crew': '47,060',\n", + " 'passengers': 'n/a',\n", + " 'cargo_capacity': '36000000',\n", + " 'consumables': '2 years',\n", + " 'hyperdrive_rating': '2.0',\n", + " 'MGLT': '60',\n", + " 'starship_class': 'Star Destroyer',\n", + " 'pilots': [],\n", + " 'films': ['https://swapi.dev/api/films/1/',\n", + " 'https://swapi.dev/api/films/2/',\n", + " 'https://swapi.dev/api/films/3/'],\n", + " 'created': '2014-12-10T15:08:19.848000Z',\n", + " 'edited': '2014-12-20T21:23:49.870000Z',\n", + " 'url': 'https://swapi.dev/api/starships/3/'},\n", + " {'name': 'Sentinel-class landing craft',\n", + " 'model': 'Sentinel-class landing craft',\n", + " 'manufacturer': 'Sienar Fleet Systems, Cyngus Spaceworks',\n", + " 'cost_in_credits': '240000',\n", + " 'length': '38',\n", + " 'max_atmosphering_speed': '1000',\n", + " 'crew': '5',\n", + " 'passengers': '75',\n", + " 'cargo_capacity': '180000',\n", + " 'consumables': '1 month',\n", + " 'hyperdrive_rating': '1.0',\n", + " 'MGLT': '70',\n", + " 'starship_class': 'landing craft',\n", + " 'pilots': [],\n", + " 'films': ['https://swapi.dev/api/films/1/'],\n", + " 'created': '2014-12-10T15:48:00.586000Z',\n", + " 'edited': '2014-12-20T21:23:49.873000Z',\n", + " 'url': 'https://swapi.dev/api/starships/5/'},\n", + " {'name': 'Death Star',\n", + " 'model': 'DS-1 Orbital Battle Station',\n", + " 'manufacturer': 'Imperial Department of Military Research, Sienar Fleet '\n", + " 'Systems',\n", + " 'cost_in_credits': '1000000000000',\n", + " 'length': '120000',\n", + " 'max_atmosphering_speed': 'n/a',\n", + " 'crew': '342,953',\n", + " 'passengers': '843,342',\n", + " 'cargo_capacity': '1000000000000',\n", + " 'consumables': '3 years',\n", + " 'hyperdrive_rating': '4.0',\n", + " 'MGLT': '10',\n", + " 'starship_class': 'Deep Space Mobile Battlestation',\n", + " 'pilots': [],\n", + " 'films': ['https://swapi.dev/api/films/1/'],\n", + " 'created': '2014-12-10T16:36:50.509000Z',\n", + " 'edited': '2014-12-20T21:26:24.783000Z',\n", + " 'url': 'https://swapi.dev/api/starships/9/'},\n", + " {'name': 'Millennium Falcon',\n", + " 'model': 'YT-1300 light freighter',\n", + " 'manufacturer': 'Corellian Engineering Corporation',\n", + " 'cost_in_credits': '100000',\n", + " 'length': '34.37',\n", + " 'max_atmosphering_speed': '1050',\n", + " 'crew': '4',\n", + " 'passengers': '6',\n", + " 'cargo_capacity': '100000',\n", + " 'consumables': '2 months',\n", + " 'hyperdrive_rating': '0.5',\n", + " 'MGLT': '75',\n", + " 'starship_class': 'Light freighter',\n", + " 'pilots': ['https://swapi.dev/api/people/13/',\n", + " 'https://swapi.dev/api/people/14/',\n", + " 'https://swapi.dev/api/people/25/',\n", + " 'https://swapi.dev/api/people/31/'],\n", + " 'films': ['https://swapi.dev/api/films/1/',\n", + " 'https://swapi.dev/api/films/2/',\n", + " 'https://swapi.dev/api/films/3/'],\n", + " 'created': '2014-12-10T16:59:45.094000Z',\n", + " 'edited': '2014-12-20T21:23:49.880000Z',\n", + " 'url': 'https://swapi.dev/api/starships/10/'},\n", + " {'name': 'Y-wing',\n", + " 'model': 'BTL Y-wing',\n", + " 'manufacturer': 'Koensayr Manufacturing',\n", + " 'cost_in_credits': '134999',\n", + " 'length': '14',\n", + " 'max_atmosphering_speed': '1000km',\n", + " 'crew': '2',\n", + " 'passengers': '0',\n", + " 'cargo_capacity': '110',\n", + " 'consumables': '1 week',\n", + " 'hyperdrive_rating': '1.0',\n", + " 'MGLT': '80',\n", + " 'starship_class': 'assault starfighter',\n", + " 'pilots': [],\n", + " 'films': ['https://swapi.dev/api/films/1/',\n", + " 'https://swapi.dev/api/films/2/',\n", + " 'https://swapi.dev/api/films/3/'],\n", + " 'created': '2014-12-12T11:00:39.817000Z',\n", + " 'edited': '2014-12-20T21:23:49.883000Z',\n", + " 'url': 'https://swapi.dev/api/starships/11/'},\n", + " {'name': 'X-wing',\n", + " 'model': 'T-65 X-wing',\n", + " 'manufacturer': 'Incom Corporation',\n", + " 'cost_in_credits': '149999',\n", + " 'length': '12.5',\n", + " 'max_atmosphering_speed': '1050',\n", + " 'crew': '1',\n", + " 'passengers': '0',\n", + " 'cargo_capacity': '110',\n", + " 'consumables': '1 week',\n", + " 'hyperdrive_rating': '1.0',\n", + " 'MGLT': '100',\n", + " 'starship_class': 'Starfighter',\n", + " 'pilots': ['https://swapi.dev/api/people/1/',\n", + " 'https://swapi.dev/api/people/9/',\n", + " 'https://swapi.dev/api/people/18/',\n", + " 'https://swapi.dev/api/people/19/'],\n", + " 'films': ['https://swapi.dev/api/films/1/',\n", + " 'https://swapi.dev/api/films/2/',\n", + " 'https://swapi.dev/api/films/3/'],\n", + " 'created': '2014-12-12T11:19:05.340000Z',\n", + " 'edited': '2014-12-20T21:23:49.886000Z',\n", + " 'url': 'https://swapi.dev/api/starships/12/'},\n", + " {'name': 'TIE Advanced x1',\n", + " 'model': 'Twin Ion Engine Advanced x1',\n", + " 'manufacturer': 'Sienar Fleet Systems',\n", + " 'cost_in_credits': 'unknown',\n", + " 'length': '9.2',\n", + " 'max_atmosphering_speed': '1200',\n", + " 'crew': '1',\n", + " 'passengers': '0',\n", + " 'cargo_capacity': '150',\n", + " 'consumables': '5 days',\n", + " 'hyperdrive_rating': '1.0',\n", + " 'MGLT': '105',\n", + " 'starship_class': 'Starfighter',\n", + " 'pilots': ['https://swapi.dev/api/people/4/'],\n", + " 'films': ['https://swapi.dev/api/films/1/'],\n", + " 'created': '2014-12-12T11:21:32.991000Z',\n", + " 'edited': '2014-12-20T21:23:49.889000Z',\n", + " 'url': 'https://swapi.dev/api/starships/13/'},\n", + " {'name': 'Executor',\n", + " 'model': 'Executor-class star dreadnought',\n", + " 'manufacturer': 'Kuat Drive Yards, Fondor Shipyards',\n", + " 'cost_in_credits': '1143350000',\n", + " 'length': '19000',\n", + " 'max_atmosphering_speed': 'n/a',\n", + " 'crew': '279,144',\n", + " 'passengers': '38000',\n", + " 'cargo_capacity': '250000000',\n", + " 'consumables': '6 years',\n", + " 'hyperdrive_rating': '2.0',\n", + " 'MGLT': '40',\n", + " 'starship_class': 'Star dreadnought',\n", + " 'pilots': [],\n", + " 'films': ['https://swapi.dev/api/films/2/', 'https://swapi.dev/api/films/3/'],\n", + " 'created': '2014-12-15T12:31:42.547000Z',\n", + " 'edited': '2014-12-20T21:23:49.893000Z',\n", + " 'url': 'https://swapi.dev/api/starships/15/'},\n", + " {'name': 'Rebel transport',\n", + " 'model': 'GR-75 medium transport',\n", + " 'manufacturer': 'Gallofree Yards, Inc.',\n", + " 'cost_in_credits': 'unknown',\n", + " 'length': '90',\n", + " 'max_atmosphering_speed': '650',\n", + " 'crew': '6',\n", + " 'passengers': '90',\n", + " 'cargo_capacity': '19000000',\n", + " 'consumables': '6 months',\n", + " 'hyperdrive_rating': '4.0',\n", + " 'MGLT': '20',\n", + " 'starship_class': 'Medium transport',\n", + " 'pilots': [],\n", + " 'films': ['https://swapi.dev/api/films/2/', 'https://swapi.dev/api/films/3/'],\n", + " 'created': '2014-12-15T12:34:52.264000Z',\n", + " 'edited': '2014-12-20T21:23:49.895000Z',\n", + " 'url': 'https://swapi.dev/api/starships/17/'},\n", + " {'name': 'Slave 1',\n", + " 'model': 'Firespray-31-class patrol and attack',\n", + " 'manufacturer': 'Kuat Systems Engineering',\n", + " 'cost_in_credits': 'unknown',\n", + " 'length': '21.5',\n", + " 'max_atmosphering_speed': '1000',\n", + " 'crew': '1',\n", + " 'passengers': '6',\n", + " 'cargo_capacity': '70000',\n", + " 'consumables': '1 month',\n", + " 'hyperdrive_rating': '3.0',\n", + " 'MGLT': '70',\n", + " 'starship_class': 'Patrol craft',\n", + " 'pilots': ['https://swapi.dev/api/people/22/'],\n", + " 'films': ['https://swapi.dev/api/films/2/', 'https://swapi.dev/api/films/5/'],\n", + " 'created': '2014-12-15T13:00:56.332000Z',\n", + " 'edited': '2014-12-20T21:23:49.897000Z',\n", + " 'url': 'https://swapi.dev/api/starships/21/'},\n", + " {'name': 'Imperial shuttle',\n", + " 'model': 'Lambda-class T-4a shuttle',\n", + " 'manufacturer': 'Sienar Fleet Systems',\n", + " 'cost_in_credits': '240000',\n", + " 'length': '20',\n", + " 'max_atmosphering_speed': '850',\n", + " 'crew': '6',\n", + " 'passengers': '20',\n", + " 'cargo_capacity': '80000',\n", + " 'consumables': '2 months',\n", + " 'hyperdrive_rating': '1.0',\n", + " 'MGLT': '50',\n", + " 'starship_class': 'Armed government transport',\n", + " 'pilots': ['https://swapi.dev/api/people/1/',\n", + " 'https://swapi.dev/api/people/13/',\n", + " 'https://swapi.dev/api/people/14/'],\n", + " 'films': ['https://swapi.dev/api/films/2/', 'https://swapi.dev/api/films/3/'],\n", + " 'created': '2014-12-15T13:04:47.235000Z',\n", + " 'edited': '2014-12-20T21:23:49.900000Z',\n", + " 'url': 'https://swapi.dev/api/starships/22/'},\n", + " {'name': 'EF76 Nebulon-B escort frigate',\n", + " 'model': 'EF76 Nebulon-B escort frigate',\n", + " 'manufacturer': 'Kuat Drive Yards',\n", + " 'cost_in_credits': '8500000',\n", + " 'length': '300',\n", + " 'max_atmosphering_speed': '800',\n", + " 'crew': '854',\n", + " 'passengers': '75',\n", + " 'cargo_capacity': '6000000',\n", + " 'consumables': '2 years',\n", + " 'hyperdrive_rating': '2.0',\n", + " 'MGLT': '40',\n", + " 'starship_class': 'Escort ship',\n", + " 'pilots': [],\n", + " 'films': ['https://swapi.dev/api/films/2/', 'https://swapi.dev/api/films/3/'],\n", + " 'created': '2014-12-15T13:06:30.813000Z',\n", + " 'edited': '2014-12-20T21:23:49.902000Z',\n", + " 'url': 'https://swapi.dev/api/starships/23/'},\n", + " {'name': 'Calamari Cruiser',\n", + " 'model': 'MC80 Liberty type Star Cruiser',\n", + " 'manufacturer': 'Mon Calamari shipyards',\n", + " 'cost_in_credits': '104000000',\n", + " 'length': '1200',\n", + " 'max_atmosphering_speed': 'n/a',\n", + " 'crew': '5400',\n", + " 'passengers': '1200',\n", + " 'cargo_capacity': 'unknown',\n", + " 'consumables': '2 years',\n", + " 'hyperdrive_rating': '1.0',\n", + " 'MGLT': '60',\n", + " 'starship_class': 'Star Cruiser',\n", + " 'pilots': [],\n", + " 'films': ['https://swapi.dev/api/films/3/'],\n", + " 'created': '2014-12-18T10:54:57.804000Z',\n", + " 'edited': '2014-12-20T21:23:49.904000Z',\n", + " 'url': 'https://swapi.dev/api/starships/27/'},\n", + " {'name': 'A-wing',\n", + " 'model': 'RZ-1 A-wing Interceptor',\n", + " 'manufacturer': 'Alliance Underground Engineering, Incom Corporation',\n", + " 'cost_in_credits': '175000',\n", + " 'length': '9.6',\n", + " 'max_atmosphering_speed': '1300',\n", + " 'crew': '1',\n", + " 'passengers': '0',\n", + " 'cargo_capacity': '40',\n", + " 'consumables': '1 week',\n", + " 'hyperdrive_rating': '1.0',\n", + " 'MGLT': '120',\n", + " 'starship_class': 'Starfighter',\n", + " 'pilots': ['https://swapi.dev/api/people/29/'],\n", + " 'films': ['https://swapi.dev/api/films/3/'],\n", + " 'created': '2014-12-18T11:16:34.542000Z',\n", + " 'edited': '2014-12-20T21:23:49.907000Z',\n", + " 'url': 'https://swapi.dev/api/starships/28/'},\n", + " {'name': 'B-wing',\n", + " 'model': 'A/SF-01 B-wing starfighter',\n", + " 'manufacturer': 'Slayn & Korpil',\n", + " 'cost_in_credits': '220000',\n", + " 'length': '16.9',\n", + " 'max_atmosphering_speed': '950',\n", + " 'crew': '1',\n", + " 'passengers': '0',\n", + " 'cargo_capacity': '45',\n", + " 'consumables': '1 week',\n", + " 'hyperdrive_rating': '2.0',\n", + " 'MGLT': '91',\n", + " 'starship_class': 'Assault Starfighter',\n", + " 'pilots': [],\n", + " 'films': ['https://swapi.dev/api/films/3/'],\n", + " 'created': '2014-12-18T11:18:04.763000Z',\n", + " 'edited': '2014-12-20T21:23:49.909000Z',\n", + " 'url': 'https://swapi.dev/api/starships/29/'},\n", + " {'name': 'Republic Cruiser',\n", + " 'model': 'Consular-class cruiser',\n", + " 'manufacturer': 'Corellian Engineering Corporation',\n", + " 'cost_in_credits': 'unknown',\n", + " 'length': '115',\n", + " 'max_atmosphering_speed': '900',\n", + " 'crew': '9',\n", + " 'passengers': '16',\n", + " 'cargo_capacity': 'unknown',\n", + " 'consumables': 'unknown',\n", + " 'hyperdrive_rating': '2.0',\n", + " 'MGLT': 'unknown',\n", + " 'starship_class': 'Space cruiser',\n", + " 'pilots': [],\n", + " 'films': ['https://swapi.dev/api/films/4/'],\n", + " 'created': '2014-12-19T17:01:31.488000Z',\n", + " 'edited': '2014-12-20T21:23:49.912000Z',\n", + " 'url': 'https://swapi.dev/api/starships/31/'},\n", + " {'name': 'Droid control ship',\n", + " 'model': 'Lucrehulk-class Droid Control Ship',\n", + " 'manufacturer': 'Hoersch-Kessel Drive, Inc.',\n", + " 'cost_in_credits': 'unknown',\n", + " 'length': '3170',\n", + " 'max_atmosphering_speed': 'n/a',\n", + " 'crew': '175',\n", + " 'passengers': '139000',\n", + " 'cargo_capacity': '4000000000',\n", + " 'consumables': '500 days',\n", + " 'hyperdrive_rating': '2.0',\n", + " 'MGLT': 'unknown',\n", + " 'starship_class': 'Droid control ship',\n", + " 'pilots': [],\n", + " 'films': ['https://swapi.dev/api/films/4/',\n", + " 'https://swapi.dev/api/films/5/',\n", + " 'https://swapi.dev/api/films/6/'],\n", + " 'created': '2014-12-19T17:04:06.323000Z',\n", + " 'edited': '2014-12-20T21:23:49.915000Z',\n", + " 'url': 'https://swapi.dev/api/starships/32/'},\n", + " {'name': 'Naboo fighter',\n", + " 'model': 'N-1 starfighter',\n", + " 'manufacturer': 'Theed Palace Space Vessel Engineering Corps',\n", + " 'cost_in_credits': '200000',\n", + " 'length': '11',\n", + " 'max_atmosphering_speed': '1100',\n", + " 'crew': '1',\n", + " 'passengers': '0',\n", + " 'cargo_capacity': '65',\n", + " 'consumables': '7 days',\n", + " 'hyperdrive_rating': '1.0',\n", + " 'MGLT': 'unknown',\n", + " 'starship_class': 'Starfighter',\n", + " 'pilots': ['https://swapi.dev/api/people/11/',\n", + " 'https://swapi.dev/api/people/35/',\n", + " 'https://swapi.dev/api/people/60/'],\n", + " 'films': ['https://swapi.dev/api/films/4/', 'https://swapi.dev/api/films/5/'],\n", + " 'created': '2014-12-19T17:39:17.582000Z',\n", + " 'edited': '2014-12-20T21:23:49.917000Z',\n", + " 'url': 'https://swapi.dev/api/starships/39/'},\n", + " {'name': 'Naboo Royal Starship',\n", + " 'model': 'J-type 327 Nubian royal starship',\n", + " 'manufacturer': 'Theed Palace Space Vessel Engineering Corps, Nubia Star '\n", + " 'Drives',\n", + " 'cost_in_credits': 'unknown',\n", + " 'length': '76',\n", + " 'max_atmosphering_speed': '920',\n", + " 'crew': '8',\n", + " 'passengers': 'unknown',\n", + " 'cargo_capacity': 'unknown',\n", + " 'consumables': 'unknown',\n", + " 'hyperdrive_rating': '1.8',\n", + " 'MGLT': 'unknown',\n", + " 'starship_class': 'yacht',\n", + " 'pilots': ['https://swapi.dev/api/people/39/'],\n", + " 'films': ['https://swapi.dev/api/films/4/'],\n", + " 'created': '2014-12-19T17:45:03.506000Z',\n", + " 'edited': '2014-12-20T21:23:49.919000Z',\n", + " 'url': 'https://swapi.dev/api/starships/40/'},\n", + " {'name': 'Scimitar',\n", + " 'model': 'Star Courier',\n", + " 'manufacturer': 'Republic Sienar Systems',\n", + " 'cost_in_credits': '55000000',\n", + " 'length': '26.5',\n", + " 'max_atmosphering_speed': '1180',\n", + " 'crew': '1',\n", + " 'passengers': '6',\n", + " 'cargo_capacity': '2500000',\n", + " 'consumables': '30 days',\n", + " 'hyperdrive_rating': '1.5',\n", + " 'MGLT': 'unknown',\n", + " 'starship_class': 'Space Transport',\n", + " 'pilots': ['https://swapi.dev/api/people/44/'],\n", + " 'films': ['https://swapi.dev/api/films/4/'],\n", + " 'created': '2014-12-20T09:39:56.116000Z',\n", + " 'edited': '2014-12-20T21:23:49.922000Z',\n", + " 'url': 'https://swapi.dev/api/starships/41/'},\n", + " {'name': 'J-type diplomatic barge',\n", + " 'model': 'J-type diplomatic barge',\n", + " 'manufacturer': 'Theed Palace Space Vessel Engineering Corps, Nubia Star '\n", + " 'Drives',\n", + " 'cost_in_credits': '2000000',\n", + " 'length': '39',\n", + " 'max_atmosphering_speed': '2000',\n", + " 'crew': '5',\n", + " 'passengers': '10',\n", + " 'cargo_capacity': 'unknown',\n", + " 'consumables': '1 year',\n", + " 'hyperdrive_rating': '0.7',\n", + " 'MGLT': 'unknown',\n", + " 'starship_class': 'Diplomatic barge',\n", + " 'pilots': [],\n", + " 'films': ['https://swapi.dev/api/films/5/'],\n", + " 'created': '2014-12-20T11:05:51.237000Z',\n", + " 'edited': '2014-12-20T21:23:49.925000Z',\n", + " 'url': 'https://swapi.dev/api/starships/43/'},\n", + " {'name': 'AA-9 Coruscant freighter',\n", + " 'model': 'Botajef AA-9 Freighter-Liner',\n", + " 'manufacturer': 'Botajef Shipyards',\n", + " 'cost_in_credits': 'unknown',\n", + " 'length': '390',\n", + " 'max_atmosphering_speed': 'unknown',\n", + " 'crew': 'unknown',\n", + " 'passengers': '30000',\n", + " 'cargo_capacity': 'unknown',\n", + " 'consumables': 'unknown',\n", + " 'hyperdrive_rating': 'unknown',\n", + " 'MGLT': 'unknown',\n", + " 'starship_class': 'freighter',\n", + " 'pilots': [],\n", + " 'films': ['https://swapi.dev/api/films/5/'],\n", + " 'created': '2014-12-20T17:24:23.509000Z',\n", + " 'edited': '2014-12-20T21:23:49.928000Z',\n", + " 'url': 'https://swapi.dev/api/starships/47/'},\n", + " {'name': 'Jedi starfighter',\n", + " 'model': 'Delta-7 Aethersprite-class interceptor',\n", + " 'manufacturer': 'Kuat Systems Engineering',\n", + " 'cost_in_credits': '180000',\n", + " 'length': '8',\n", + " 'max_atmosphering_speed': '1150',\n", + " 'crew': '1',\n", + " 'passengers': '0',\n", + " 'cargo_capacity': '60',\n", + " 'consumables': '7 days',\n", + " 'hyperdrive_rating': '1.0',\n", + " 'MGLT': 'unknown',\n", + " 'starship_class': 'Starfighter',\n", + " 'pilots': ['https://swapi.dev/api/people/10/',\n", + " 'https://swapi.dev/api/people/58/'],\n", + " 'films': ['https://swapi.dev/api/films/5/', 'https://swapi.dev/api/films/6/'],\n", + " 'created': '2014-12-20T17:35:23.906000Z',\n", + " 'edited': '2014-12-20T21:23:49.930000Z',\n", + " 'url': 'https://swapi.dev/api/starships/48/'},\n", + " {'name': 'H-type Nubian yacht',\n", + " 'model': 'H-type Nubian yacht',\n", + " 'manufacturer': 'Theed Palace Space Vessel Engineering Corps',\n", + " 'cost_in_credits': 'unknown',\n", + " 'length': '47.9',\n", + " 'max_atmosphering_speed': '8000',\n", + " 'crew': '4',\n", + " 'passengers': 'unknown',\n", + " 'cargo_capacity': 'unknown',\n", + " 'consumables': 'unknown',\n", + " 'hyperdrive_rating': '0.9',\n", + " 'MGLT': 'unknown',\n", + " 'starship_class': 'yacht',\n", + " 'pilots': ['https://swapi.dev/api/people/35/'],\n", + " 'films': ['https://swapi.dev/api/films/5/'],\n", + " 'created': '2014-12-20T17:46:46.847000Z',\n", + " 'edited': '2014-12-20T21:23:49.932000Z',\n", + " 'url': 'https://swapi.dev/api/starships/49/'},\n", + " {'name': 'Republic Assault ship',\n", + " 'model': 'Acclamator I-class assault ship',\n", + " 'manufacturer': 'Rothana Heavy Engineering',\n", + " 'cost_in_credits': 'unknown',\n", + " 'length': '752',\n", + " 'max_atmosphering_speed': 'unknown',\n", + " 'crew': '700',\n", + " 'passengers': '16000',\n", + " 'cargo_capacity': '11250000',\n", + " 'consumables': '2 years',\n", + " 'hyperdrive_rating': '0.6',\n", + " 'MGLT': 'unknown',\n", + " 'starship_class': 'assault ship',\n", + " 'pilots': [],\n", + " 'films': ['https://swapi.dev/api/films/5/'],\n", + " 'created': '2014-12-20T18:08:42.926000Z',\n", + " 'edited': '2014-12-20T21:23:49.935000Z',\n", + " 'url': 'https://swapi.dev/api/starships/52/'},\n", + " {'name': 'Solar Sailer',\n", + " 'model': 'Punworcca 116-class interstellar sloop',\n", + " 'manufacturer': 'Huppla Pasa Tisc Shipwrights Collective',\n", + " 'cost_in_credits': '35700',\n", + " 'length': '15.2',\n", + " 'max_atmosphering_speed': '1600',\n", + " 'crew': '3',\n", + " 'passengers': '11',\n", + " 'cargo_capacity': '240',\n", + " 'consumables': '7 days',\n", + " 'hyperdrive_rating': '1.5',\n", + " 'MGLT': 'unknown',\n", + " 'starship_class': 'yacht',\n", + " 'pilots': [],\n", + " 'films': ['https://swapi.dev/api/films/5/'],\n", + " 'created': '2014-12-20T18:37:56.969000Z',\n", + " 'edited': '2014-12-20T21:23:49.937000Z',\n", + " 'url': 'https://swapi.dev/api/starships/58/'},\n", + " {'name': 'Trade Federation cruiser',\n", + " 'model': 'Providence-class carrier/destroyer',\n", + " 'manufacturer': 'Rendili StarDrive, Free Dac Volunteers Engineering corps.',\n", + " 'cost_in_credits': '125000000',\n", + " 'length': '1088',\n", + " 'max_atmosphering_speed': '1050',\n", + " 'crew': '600',\n", + " 'passengers': '48247',\n", + " 'cargo_capacity': '50000000',\n", + " 'consumables': '4 years',\n", + " 'hyperdrive_rating': '1.5',\n", + " 'MGLT': 'unknown',\n", + " 'starship_class': 'capital ship',\n", + " 'pilots': ['https://swapi.dev/api/people/10/',\n", + " 'https://swapi.dev/api/people/11/'],\n", + " 'films': ['https://swapi.dev/api/films/6/'],\n", + " 'created': '2014-12-20T19:40:21.902000Z',\n", + " 'edited': '2014-12-20T21:23:49.941000Z',\n", + " 'url': 'https://swapi.dev/api/starships/59/'},\n", + " {'name': 'Theta-class T-2c shuttle',\n", + " 'model': 'Theta-class T-2c shuttle',\n", + " 'manufacturer': 'Cygnus Spaceworks',\n", + " 'cost_in_credits': '1000000',\n", + " 'length': '18.5',\n", + " 'max_atmosphering_speed': '2000',\n", + " 'crew': '5',\n", + " 'passengers': '16',\n", + " 'cargo_capacity': '50000',\n", + " 'consumables': '56 days',\n", + " 'hyperdrive_rating': '1.0',\n", + " 'MGLT': 'unknown',\n", + " 'starship_class': 'transport',\n", + " 'pilots': [],\n", + " 'films': ['https://swapi.dev/api/films/6/'],\n", + " 'created': '2014-12-20T19:48:40.409000Z',\n", + " 'edited': '2014-12-20T21:23:49.944000Z',\n", + " 'url': 'https://swapi.dev/api/starships/61/'},\n", + " {'name': 'Republic attack cruiser',\n", + " 'model': 'Senator-class Star Destroyer',\n", + " 'manufacturer': 'Kuat Drive Yards, Allanteen Six shipyards',\n", + " 'cost_in_credits': '59000000',\n", + " 'length': '1137',\n", + " 'max_atmosphering_speed': '975',\n", + " 'crew': '7400',\n", + " 'passengers': '2000',\n", + " 'cargo_capacity': '20000000',\n", + " 'consumables': '2 years',\n", + " 'hyperdrive_rating': '1.0',\n", + " 'MGLT': 'unknown',\n", + " 'starship_class': 'star destroyer',\n", + " 'pilots': [],\n", + " 'films': ['https://swapi.dev/api/films/6/'],\n", + " 'created': '2014-12-20T19:52:56.232000Z',\n", + " 'edited': '2014-12-20T21:23:49.946000Z',\n", + " 'url': 'https://swapi.dev/api/starships/63/'}]\n" + ] + } + ], + "source": [ + "result_list = []\n", + "model_names = []\n", + "for a in new_list:\n", + " for b in a['results']:\n", + " result_list.append(b)\n", + "pp(result_list)\n", + "\n", + "for c in result_list:\n", + " for x in c['name']:\n", + " model_names.append(x)\n", + "#print(model_names)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "CR90 corvette\n", + "Star Destroyer\n", + "Sentinel-class landing craft\n", + "Death Star\n", + "Millennium Falcon\n", + "Y-wing\n", + "X-wing\n", + "TIE Advanced x1\n", + "Executor\n", + "Rebel transport\n", + "Slave 1\n", + "Imperial shuttle\n", + "EF76 Nebulon-B escort frigate\n", + "Calamari Cruiser\n", + "A-wing\n", + "B-wing\n", + "Republic Cruiser\n", + "Droid control ship\n", + "Naboo fighter\n", + "Naboo Royal Starship\n", + "Scimitar\n", + "J-type diplomatic barge\n", + "AA-9 Coruscant freighter\n", + "Jedi starfighter\n", + "H-type Nubian yacht\n", + "Republic Assault ship\n", + "Solar Sailer\n", + "Trade Federation cruiser\n", + "Theta-class T-2c shuttle\n", + "Republic attack cruiser\n" + ] + } + ], + "source": [ + "model_names = []\n", + "for c in result_list:\n", + " model_names.append(c['name'])\n", + "#print(model_names)\n", + "for i in model_names:\n", + " print(i)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'result_list' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[4], line 14\u001b[0m\n\u001b[0;32m 11\u001b[0m cargoship_list\u001b[39m.\u001b[39mappend(e[\u001b[39m'\u001b[39m\u001b[39mname\u001b[39m\u001b[39m'\u001b[39m]) \n\u001b[0;32m 12\u001b[0m \u001b[39mreturn\u001b[39;00m cargoship_list\n\u001b[1;32m---> 14\u001b[0m \u001b[39mprint\u001b[39m(most_cargo())\n", + "Cell \u001b[1;32mIn[4], line 4\u001b[0m, in \u001b[0;36mmost_cargo\u001b[1;34m()\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mmost_cargo\u001b[39m():\n\u001b[1;32m----> 4\u001b[0m \u001b[39mfor\u001b[39;00m d \u001b[39min\u001b[39;00m result_list:\n\u001b[0;32m 5\u001b[0m \u001b[39mif\u001b[39;00m d[\u001b[39m'\u001b[39m\u001b[39mcargo_capacity\u001b[39m\u001b[39m'\u001b[39m]\u001b[39m.\u001b[39misnumeric(): \n\u001b[0;32m 6\u001b[0m cargo_list\u001b[39m.\u001b[39mappend(\u001b[39mint\u001b[39m((d[\u001b[39m'\u001b[39m\u001b[39mcargo_capacity\u001b[39m\u001b[39m'\u001b[39m])))\n", + "\u001b[1;31mNameError\u001b[0m: name 'result_list' is not defined" + ] + } + ], + "source": [ + "cargo_list = []\n", + "cargoship_list=[]\n", + "def most_cargo():\n", + " for d in result_list:\n", + " if d['cargo_capacity'].isnumeric(): \n", + " cargo_list.append(int((d['cargo_capacity'])))\n", + " #print(cargo_list)\n", + "\n", + " for e in result_list:\n", + " if (e['cargo_capacity'].isnumeric() and int(e['cargo_capacity']) == max(cargo_list)):\n", + " cargoship_list.append(e['name']) \n", + " return cargoship_list\n", + " \n", + "print(most_cargo())\n" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "https://swapi.dev/api/films/3/\n" + ] + } + ], + "source": [ + "print(new_list[1]['results'][1]['films'][1])\n" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[150, 38, 120000, 14, 19000, 90, 20, 300, 1200, 115, 3170, 11, 76, 39, 390, 8, 752, 1088, 1137]\n", + "120000\n", + "8\n", + "Death Star\n", + "Jedi starfighter\n" + ] + } + ], + "source": [ + "length_list = []\n", + "for i in result_list:\n", + " if i['length'].isnumeric():\n", + " length_list.append(int(i['length']))\n", + "print(length_list)\n", + "print(max(length_list))\n", + "print(min(length_list))\n", + "\n", + "for e in result_list:\n", + " if (e['length'].isnumeric() and (int(e['length']) == max(length_list) or int(e['length']) == min(length_list))):\n", + " print(e['name'])\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "starship_list: ['Executor', 'Rebel transport', 'Slave 1', 'Imperial shuttle', 'EF76 Nebulon-B escort frigate', 'Naboo fighter', 'Jedi starfighter']\n", + "number of starships: 7\n" + ] + } + ], + "source": [ + "film_list = []\n", + "x = 2\n", + "starship_list = []\n", + "for i in result_list:\n", + " if len(i['films']) == x:\n", + " starship_list.append(i['name'])\n", + " \n", + "print('starship_list:', starship_list)\n", + "print('number of starships:', len(starship_list))\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)]" + }, + "orig_nbformat": 4, + "vscode": { + "interpreter": { + "hash": "8b70a4ea831fd5b9b770fab33cac3f0a285469b7491720cba6f6d634f19e8405" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/avg_list.py b/avg_list.py new file mode 100644 index 0000000..1199b9a --- /dev/null +++ b/avg_list.py @@ -0,0 +1,11 @@ + +#Average - Create a function that takes a list as an argument and returns the average of all the values in the list. For example multiples([1,2,3,4]) should return #2.5 +def avglist(lista): + sumvalue = 0 + avgvalue = 0 + for i in lista: + sumvalue = sumvalue + i + avgvalue = sumvalue/len(lista) + return avgvalue + +avg1 = print(avglist([1,2,3,4])) \ No newline at end of file diff --git a/closures.py b/closures.py new file mode 100644 index 0000000..9ecdab5 --- /dev/null +++ b/closures.py @@ -0,0 +1,59 @@ +def outer(msg): + lang = 'Python' + print(locals()) + def inner(): + print(lang, msg) + inner() + +outer('is fun!!!') + + +#Want a closure that take 2 numbers and sum +#def sum(num1): + +def make_multiplier_of(n): + def multiply(k): + return k * n + return multiply + +multiplier3 = make_multiplier_of(3) +print(multiplier3(5)) + +#def dec(func, n): +# def wrapper(): +# for _ in range(n): +# func() +# return wrapper + +#@dec.wraps(func, 5) +#def say_hello(): +# print('hello') + +#say_hello = dec(say_hello, 5) +#say_hello() + +def make_multiplier_of(n): + def multiply(k): + return k * n + return multiply + +m3_under30 = make_multiplier_of(3) +print(type(m3_under30)) + + +def make_multiplier_of(n): + def multiply(*k): + for x in range(k): + return x * n + return multiply + +multiplier3 = make_multiplier_of(5) +print(*multiplier3) + +def multiplier(*x): + return x * 2 + +print(multiplier(3,4)) + + + diff --git a/fibchk.py b/fibchk.py new file mode 100644 index 0000000..2483a22 --- /dev/null +++ b/fibchk.py @@ -0,0 +1,19 @@ + +#Fibonacci- The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, #starting from 0 and 1. That is, + #F(0) = 0, F(1) = 1 + #F(n) = F(n - 1) + F(n - 2), for n > 1. + #Create a function that accepts any number and will create a sequence based on the fibonacci sequence. +def fibchk(n): + n1 = 0 + n2 = 1 + sum = 0 + for i in range(n): + sum = n1 + n2 + n1 = n2 + n2 = sum + i+=1 + print(sum) + + +fibchk(50) + diff --git a/fizz_buzz.py b/fizz_buzz.py new file mode 100644 index 0000000..d72b4af --- /dev/null +++ b/fizz_buzz.py @@ -0,0 +1,23 @@ + +#Fizzbuzz- Create a function that will print numbers from 1 to 100, with certain exceptions: + #If the number is a multiple of 3, print “Fizz” instead of the number. + #If the number is a multiple of 5, print “Buzz” instead of the number. + #If the number is a multiple of 3 and 5, print “FizzBuzz” instead of the number. +def fizzbuzz(x): + i =1 + for i in range(x+1): + if i%3 == 0 and i%5 != 0: + i = 'Fizz' + print(i) + elif (i%5 == 0 and i%3 != 0): + i = 'Buzz' + print(i) + elif i%3 == 0 and i%5 == 0: + i = 'FizzBuzz' + print(i) + else: + i = i + print(i) + +fizzbuzz(100) + \ No newline at end of file diff --git a/hackerankJSON.ipynb b/hackerankJSON.ipynb new file mode 100644 index 0000000..9dabb09 --- /dev/null +++ b/hackerankJSON.ipynb @@ -0,0 +1,580 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "import requests\n", + "import json\n", + "from pprint import pp" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'count': 82, 'next': 'https://swapi.dev/api/people/?page=2&page+=+1', 'previous': None, 'results': [{'name': 'Luke Skywalker', 'height': '172', 'mass': '77', 'hair_color': 'blond', 'skin_color': 'fair', 'eye_color': 'blue', 'birth_year': '19BBY', 'gender': 'male', 'homeworld': 'https://swapi.dev/api/planets/1/', 'films': ['https://swapi.dev/api/films/1/', 'https://swapi.dev/api/films/2/', 'https://swapi.dev/api/films/3/', 'https://swapi.dev/api/films/6/'], 'species': [], 'vehicles': ['https://swapi.dev/api/vehicles/14/', 'https://swapi.dev/api/vehicles/30/'], 'starships': ['https://swapi.dev/api/starships/12/', 'https://swapi.dev/api/starships/22/'], 'created': '2014-12-09T13:50:51.644000Z', 'edited': '2014-12-20T21:17:56.891000Z', 'url': 'https://swapi.dev/api/people/1/'}, {'name': 'C-3PO', 'height': '167', 'mass': '75', 'hair_color': 'n/a', 'skin_color': 'gold', 'eye_color': 'yellow', 'birth_year': '112BBY', 'gender': 'n/a', 'homeworld': 'https://swapi.dev/api/planets/1/', 'films': ['https://swapi.dev/api/films/1/', 'https://swapi.dev/api/films/2/', 'https://swapi.dev/api/films/3/', 'https://swapi.dev/api/films/4/', 'https://swapi.dev/api/films/5/', 'https://swapi.dev/api/films/6/'], 'species': ['https://swapi.dev/api/species/2/'], 'vehicles': [], 'starships': [], 'created': '2014-12-10T15:10:51.357000Z', 'edited': '2014-12-20T21:17:50.309000Z', 'url': 'https://swapi.dev/api/people/2/'}, {'name': 'R2-D2', 'height': '96', 'mass': '32', 'hair_color': 'n/a', 'skin_color': 'white, blue', 'eye_color': 'red', 'birth_year': '33BBY', 'gender': 'n/a', 'homeworld': 'https://swapi.dev/api/planets/8/', 'films': ['https://swapi.dev/api/films/1/', 'https://swapi.dev/api/films/2/', 'https://swapi.dev/api/films/3/', 'https://swapi.dev/api/films/4/', 'https://swapi.dev/api/films/5/', 'https://swapi.dev/api/films/6/'], 'species': ['https://swapi.dev/api/species/2/'], 'vehicles': [], 'starships': [], 'created': '2014-12-10T15:11:50.376000Z', 'edited': '2014-12-20T21:17:50.311000Z', 'url': 'https://swapi.dev/api/people/3/'}, {'name': 'Darth Vader', 'height': '202', 'mass': '136', 'hair_color': 'none', 'skin_color': 'white', 'eye_color': 'yellow', 'birth_year': '41.9BBY', 'gender': 'male', 'homeworld': 'https://swapi.dev/api/planets/1/', 'films': ['https://swapi.dev/api/films/1/', 'https://swapi.dev/api/films/2/', 'https://swapi.dev/api/films/3/', 'https://swapi.dev/api/films/6/'], 'species': [], 'vehicles': [], 'starships': ['https://swapi.dev/api/starships/13/'], 'created': '2014-12-10T15:18:20.704000Z', 'edited': '2014-12-20T21:17:50.313000Z', 'url': 'https://swapi.dev/api/people/4/'}, {'name': 'Leia Organa', 'height': '150', 'mass': '49', 'hair_color': 'brown', 'skin_color': 'light', 'eye_color': 'brown', 'birth_year': '19BBY', 'gender': 'female', 'homeworld': 'https://swapi.dev/api/planets/2/', 'films': ['https://swapi.dev/api/films/1/', 'https://swapi.dev/api/films/2/', 'https://swapi.dev/api/films/3/', 'https://swapi.dev/api/films/6/'], 'species': [], 'vehicles': ['https://swapi.dev/api/vehicles/30/'], 'starships': [], 'created': '2014-12-10T15:20:09.791000Z', 'edited': '2014-12-20T21:17:50.315000Z', 'url': 'https://swapi.dev/api/people/5/'}, {'name': 'Owen Lars', 'height': '178', 'mass': '120', 'hair_color': 'brown, grey', 'skin_color': 'light', 'eye_color': 'blue', 'birth_year': '52BBY', 'gender': 'male', 'homeworld': 'https://swapi.dev/api/planets/1/', 'films': ['https://swapi.dev/api/films/1/', 'https://swapi.dev/api/films/5/', 'https://swapi.dev/api/films/6/'], 'species': [], 'vehicles': [], 'starships': [], 'created': '2014-12-10T15:52:14.024000Z', 'edited': '2014-12-20T21:17:50.317000Z', 'url': 'https://swapi.dev/api/people/6/'}, {'name': 'Beru Whitesun lars', 'height': '165', 'mass': '75', 'hair_color': 'brown', 'skin_color': 'light', 'eye_color': 'blue', 'birth_year': '47BBY', 'gender': 'female', 'homeworld': 'https://swapi.dev/api/planets/1/', 'films': ['https://swapi.dev/api/films/1/', 'https://swapi.dev/api/films/5/', 'https://swapi.dev/api/films/6/'], 'species': [], 'vehicles': [], 'starships': [], 'created': '2014-12-10T15:53:41.121000Z', 'edited': '2014-12-20T21:17:50.319000Z', 'url': 'https://swapi.dev/api/people/7/'}, {'name': 'R5-D4', 'height': '97', 'mass': '32', 'hair_color': 'n/a', 'skin_color': 'white, red', 'eye_color': 'red', 'birth_year': 'unknown', 'gender': 'n/a', 'homeworld': 'https://swapi.dev/api/planets/1/', 'films': ['https://swapi.dev/api/films/1/'], 'species': ['https://swapi.dev/api/species/2/'], 'vehicles': [], 'starships': [], 'created': '2014-12-10T15:57:50.959000Z', 'edited': '2014-12-20T21:17:50.321000Z', 'url': 'https://swapi.dev/api/people/8/'}, {'name': 'Biggs Darklighter', 'height': '183', 'mass': '84', 'hair_color': 'black', 'skin_color': 'light', 'eye_color': 'brown', 'birth_year': '24BBY', 'gender': 'male', 'homeworld': 'https://swapi.dev/api/planets/1/', 'films': ['https://swapi.dev/api/films/1/'], 'species': [], 'vehicles': [], 'starships': ['https://swapi.dev/api/starships/12/'], 'created': '2014-12-10T15:59:50.509000Z', 'edited': '2014-12-20T21:17:50.323000Z', 'url': 'https://swapi.dev/api/people/9/'}, {'name': 'Obi-Wan Kenobi', 'height': '182', 'mass': '77', 'hair_color': 'auburn, white', 'skin_color': 'fair', 'eye_color': 'blue-gray', 'birth_year': '57BBY', 'gender': 'male', 'homeworld': 'https://swapi.dev/api/planets/20/', 'films': ['https://swapi.dev/api/films/1/', 'https://swapi.dev/api/films/2/', 'https://swapi.dev/api/films/3/', 'https://swapi.dev/api/films/4/', 'https://swapi.dev/api/films/5/', 'https://swapi.dev/api/films/6/'], 'species': [], 'vehicles': ['https://swapi.dev/api/vehicles/38/'], 'starships': ['https://swapi.dev/api/starships/48/', 'https://swapi.dev/api/starships/59/', 'https://swapi.dev/api/starships/64/', 'https://swapi.dev/api/starships/65/', 'https://swapi.dev/api/starships/74/'], 'created': '2014-12-10T16:16:29.192000Z', 'edited': '2014-12-20T21:17:50.325000Z', 'url': 'https://swapi.dev/api/people/10/'}]}\n" + ] + } + ], + "source": [ + "url = 'https://swapi.dev/api/people/?page = 1'\n", + "response1 = requests.get(url)\n", + "r = response1.json()\n", + "print(r)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "[{'count': 82,\n", + " 'next': 'https://swapi.dev/api/people/?page=2',\n", + " 'previous': None,\n", + " 'results': [{'name': 'Luke Skywalker',\n", + " 'height': '172',\n", + " 'mass': '77',\n", + " 'hair_color': 'blond',\n", + " 'skin_color': 'fair',\n", + " 'eye_color': 'blue',\n", + " 'birth_year': '19BBY',\n", + " 'gender': 'male',\n", + " 'homeworld': 'https://swapi.dev/api/planets/1/',\n", + " 'films': ['https://swapi.dev/api/films/1/',\n", + " 'https://swapi.dev/api/films/2/',\n", + " 'https://swapi.dev/api/films/3/',\n", + " 'https://swapi.dev/api/films/6/'],\n", + " 'species': [],\n", + " 'vehicles': ['https://swapi.dev/api/vehicles/14/',\n", + " 'https://swapi.dev/api/vehicles/30/'],\n", + " 'starships': ['https://swapi.dev/api/starships/12/',\n", + " 'https://swapi.dev/api/starships/22/'],\n", + " 'created': '2014-12-09T13:50:51.644000Z',\n", + " 'edited': '2014-12-20T21:17:56.891000Z',\n", + " 'url': 'https://swapi.dev/api/people/1/'},\n", + " {'name': 'C-3PO',\n", + " 'height': '167',\n", + " 'mass': '75',\n", + " 'hair_color': 'n/a',\n", + " 'skin_color': 'gold',\n", + " 'eye_color': 'yellow',\n", + " 'birth_year': '112BBY',\n", + " 'gender': 'n/a',\n", + " 'homeworld': 'https://swapi.dev/api/planets/1/',\n", + " 'films': ['https://swapi.dev/api/films/1/',\n", + " 'https://swapi.dev/api/films/2/',\n", + " 'https://swapi.dev/api/films/3/',\n", + " 'https://swapi.dev/api/films/4/',\n", + " 'https://swapi.dev/api/films/5/',\n", + " 'https://swapi.dev/api/films/6/'],\n", + " 'species': ['https://swapi.dev/api/species/2/'],\n", + " 'vehicles': [],\n", + " 'starships': [],\n", + " 'created': '2014-12-10T15:10:51.357000Z',\n", + " 'edited': '2014-12-20T21:17:50.309000Z',\n", + " 'url': 'https://swapi.dev/api/people/2/'},\n", + " {'name': 'R2-D2',\n", + " 'height': '96',\n", + " 'mass': '32',\n", + " 'hair_color': 'n/a',\n", + " 'skin_color': 'white, blue',\n", + " 'eye_color': 'red',\n", + " 'birth_year': '33BBY',\n", + " 'gender': 'n/a',\n", + " 'homeworld': 'https://swapi.dev/api/planets/8/',\n", + " 'films': ['https://swapi.dev/api/films/1/',\n", + " 'https://swapi.dev/api/films/2/',\n", + " 'https://swapi.dev/api/films/3/',\n", + " 'https://swapi.dev/api/films/4/',\n", + " 'https://swapi.dev/api/films/5/',\n", + " 'https://swapi.dev/api/films/6/'],\n", + " 'species': ['https://swapi.dev/api/species/2/'],\n", + " 'vehicles': [],\n", + " 'starships': [],\n", + " 'created': '2014-12-10T15:11:50.376000Z',\n", + " 'edited': '2014-12-20T21:17:50.311000Z',\n", + " 'url': 'https://swapi.dev/api/people/3/'},\n", + " {'name': 'Darth Vader',\n", + " 'height': '202',\n", + " 'mass': '136',\n", + " 'hair_color': 'none',\n", + " 'skin_color': 'white',\n", + " 'eye_color': 'yellow',\n", + " 'birth_year': '41.9BBY',\n", + " 'gender': 'male',\n", + " 'homeworld': 'https://swapi.dev/api/planets/1/',\n", + " 'films': ['https://swapi.dev/api/films/1/',\n", + " 'https://swapi.dev/api/films/2/',\n", + " 'https://swapi.dev/api/films/3/',\n", + " 'https://swapi.dev/api/films/6/'],\n", + " 'species': [],\n", + " 'vehicles': [],\n", + " 'starships': ['https://swapi.dev/api/starships/13/'],\n", + " 'created': '2014-12-10T15:18:20.704000Z',\n", + " 'edited': '2014-12-20T21:17:50.313000Z',\n", + " 'url': 'https://swapi.dev/api/people/4/'},\n", + " {'name': 'Leia Organa',\n", + " 'height': '150',\n", + " 'mass': '49',\n", + " 'hair_color': 'brown',\n", + " 'skin_color': 'light',\n", + " 'eye_color': 'brown',\n", + " 'birth_year': '19BBY',\n", + " 'gender': 'female',\n", + " 'homeworld': 'https://swapi.dev/api/planets/2/',\n", + " 'films': ['https://swapi.dev/api/films/1/',\n", + " 'https://swapi.dev/api/films/2/',\n", + " 'https://swapi.dev/api/films/3/',\n", + " 'https://swapi.dev/api/films/6/'],\n", + " 'species': [],\n", + " 'vehicles': ['https://swapi.dev/api/vehicles/30/'],\n", + " 'starships': [],\n", + " 'created': '2014-12-10T15:20:09.791000Z',\n", + " 'edited': '2014-12-20T21:17:50.315000Z',\n", + " 'url': 'https://swapi.dev/api/people/5/'},\n", + " {'name': 'Owen Lars',\n", + " 'height': '178',\n", + " 'mass': '120',\n", + " 'hair_color': 'brown, grey',\n", + " 'skin_color': 'light',\n", + " 'eye_color': 'blue',\n", + " 'birth_year': '52BBY',\n", + " 'gender': 'male',\n", + " 'homeworld': 'https://swapi.dev/api/planets/1/',\n", + " 'films': ['https://swapi.dev/api/films/1/',\n", + " 'https://swapi.dev/api/films/5/',\n", + " 'https://swapi.dev/api/films/6/'],\n", + " 'species': [],\n", + " 'vehicles': [],\n", + " 'starships': [],\n", + " 'created': '2014-12-10T15:52:14.024000Z',\n", + " 'edited': '2014-12-20T21:17:50.317000Z',\n", + " 'url': 'https://swapi.dev/api/people/6/'},\n", + " {'name': 'Beru Whitesun lars',\n", + " 'height': '165',\n", + " 'mass': '75',\n", + " 'hair_color': 'brown',\n", + " 'skin_color': 'light',\n", + " 'eye_color': 'blue',\n", + " 'birth_year': '47BBY',\n", + " 'gender': 'female',\n", + " 'homeworld': 'https://swapi.dev/api/planets/1/',\n", + " 'films': ['https://swapi.dev/api/films/1/',\n", + " 'https://swapi.dev/api/films/5/',\n", + " 'https://swapi.dev/api/films/6/'],\n", + " 'species': [],\n", + " 'vehicles': [],\n", + " 'starships': [],\n", + " 'created': '2014-12-10T15:53:41.121000Z',\n", + " 'edited': '2014-12-20T21:17:50.319000Z',\n", + " 'url': 'https://swapi.dev/api/people/7/'},\n", + " {'name': 'R5-D4',\n", + " 'height': '97',\n", + " 'mass': '32',\n", + " 'hair_color': 'n/a',\n", + " 'skin_color': 'white, red',\n", + " 'eye_color': 'red',\n", + " 'birth_year': 'unknown',\n", + " 'gender': 'n/a',\n", + " 'homeworld': 'https://swapi.dev/api/planets/1/',\n", + " 'films': ['https://swapi.dev/api/films/1/'],\n", + " 'species': ['https://swapi.dev/api/species/2/'],\n", + " 'vehicles': [],\n", + " 'starships': [],\n", + " 'created': '2014-12-10T15:57:50.959000Z',\n", + " 'edited': '2014-12-20T21:17:50.321000Z',\n", + " 'url': 'https://swapi.dev/api/people/8/'},\n", + " {'name': 'Biggs Darklighter',\n", + " 'height': '183',\n", + " 'mass': '84',\n", + " 'hair_color': 'black',\n", + " 'skin_color': 'light',\n", + " 'eye_color': 'brown',\n", + " 'birth_year': '24BBY',\n", + " 'gender': 'male',\n", + " 'homeworld': 'https://swapi.dev/api/planets/1/',\n", + " 'films': ['https://swapi.dev/api/films/1/'],\n", + " 'species': [],\n", + " 'vehicles': [],\n", + " 'starships': ['https://swapi.dev/api/starships/12/'],\n", + " 'created': '2014-12-10T15:59:50.509000Z',\n", + " 'edited': '2014-12-20T21:17:50.323000Z',\n", + " 'url': 'https://swapi.dev/api/people/9/'},\n", + " {'name': 'Obi-Wan Kenobi',\n", + " 'height': '182',\n", + " 'mass': '77',\n", + " 'hair_color': 'auburn, white',\n", + " 'skin_color': 'fair',\n", + " 'eye_color': 'blue-gray',\n", + " 'birth_year': '57BBY',\n", + " 'gender': 'male',\n", + " 'homeworld': 'https://swapi.dev/api/planets/20/',\n", + " 'films': ['https://swapi.dev/api/films/1/',\n", + " 'https://swapi.dev/api/films/2/',\n", + " 'https://swapi.dev/api/films/3/',\n", + " 'https://swapi.dev/api/films/4/',\n", + " 'https://swapi.dev/api/films/5/',\n", + " 'https://swapi.dev/api/films/6/'],\n", + " 'species': [],\n", + " 'vehicles': ['https://swapi.dev/api/vehicles/38/'],\n", + " 'starships': ['https://swapi.dev/api/starships/48/',\n", + " 'https://swapi.dev/api/starships/59/',\n", + " 'https://swapi.dev/api/starships/64/',\n", + " 'https://swapi.dev/api/starships/65/',\n", + " 'https://swapi.dev/api/starships/74/'],\n", + " 'created': '2014-12-10T16:16:29.192000Z',\n", + " 'edited': '2014-12-20T21:17:50.325000Z',\n", + " 'url': 'https://swapi.dev/api/people/10/'}]}]\n" + ] + } + ], + "source": [ + "baseurl = 'https://swapi.dev/api/people/?page='\n", + "new_list = []\n", + "for i in range(1, 2):\n", + " response2 = requests.get(baseurl + str(i))\n", + " r = response2.json()\n", + " new_list.append(r)\n", + "\n", + "print(len(new_list))\n", + "pp(new_list)\n", + "\n", + "\n", + "\n", + "\n", + "\n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[{'name': 'Luke Skywalker', 'height': '172', 'mass': '77', 'hair_color': 'blond', 'skin_color': 'fair', 'eye_color': 'blue', 'birth_year': '19BBY', 'gender': 'male', 'homeworld': 'https://swapi.dev/api/planets/1/', 'films': ['https://swapi.dev/api/films/1/', 'https://swapi.dev/api/films/2/', 'https://swapi.dev/api/films/3/', 'https://swapi.dev/api/films/6/'], 'species': [], 'vehicles': ['https://swapi.dev/api/vehicles/14/', 'https://swapi.dev/api/vehicles/30/'], 'starships': ['https://swapi.dev/api/starships/12/', 'https://swapi.dev/api/starships/22/'], 'created': '2014-12-09T13:50:51.644000Z', 'edited': '2014-12-20T21:17:56.891000Z', 'url': 'https://swapi.dev/api/people/1/'}, {'name': 'C-3PO', 'height': '167', 'mass': '75', 'hair_color': 'n/a', 'skin_color': 'gold', 'eye_color': 'yellow', 'birth_year': '112BBY', 'gender': 'n/a', 'homeworld': 'https://swapi.dev/api/planets/1/', 'films': ['https://swapi.dev/api/films/1/', 'https://swapi.dev/api/films/2/', 'https://swapi.dev/api/films/3/', 'https://swapi.dev/api/films/4/', 'https://swapi.dev/api/films/5/', 'https://swapi.dev/api/films/6/'], 'species': ['https://swapi.dev/api/species/2/'], 'vehicles': [], 'starships': [], 'created': '2014-12-10T15:10:51.357000Z', 'edited': '2014-12-20T21:17:50.309000Z', 'url': 'https://swapi.dev/api/people/2/'}, {'name': 'R2-D2', 'height': '96', 'mass': '32', 'hair_color': 'n/a', 'skin_color': 'white, blue', 'eye_color': 'red', 'birth_year': '33BBY', 'gender': 'n/a', 'homeworld': 'https://swapi.dev/api/planets/8/', 'films': ['https://swapi.dev/api/films/1/', 'https://swapi.dev/api/films/2/', 'https://swapi.dev/api/films/3/', 'https://swapi.dev/api/films/4/', 'https://swapi.dev/api/films/5/', 'https://swapi.dev/api/films/6/'], 'species': ['https://swapi.dev/api/species/2/'], 'vehicles': [], 'starships': [], 'created': '2014-12-10T15:11:50.376000Z', 'edited': '2014-12-20T21:17:50.311000Z', 'url': 'https://swapi.dev/api/people/3/'}, {'name': 'Darth Vader', 'height': '202', 'mass': '136', 'hair_color': 'none', 'skin_color': 'white', 'eye_color': 'yellow', 'birth_year': '41.9BBY', 'gender': 'male', 'homeworld': 'https://swapi.dev/api/planets/1/', 'films': ['https://swapi.dev/api/films/1/', 'https://swapi.dev/api/films/2/', 'https://swapi.dev/api/films/3/', 'https://swapi.dev/api/films/6/'], 'species': [], 'vehicles': [], 'starships': ['https://swapi.dev/api/starships/13/'], 'created': '2014-12-10T15:18:20.704000Z', 'edited': '2014-12-20T21:17:50.313000Z', 'url': 'https://swapi.dev/api/people/4/'}, {'name': 'Leia Organa', 'height': '150', 'mass': '49', 'hair_color': 'brown', 'skin_color': 'light', 'eye_color': 'brown', 'birth_year': '19BBY', 'gender': 'female', 'homeworld': 'https://swapi.dev/api/planets/2/', 'films': ['https://swapi.dev/api/films/1/', 'https://swapi.dev/api/films/2/', 'https://swapi.dev/api/films/3/', 'https://swapi.dev/api/films/6/'], 'species': [], 'vehicles': ['https://swapi.dev/api/vehicles/30/'], 'starships': [], 'created': '2014-12-10T15:20:09.791000Z', 'edited': '2014-12-20T21:17:50.315000Z', 'url': 'https://swapi.dev/api/people/5/'}, {'name': 'Owen Lars', 'height': '178', 'mass': '120', 'hair_color': 'brown, grey', 'skin_color': 'light', 'eye_color': 'blue', 'birth_year': '52BBY', 'gender': 'male', 'homeworld': 'https://swapi.dev/api/planets/1/', 'films': ['https://swapi.dev/api/films/1/', 'https://swapi.dev/api/films/5/', 'https://swapi.dev/api/films/6/'], 'species': [], 'vehicles': [], 'starships': [], 'created': '2014-12-10T15:52:14.024000Z', 'edited': '2014-12-20T21:17:50.317000Z', 'url': 'https://swapi.dev/api/people/6/'}, {'name': 'Beru Whitesun lars', 'height': '165', 'mass': '75', 'hair_color': 'brown', 'skin_color': 'light', 'eye_color': 'blue', 'birth_year': '47BBY', 'gender': 'female', 'homeworld': 'https://swapi.dev/api/planets/1/', 'films': ['https://swapi.dev/api/films/1/', 'https://swapi.dev/api/films/5/', 'https://swapi.dev/api/films/6/'], 'species': [], 'vehicles': [], 'starships': [], 'created': '2014-12-10T15:53:41.121000Z', 'edited': '2014-12-20T21:17:50.319000Z', 'url': 'https://swapi.dev/api/people/7/'}, {'name': 'R5-D4', 'height': '97', 'mass': '32', 'hair_color': 'n/a', 'skin_color': 'white, red', 'eye_color': 'red', 'birth_year': 'unknown', 'gender': 'n/a', 'homeworld': 'https://swapi.dev/api/planets/1/', 'films': ['https://swapi.dev/api/films/1/'], 'species': ['https://swapi.dev/api/species/2/'], 'vehicles': [], 'starships': [], 'created': '2014-12-10T15:57:50.959000Z', 'edited': '2014-12-20T21:17:50.321000Z', 'url': 'https://swapi.dev/api/people/8/'}, {'name': 'Biggs Darklighter', 'height': '183', 'mass': '84', 'hair_color': 'black', 'skin_color': 'light', 'eye_color': 'brown', 'birth_year': '24BBY', 'gender': 'male', 'homeworld': 'https://swapi.dev/api/planets/1/', 'films': ['https://swapi.dev/api/films/1/'], 'species': [], 'vehicles': [], 'starships': ['https://swapi.dev/api/starships/12/'], 'created': '2014-12-10T15:59:50.509000Z', 'edited': '2014-12-20T21:17:50.323000Z', 'url': 'https://swapi.dev/api/people/9/'}, {'name': 'Obi-Wan Kenobi', 'height': '182', 'mass': '77', 'hair_color': 'auburn, white', 'skin_color': 'fair', 'eye_color': 'blue-gray', 'birth_year': '57BBY', 'gender': 'male', 'homeworld': 'https://swapi.dev/api/planets/20/', 'films': ['https://swapi.dev/api/films/1/', 'https://swapi.dev/api/films/2/', 'https://swapi.dev/api/films/3/', 'https://swapi.dev/api/films/4/', 'https://swapi.dev/api/films/5/', 'https://swapi.dev/api/films/6/'], 'species': [], 'vehicles': ['https://swapi.dev/api/vehicles/38/'], 'starships': ['https://swapi.dev/api/starships/48/', 'https://swapi.dev/api/starships/59/', 'https://swapi.dev/api/starships/64/', 'https://swapi.dev/api/starships/65/', 'https://swapi.dev/api/starships/74/'], 'created': '2014-12-10T16:16:29.192000Z', 'edited': '2014-12-20T21:17:50.325000Z', 'url': 'https://swapi.dev/api/people/10/'}]\n" + ] + } + ], + "source": [ + "result_list = []\n", + "#for num in range(0, 9):\n", + " #results = new_list[num]['results']\n", + " #result_list.extend(results)\n", + "\n", + "#\n", + "\n", + "for i in new_list:\n", + " #print(i)\n", + " for j in i['results']:\n", + " result_list.append(j)\n", + "\n", + "print(result_list)\n", + "#print(new_list[0])\n", + " \n", + "#print(result_list[0]) \n" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi']\n" + ] + } + ], + "source": [ + "result_list1 = []\n", + "names = []\n", + "def get_all_names(result_list1):\n", + " names = []\n", + " for result in result_list1:\n", + " names.append(result['name'])\n", + " return names\n", + " \n", + "\n", + "result_list1 = result_list \n", + "names1 = get_all_names(result_list1)\n", + "print(names1)\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "60 17\n" + ] + } + ], + "source": [ + "def count_genders(base_url):\n", + " male_count = 0\n", + " female_count = 0\n", + " page = 1\n", + " while True:\n", + " url = base_url + str(page)\n", + " response = requests.get(url)\n", + " data = response.json()\n", + " for result in data['results']:\n", + " if result['gender'] == 'male':\n", + " male_count += 1\n", + " elif result['gender'] == 'female':\n", + " female_count += 1\n", + " if data['next'] is None:\n", + " break\n", + " page += 1\n", + " return male_count, female_count\n", + "\n", + "baseurl1 = 'https://swapi.dev/api/people/?page='\n", + "male_count1, female_count1 = count_genders(baseurl1)\n", + "print(male_count1, female_count1)\n", + "\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Anakin Skywalker', 'Wilhuff Tarkin', 'Chewbacca', 'Han Solo', 'Greedo', 'Jabba Desilijic Tiure', 'Wedge Antilles', 'Jek Tono Porkins', 'Yoda', 'Palpatine', 'Boba Fett', 'IG-88', 'Bossk', 'Lando Calrissian', 'Lobot', 'Ackbar', 'Mon Mothma', 'Arvel Crynyd', 'Wicket Systri Warrick', 'Nien Nunb', 'Qui-Gon Jinn', 'Nute Gunray', 'Finis Valorum', 'Padmé Amidala', 'Jar Jar Binks', 'Roos Tarpals', 'Rugor Nass', 'Ric Olié', 'Watto', 'Sebulba', 'Quarsh Panaka', 'Shmi Skywalker', 'Darth Maul', 'Bib Fortuna', 'Ayla Secura', 'Ratts Tyerel', 'Dud Bolt', 'Gasgano', 'Ben Quadinaros', 'Mace Windu', 'Ki-Adi-Mundi', 'Kit Fisto', 'Eeth Koth', 'Adi Gallia', 'Saesee Tiin', 'Yarael Poof', 'Plo Koon', 'Mas Amedda', 'Gregar Typho', 'Cordé', 'Cliegg Lars', 'Poggle the Lesser', 'Luminara Unduli', 'Barriss Offee', 'Dormé', 'Dooku', 'Bail Prestor Organa', 'Jango Fett', 'Zam Wesell', 'Dexter Jettster', 'Lama Su', 'Taun We', 'Jocasta Nu', 'R4-P17', 'Wat Tambor', 'San Hill', 'Shaak Ti', 'Grievous', 'Tarfful', 'Raymus Antilles', 'Sly Moore', 'Tion Medon']\n" + ] + } + ], + "source": [ + "baseurl = 'https://swapi.dev/api/people/?page='\n", + "def get_names(base_url):\n", + " names = []\n", + " page = 1\n", + " while True:\n", + " url = base_url + str(page)\n", + " response = requests.get(url)\n", + " data = response.json()\n", + " names.extend([d['name'] for d in data['results']])\n", + " if data['next'] is None:\n", + " break\n", + " page += 1\n", + " return names\n", + "\n", + "names = get_names(baseurl)\n", + "print(names)" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Owen Lars', 'Beru Whitesun lars', 'R5-D4', 'Biggs Darklighter', 'Obi-Wan Kenobi']\n", + "180\n", + "72\n" + ] + } + ], + "source": [ + "url = 'https://swapi.dev/api/people/?page = '\n", + "response1 = requests.get(url)\n", + "r = response1.json()\n", + "new_list.append(r)\n", + "names = []\n", + "for i in new_list:\n", + " for j in i['results']:\n", + " result_list.append(j)\n", + " \n", + "def get_all_names(result_list1):\n", + " \n", + " for result in result_list1:\n", + " names.append(result['name'])\n", + " return names\n", + " \n", + "def get_male_female_count(result_list2):\n", + " countmale = 0\n", + " countfemale = 0\n", + " for e in result_list2:\n", + " if e['gender'] == 'male':\n", + " countmale = countmale + 1\n", + " \n", + " elif e['gender'] == 'female':\n", + " countfemale = countfemale + 1\n", + " print(countmale)\n", + " print(countfemale)\n", + "\n", + "\n", + "\n", + "result_list1 = result_list \n", + "names1 = get_all_names(result_list1)\n", + "print(names1)\n", + "\n", + "result_list5 = result_list\n", + "get_male_female_count(result_list5)\n", + "\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Obi-Wan Kenobi', 'Luke Skywalker', 'C-3PO', 'R2-D2', 'Darth Vader', 'Leia Organa', 'Obi-Wan Kenobi']\n" + ] + } + ], + "source": [ + "def get_characters_by_film_count(result_list, min_film_count):\n", + " characters = []\n", + " for d in result_list:\n", + " film_count = len(d['films'])\n", + " if film_count > min_film_count:\n", + " characters.append(d['name'])\n", + " return characters\n", + "\n", + "results1 = result_list\n", + "characters = get_characters_by_film_count(results1, 3)\n", + "print(characters)" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "(96, 202, 159.2)\n" + ] + } + ], + "source": [ + "def get_height_stats(results):\n", + " heights = []\n", + " for d in results:\n", + " height = d['height']\n", + " if height.isdigit():\n", + " heights.append(int(height))\n", + " min_height = min(heights)\n", + " max_height = max(heights)\n", + " avg_height = sum(heights) / len(heights)\n", + " stats = min_height, max_height, avg_height\n", + " return stats\n", + "\n", + "results1 = result_list\n", + "tuple_stats = get_height_stats(results1)\n", + "print(tuple_stats)\n", + "\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 58, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'min_height': 165, 'max_height': 165, 'avg_height': 165.0}\n" + ] + } + ], + "source": [ + "def get_height_stats_by_color(results, eye_color, hair_color):\n", + " heights = []\n", + " for d in results:\n", + " if d['eye_color'] == eye_color and d['hair_color'] == hair_color:\n", + " height = d['height']\n", + " if height.isdigit():\n", + " heights.append(int(height))\n", + " min_height = min(heights)\n", + " max_height = max(heights)\n", + " avg_height = sum(heights) / len(heights)\n", + " stats = {\n", + " 'min_height': min_height,\n", + " 'max_height': max_height,\n", + " 'avg_height': avg_height,\n", + " }\n", + " return stats\n", + "\n", + "results3 = result_list\n", + "eye_color = 'blue'\n", + "hair_color = 'brown'\n", + "dict1 = get_height_stats_by_color(results3, eye_color, hair_color)\n", + "print(dict1)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)]" + }, + "orig_nbformat": 4, + "vscode": { + "interpreter": { + "hash": "8b70a4ea831fd5b9b770fab33cac3f0a285469b7491720cba6f6d634f19e8405" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/hackernewsJSON.ipynb b/hackernewsJSON.ipynb new file mode 100644 index 0000000..f34ac5b --- /dev/null +++ b/hackernewsJSON.ipynb @@ -0,0 +1,1258 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "import requests\n", + "import datetime\n", + "import json\n", + "from pprint import pp" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "99\n", + "[{'by': 'pg',\n", + " 'descendants': 15,\n", + " 'id': 1,\n", + " 'kids': [15, 234509, 487171, 82729],\n", + " 'score': 57,\n", + " 'time': 1160418111,\n", + " 'title': 'Y Combinator',\n", + " 'type': 'story',\n", + " 'url': 'http://ycombinator.com'},\n", + " {'by': 'phyllis',\n", + " 'descendants': 0,\n", + " 'id': 2,\n", + " 'score': 16,\n", + " 'time': 1160418628,\n", + " 'title': \"A Student's Guide to Startups\",\n", + " 'type': 'story',\n", + " 'url': 'http://www.paulgraham.com/mit.html'},\n", + " {'by': 'phyllis',\n", + " 'descendants': 0,\n", + " 'id': 3,\n", + " 'kids': [531602],\n", + " 'score': 7,\n", + " 'time': 1160419233,\n", + " 'title': 'Woz Interview: the early days of Apple',\n", + " 'type': 'story',\n", + " 'url': 'http://www.foundersatwork.com/stevewozniak.html'},\n", + " {'by': 'onebeerdave',\n", + " 'descendants': 0,\n", + " 'id': 4,\n", + " 'score': 5,\n", + " 'time': 1160419662,\n", + " 'title': 'NYC Developer Dilemma',\n", + " 'type': 'story',\n", + " 'url': 'http://avc.blogs.com/a_vc/2006/10/the_nyc_develop.html'},\n", + " {'by': 'perler',\n", + " 'descendants': 0,\n", + " 'id': 5,\n", + " 'score': 7,\n", + " 'time': 1160419864,\n", + " 'title': 'Google, YouTube acquisition announcement could come tonight',\n", + " 'type': 'story',\n", + " 'url': 'http://www.techcrunch.com/2006/10/09/google-youtube-sign-more-separate-deals/'},\n", + " {'by': 'perler',\n", + " 'descendants': 0,\n", + " 'id': 6,\n", + " 'score': 4,\n", + " 'time': 1160420200,\n", + " 'title': 'Business Intelligence the Inkling Way: cool prediction markets '\n", + " 'software',\n", + " 'type': 'story',\n", + " 'url': 'http://360techblog.com/2006/10/02/business-intelligence-the-inkling-way/'},\n", + " {'by': 'phyllis',\n", + " 'descendants': 0,\n", + " 'id': 7,\n", + " 'score': 5,\n", + " 'time': 1160420455,\n", + " 'title': 'Sevin Rosen Unfunds - why?',\n", + " 'type': 'story',\n", + " 'url': 'http://featured.gigaom.com/2006/10/09/sevin-rosen-unfunds-why/'},\n", + " {'by': 'frobnicate',\n", + " 'descendants': 0,\n", + " 'id': 8,\n", + " 'score': 10,\n", + " 'time': 1160421459,\n", + " 'title': 'LikeBetter featured by BBC',\n", + " 'type': 'story',\n", + " 'url': 'http://news.bbc.co.uk/2/hi/programmes/click_online/5412216.stm'},\n", + " {'by': 'askjigga',\n", + " 'descendants': 0,\n", + " 'id': 9,\n", + " 'score': 4,\n", + " 'time': 1160421542,\n", + " 'title': 'weekendr: social network for the weekend',\n", + " 'type': 'story',\n", + " 'url': 'http://www.weekendr.com/'},\n", + " {'by': 'frobnicate',\n", + " 'descendants': 0,\n", + " 'id': 10,\n", + " 'score': 3,\n", + " 'time': 1160421674,\n", + " 'title': 'PhotoShow: Broadcast Photos to Cable TV',\n", + " 'type': 'story',\n", + " 'url': 'http://www.techcrunch.com/2006/10/09/broadcast-photos-to-cable-tv/'},\n", + " {'by': 'frobnicate',\n", + " 'descendants': 0,\n", + " 'id': 11,\n", + " 'score': 5,\n", + " 'time': 1160421884,\n", + " 'title': 'Participation Inequality: Encouraging More Users to Contribute',\n", + " 'type': 'story',\n", + " 'url': 'http://www.useit.com/alertbox/participation_inequality.html'},\n", + " {'by': 'farmer',\n", + " 'descendants': 0,\n", + " 'id': 12,\n", + " 'score': 5,\n", + " 'time': 1160422112,\n", + " 'title': 'Wired: The Desktop is Dead',\n", + " 'type': 'story',\n", + " 'url': 'http://wired.com/wired/archive/14.10/cloudware.html'},\n", + " {'by': 'phyllis',\n", + " 'descendants': 0,\n", + " 'id': 13,\n", + " 'score': 5,\n", + " 'time': 1160422572,\n", + " 'title': 'The Hardest Lessons for Startups to Learn',\n", + " 'type': 'story',\n", + " 'url': 'http://www.paulgraham.com/startuplessons.html'},\n", + " {'by': 'pg',\n", + " 'descendants': 0,\n", + " 'id': 14,\n", + " 'score': 4,\n", + " 'time': 1160423420,\n", + " 'title': 'Small is Beautiful: Building a Successful Company with Less '\n", + " 'Capital',\n", + " 'type': 'story',\n", + " 'url': 'http://blogs.zdnet.com/BTL/?p=3738'},\n", + " {'by': 'sama',\n", + " 'id': 15,\n", + " 'kids': [17],\n", + " 'parent': 1,\n", + " 'text': '"the rising star of venture capital" -unknown VC eating '\n", + " 'lunch on SHR',\n", + " 'time': 1160423461,\n", + " 'type': 'comment'},\n", + " {'by': 'pg',\n", + " 'descendants': 0,\n", + " 'id': 16,\n", + " 'score': 11,\n", + " 'time': 1160423503,\n", + " 'title': 'Feld: Question Regarding NDAs',\n", + " 'type': 'story',\n", + " 'url': 'http://www.feld.com/blog/archives/001979.html'},\n", + " {'by': 'pg',\n", + " 'id': 17,\n", + " 'kids': [1079],\n", + " 'parent': 15,\n", + " 'text': 'Is there anywhere to eat on Sandhill Road?',\n", + " 'time': 1160423565,\n", + " 'type': 'comment'},\n", + " {'by': 'farmer',\n", + " 'descendants': 0,\n", + " 'id': 18,\n", + " 'score': 3,\n", + " 'time': 1160423746,\n", + " 'title': 'Voddler Raises $2.2M For Virtual Cable TV',\n", + " 'type': 'story',\n", + " 'url': 'http://www.thealarmclock.com/euro/archives/2006/10/stockholmnfounded_vo.html'},\n", + " {'by': 'pg',\n", + " 'descendants': 0,\n", + " 'id': 19,\n", + " 'score': 2,\n", + " 'time': 1160423943,\n", + " 'title': 'Will Silicon Light Illuminate the Future?',\n", + " 'type': 'story',\n", + " 'url': 'http://www.technologyreview.com/read_article.aspx?id=17588&ch=energy'},\n", + " {'by': 'pg',\n", + " 'descendants': 1,\n", + " 'id': 20,\n", + " 'kids': [23],\n", + " 'score': 8,\n", + " 'time': 1160424038,\n", + " 'title': 'Salaries at VC-backed companies',\n", + " 'type': 'story',\n", + " 'url': 'http://avc.blogs.com/a_vc/2006/10/search_by_salar.html'},\n", + " {'by': 'sama',\n", + " 'descendants': 1,\n", + " 'id': 21,\n", + " 'kids': [22],\n", + " 'score': 6,\n", + " 'time': 1160443271,\n", + " 'title': 'Best IRR ever? YouTube 1.65B...',\n", + " 'type': 'story',\n", + " 'url': 'http://www.techcrunch.com/2006/10/09/google-has-acquired-youtube/'},\n", + " {'by': 'pg',\n", + " 'id': 22,\n", + " 'parent': 21,\n", + " 'text': \"It's kind of funny that Sevin Rosen is giving up at the same time \"\n", + " 'Sequoia is scoring on this scale.',\n", + " 'time': 1160446702,\n", + " 'type': 'comment'},\n", + " {'by': 'starklysnarky',\n", + " 'id': 23,\n", + " 'parent': 20,\n", + " 'text': 'This is interesting, but the limitations become apparent with one '\n", + " 'of their example searches: http://tinyurl.com/qqnum\\r\\n'\n", + " '\\r\\n'\n", + " 'This comparison shows \"early stage\" companies offering a higher '\n", + " \"salary than fortune XXX companies. However, these numbers don't \"\n", + " 'seem to indicate total compensation, such as benefits and stock or '\n", + " 'options.\\r\\n'\n", + " '\\r\\n'\n", + " 'Still, a cool use of search technology.',\n", + " 'time': 1160447453,\n", + " 'type': 'comment'},\n", + " {'by': 'starklysnarky',\n", + " 'descendants': 0,\n", + " 'id': 24,\n", + " 'score': 9,\n", + " 'time': 1160452465,\n", + " 'title': 'MySpace: Not a purely viral start',\n", + " 'type': 'story',\n", + " 'url': 'http://www.startup-review.com/blog/myspace-case-study-not-a-purely-viral-start.php'},\n", + " {'by': 'starklysnarky',\n", + " 'descendants': 0,\n", + " 'id': 25,\n", + " 'score': 5,\n", + " 'time': 1160458412,\n", + " 'title': 'A Story About Not Going IPO During The Bubble',\n", + " 'type': 'story',\n", + " 'url': 'http://www.usatoday.com/money/industries/technology/2004-04-21-sas-culture_x.htm'},\n", + " {'by': 'zak',\n", + " 'dead': True,\n", + " 'id': 26,\n", + " 'score': 1,\n", + " 'time': 1160460733,\n", + " 'type': 'story'},\n", + " {'by': 'spez',\n", + " 'descendants': 0,\n", + " 'id': 27,\n", + " 'score': 12,\n", + " 'time': 1160489043,\n", + " 'title': 'Google Acquires YouTube For $1.6B',\n", + " 'type': 'story',\n", + " 'url': 'http://www.google.com/press/pressrel/google_youtube.html'},\n", + " {'by': 'spez',\n", + " 'dead': True,\n", + " 'descendants': -1,\n", + " 'id': 28,\n", + " 'score': 1,\n", + " 'time': 1160490910,\n", + " 'title': 'google!',\n", + " 'type': 'story',\n", + " 'url': 'http://www.google.com/press/pressrel/google_youtube.html'},\n", + " {'by': 'spez',\n", + " 'dead': True,\n", + " 'id': 29,\n", + " 'kids': [30],\n", + " 'score': 2,\n", + " 'time': 1160494490,\n", + " 'type': 'story'},\n", + " {'by': 'spez',\n", + " 'id': 30,\n", + " 'kids': [31],\n", + " 'parent': 29,\n", + " 'text': 'Stay tuned...',\n", + " 'time': 1160494499,\n", + " 'type': 'comment'},\n", + " {'by': 'pg',\n", + " 'id': 31,\n", + " 'kids': [33],\n", + " 'parent': 30,\n", + " 'text': \"I'm tuned...\",\n", + " 'time': 1160494805,\n", + " 'type': 'comment'},\n", + " {'by': 'eshear',\n", + " 'descendants': 0,\n", + " 'id': 32,\n", + " 'score': 5,\n", + " 'time': 1160494980,\n", + " 'title': 'Scratchtop - notepad for the web',\n", + " 'type': 'story',\n", + " 'url': 'http://scratchtop.com'},\n", + " {'by': 'spez',\n", + " 'id': 33,\n", + " 'kids': [34],\n", + " 'parent': 31,\n", + " 'text': 'winnar winnar chicken dinnar!',\n", + " 'time': 1160495440,\n", + " 'type': 'comment'},\n", + " {'by': 'pg',\n", + " 'id': 34,\n", + " 'kids': [36, 35, 531706],\n", + " 'parent': 33,\n", + " 'text': \"what do you mean? this story's still not #1\",\n", + " 'time': 1160495633,\n", + " 'type': 'comment'},\n", + " {'by': 'spez',\n", + " 'id': 35,\n", + " 'parent': 34,\n", + " 'text': \"perhaps if i hadn't told you it was coming\\r\\n\"\n", + " '\\r\\n'\n", + " 'http://lipstick.com/static/onlyforamoment.png',\n", + " 'time': 1160495862,\n", + " 'type': 'comment'},\n", + " {'by': 'pg',\n", + " 'id': 36,\n", + " 'parent': 34,\n", + " 'text': 'Can you do it again?',\n", + " 'time': 1160496061,\n", + " 'type': 'comment'},\n", + " {'by': 'gaborcselle',\n", + " 'descendants': 1,\n", + " 'id': 37,\n", + " 'kids': [41],\n", + " 'score': 4,\n", + " 'time': 1160511234,\n", + " 'title': 'Woah, Scrybe! - A new kind of online organiser.',\n", + " 'type': 'story',\n", + " 'url': 'http://marcusfoster.com/blog/2006/10/09/woah-scrybe/'},\n", + " {'by': 'farmer',\n", + " 'descendants': 0,\n", + " 'id': 38,\n", + " 'score': 3,\n", + " 'time': 1160513093,\n", + " 'title': 'Dot-Com Boom Echoed in Deal to Buy YouTube - New York Times',\n", + " 'type': 'story',\n", + " 'url': 'http://www.nytimes.com/2006/10/10/technology/10deal.html?ex=1318132800&en=d8a82aacfcbbe1ee&ei=5090&partner=rssuserland&emc=rss'},\n", + " {'by': 'frobnicate',\n", + " 'descendants': 0,\n", + " 'id': 39,\n", + " 'score': 3,\n", + " 'time': 1160513750,\n", + " 'title': 'Competitio.us: track your competitors online',\n", + " 'type': 'story',\n", + " 'url': 'http://www.techcrunch.com/2006/10/04/competitous-track-your-competition-online/'},\n", + " {'by': 'pg',\n", + " 'descendants': 0,\n", + " 'id': 40,\n", + " 'score': 3,\n", + " 'time': 1160513941,\n", + " 'title': 'Scoble: Google web apps "will sneak in the back door"',\n", + " 'type': 'story',\n", + " 'url': 'http://www.cioinsight.com/print_article2/0,1217,a=190067,00.asp'},\n", + " {'by': 'starklysnarky',\n", + " 'id': 41,\n", + " 'parent': 37,\n", + " 'text': \"it's interesting how a simple set of features can make the product \"\n", + " 'seem wholly different, new, and interesting. that, and the use of '\n", + " \"'revolutionary' and 'incredible' everywhere. =)\",\n", + " 'time': 1160520368,\n", + " 'type': 'comment'},\n", + " {'by': 'sergei',\n", + " 'descendants': 0,\n", + " 'id': 42,\n", + " 'kids': [28355, 28717],\n", + " 'score': 5,\n", + " 'time': 1160532601,\n", + " 'title': 'An alternative to VC: "Selling In"',\n", + " 'type': 'story',\n", + " 'url': 'http://www.venturebeat.com/contributors/2006/10/10/an-alternative-to-vc-selling-in/'},\n", + " {'by': 'farmer',\n", + " 'descendants': 1,\n", + " 'id': 43,\n", + " 'kids': [44],\n", + " 'score': 3,\n", + " 'time': 1160577901,\n", + " 'title': \"Yahoo's Growth Being Eroded by New Rivals - New York Times\",\n", + " 'type': 'story',\n", + " 'url': 'http://www.nytimes.com/2006/10/11/technology/11yahoo.html?ex=1318219200&en=538f73d9faa9d263&ei=5090&partner=rssuserland&emc=rss'},\n", + " {'by': 'spez',\n", + " 'id': 44,\n", + " 'parent': 43,\n", + " 'text': 'Welcome back, Randall',\n", + " 'time': 1160578848,\n", + " 'type': 'comment'},\n", + " {'by': 'perler',\n", + " 'descendants': 0,\n", + " 'id': 45,\n", + " 'score': 5,\n", + " 'time': 1160579078,\n", + " 'title': 'Coghead Goes Live: Build Applications Visually',\n", + " 'type': 'story',\n", + " 'url': 'http://www.techcrunch.com/2006/10/11/coghead-goes-live-build-applications-visually/'},\n", + " {'by': 'goldfish',\n", + " 'descendants': 0,\n", + " 'id': 46,\n", + " 'score': 4,\n", + " 'time': 1160581168,\n", + " 'title': 'Rentometer: Check How Your Rent Compares to Others in Your Area',\n", + " 'type': 'story',\n", + " 'url': 'http://www.rentometer.com/'},\n", + " {'by': 'onebeerdave',\n", + " 'descendants': 0,\n", + " 'id': 47,\n", + " 'score': 2,\n", + " 'time': 1160603268,\n", + " 'title': 'Another audience-driven news site that thinks it will win by '\n", + " 'paying users ',\n", + " 'type': 'story',\n", + " 'url': 'http://business2.blogs.com/business2blog/2006/10/startup_watch_c.html'},\n", + " {'by': 'zak',\n", + " 'dead': True,\n", + " 'id': 48,\n", + " 'score': 9,\n", + " 'time': 1160625123,\n", + " 'type': 'story'},\n", + " {'by': 'pg',\n", + " 'descendants': 0,\n", + " 'id': 49,\n", + " 'score': 4,\n", + " 'time': 1160659814,\n", + " 'title': 'Preezo Enters Online Office Race',\n", + " 'type': 'story',\n", + " 'url': 'http://www.techcrunch.com/2006/10/11/preezo-enters-online-office-race/'},\n", + " {'by': 'pg',\n", + " 'descendants': 0,\n", + " 'id': 50,\n", + " 'score': 4,\n", + " 'time': 1160661570,\n", + " 'title': 'Boston VCs invest in fewer consumer Internet startups than Silicon '\n", + " 'Valley firms',\n", + " 'type': 'story',\n", + " 'url': 'http://masshightech.bizjournals.com/masshightech/stories/2006/09/25/story8.html'},\n", + " {'by': 'phyllis',\n", + " 'descendants': 0,\n", + " 'id': 51,\n", + " 'score': 3,\n", + " 'time': 1160680406,\n", + " 'title': 'How Much Money Do You Make?',\n", + " 'type': 'story',\n", + " 'url': 'http://www.techcrunch.com/2006/10/12/how-much-money-do-you-make/'},\n", + " {'by': 'onebeerdave',\n", + " 'descendants': 0,\n", + " 'id': 52,\n", + " 'score': 2,\n", + " 'time': 1160680740,\n", + " 'title': 'Winners and Losers in the Google YouTube deal',\n", + " 'type': 'story',\n", + " 'url': 'http://polls.gigaom.com/2006/10/09/goobed/'},\n", + " {'by': 'gaborcselle',\n", + " 'descendants': 0,\n", + " 'id': 53,\n", + " 'score': 7,\n", + " 'time': 1160692722,\n", + " 'title': 'Top ten geek business myths',\n", + " 'type': 'story',\n", + " 'url': 'http://rondam.blogspot.com/2006/10/top-ten-geek-business-myths.html'},\n", + " {'by': 'phyllis',\n", + " 'descendants': 0,\n", + " 'id': 54,\n", + " 'score': 1,\n", + " 'time': 1160750722,\n", + " 'title': 'The PayPerPost Virus Spreads',\n", + " 'type': 'story',\n", + " 'url': 'http://www.techcrunch.com/2006/10/12/the-payperpost-virus-spreads/'},\n", + " {'by': 'perler',\n", + " 'descendants': 0,\n", + " 'id': 55,\n", + " 'score': 2,\n", + " 'time': 1160750810,\n", + " 'title': 'RealTravel Trip Planner: Cut, Paste & Share Travel Tips',\n", + " 'type': 'story',\n", + " 'url': 'http://www.techcrunch.com/2006/10/13/realtravel-trip-planner-cut-paste-share-travel-tips/'},\n", + " {'by': 'pg',\n", + " 'descendants': 0,\n", + " 'id': 56,\n", + " 'score': 4,\n", + " 'time': 1160840625,\n", + " 'title': 'Cyberface: New technology that "captures the soul"',\n", + " 'type': 'story',\n", + " 'url': 'http://www.nytimes.com/2006/10/15/movies/15waxm.html?ex=1318564800&en=e5fbcfe899f7c41d&ei=5090&partner=rssuserland&emc=rss'},\n", + " {'by': 'adamsmith',\n", + " 'descendants': 1,\n", + " 'id': 57,\n", + " 'kids': [439383],\n", + " 'score': 12,\n", + " 'time': 1160866541,\n", + " 'title': 'Black triangle: a useful shorthand and metaphor',\n", + " 'type': 'story',\n", + " 'url': 'http://www.rampantgames.com/blog/2004/10/black-triangle.html'},\n", + " {'by': 'frobnicate',\n", + " 'descendants': 0,\n", + " 'id': 58,\n", + " 'score': 7,\n", + " 'time': 1160952510,\n", + " 'title': 'NYT: How Friendster Blew It',\n", + " 'type': 'story',\n", + " 'url': 'http://www.nytimes.com/2006/10/15/business/yourmoney/15friend.html?ex=1318564800&en=3e9438ed349f7ce7&ei=5090&partner=rssuserland&emc=rss'},\n", + " {'by': 'gaborcselle',\n", + " 'descendants': 0,\n", + " 'id': 59,\n", + " 'score': 3,\n", + " 'time': 1161073953,\n", + " 'title': 'NYT on the network of ex-PayPal employees in Silicon Valley',\n", + " 'type': 'story',\n", + " 'url': 'http://www.nytimes.com/2006/10/17/technology/17paypal.html?ex=1318737600&en=6fcef6809e87f6fc&ei=5090&partner=rssuserland&emc=rss'},\n", + " {'by': 'zak',\n", + " 'dead': True,\n", + " 'id': 60,\n", + " 'score': 2,\n", + " 'time': 1161406610,\n", + " 'type': 'story'},\n", + " {'by': 'goldfish',\n", + " 'descendants': 0,\n", + " 'id': 61,\n", + " 'score': 2,\n", + " 'time': 1161541795,\n", + " 'title': 'VCs Prefer to Fund Nearby Firms - New York Times',\n", + " 'type': 'story',\n", + " 'url': 'http://www.nytimes.com/2006/10/22/business/yourmoney/22digi.html?ex=1319169600&en=fc0cc8346b8b9382&ei=5090&partner=rssuserland&emc=rss'},\n", + " {'by': 'pg',\n", + " 'descendants': 0,\n", + " 'id': 62,\n", + " 'score': 3,\n", + " 'time': 1166112730,\n", + " 'title': 'Productivity Surges During Reddit Downtime',\n", + " 'type': 'story',\n", + " 'url': 'http://rura.org/blog/2006/12/13/productivity-surges-amid-reddit-downtime/'},\n", + " {'by': 'kul',\n", + " 'descendants': 2,\n", + " 'id': 63,\n", + " 'kids': [2686, 554],\n", + " 'score': 25,\n", + " 'time': 1171852224,\n", + " 'title': 'From Oxford to Silicon Valley',\n", + " 'type': 'story',\n", + " 'url': 'http://news.bbc.co.uk/2/hi/business/6355289.stm'},\n", + " {'by': 'pg',\n", + " 'dead': True,\n", + " 'descendants': 0,\n", + " 'id': 64,\n", + " 'kids': [917],\n", + " 'score': 6,\n", + " 'time': 1171852226,\n", + " 'title': 'From Oxford to Silicon Valley',\n", + " 'type': 'story',\n", + " 'url': 'http://news.bbc.co.uk/2/hi/business/6355289.stm'},\n", + " {'by': 'perler',\n", + " 'descendants': 0,\n", + " 'id': 65,\n", + " 'score': 13,\n", + " 'time': 1171852700,\n", + " 'title': 'Y Europe can seed growth of its new stars',\n", + " 'type': 'story',\n", + " 'url': 'http://localglobe.blogspot.com/2007/02/y-europe-can-seed-growth-of-its-new.html'},\n", + " {'by': 'pg',\n", + " 'descendants': 0,\n", + " 'id': 66,\n", + " 'score': 12,\n", + " 'time': 1171852951,\n", + " 'title': 'Adobe gives new flash tools only to Photobucket',\n", + " 'type': 'story',\n", + " 'url': 'http://www.techcrunch.com/2007/02/16/newest-flash-tools-on-display-at-photobucket/'},\n", + " {'by': 'zak',\n", + " 'dead': True,\n", + " 'id': 67,\n", + " 'kids': [166],\n", + " 'score': 14,\n", + " 'time': 1171854098,\n", + " 'type': 'story'},\n", + " {'by': 'zak',\n", + " 'dead': True,\n", + " 'id': 68,\n", + " 'score': 3,\n", + " 'time': 1171854261,\n", + " 'type': 'story'},\n", + " {'by': 'zak',\n", + " 'dead': True,\n", + " 'id': 69,\n", + " 'kids': [76],\n", + " 'score': 7,\n", + " 'time': 1171854456,\n", + " 'type': 'story'},\n", + " {'by': 'zak',\n", + " 'dead': True,\n", + " 'id': 70,\n", + " 'kids': [82],\n", + " 'score': 8,\n", + " 'time': 1171854524,\n", + " 'type': 'story'},\n", + " {'by': 'zak',\n", + " 'dead': True,\n", + " 'id': 71,\n", + " 'score': 2,\n", + " 'time': 1171854730,\n", + " 'type': 'story'},\n", + " {'by': 'onebeerdave',\n", + " 'descendants': 0,\n", + " 'id': 72,\n", + " 'score': 3,\n", + " 'time': 1171855419,\n", + " 'title': 'New Revenue Stream For Bloggers: TextMark SMS Alerts',\n", + " 'type': 'story',\n", + " 'url': 'http://www.techcrunch.com/2007/02/18/new-revenue-stream-for-bloggers-textmark-sms-alerts/'},\n", + " {'by': 'onebeerdave',\n", + " 'dead': True,\n", + " 'descendants': -1,\n", + " 'id': 73,\n", + " 'score': 1,\n", + " 'time': 1171855797,\n", + " 'title': 'New Revenue Stream For Bloggers: TextMark SMS Alerts',\n", + " 'type': 'story',\n", + " 'url': 'http://www.techcrunch.com/2007/02/18/new-revenue-stream-for-bloggers-textmark-sms-alerts/'},\n", + " {'by': 'phyllis',\n", + " 'descendants': 0,\n", + " 'id': 74,\n", + " 'score': 37,\n", + " 'time': 1171857439,\n", + " 'title': 'Startup School 2007: 3/24 at Stanford',\n", + " 'type': 'story',\n", + " 'url': 'http://startupschool.org/'},\n", + " {'by': 'phyllis',\n", + " 'descendants': 1,\n", + " 'id': 75,\n", + " 'kids': [200],\n", + " 'score': 13,\n", + " 'time': 1171858040,\n", + " 'title': 'Learning from Founders',\n", + " 'type': 'story',\n", + " 'url': 'http://www.paulgraham.com/foundersatwork.html'},\n", + " {'by': 'matt',\n", + " 'id': 76,\n", + " 'parent': 69,\n", + " 'text': '>> â\\x80\\x9cI came to the conclusion that $500,000 was the '\n", + " 'new $5 million,â\\x80\\x9d said Michael Maples Jr., an entrepreneur '\n", + " 'who created a $15 million venture fund aimed at investing in '\n", + " 'companies that required little capital.\\r\\n'\n", + " '\\r\\n'\n", + " 'Pfft. $18k is the new $500k.',\n", + " 'time': 1171858302,\n", + " 'type': 'comment'},\n", + " {'by': 'phyllis',\n", + " 'descendants': 2,\n", + " 'id': 77,\n", + " 'kids': [79],\n", + " 'score': 9,\n", + " 'time': 1171858425,\n", + " 'title': \"Tech's younger generation leans on Web 2.0 for love\",\n", + " 'type': 'story',\n", + " 'url': 'http://www.sfgate.com/cgi-bin/article.cgi?f=/c/a/2007/02/14/MNGEVO4DOV1.DTL&hw=jessica+guynn&sn=001&sc=1000'},\n", + " {'by': 'matt',\n", + " 'descendants': 0,\n", + " 'id': 78,\n", + " 'score': 8,\n", + " 'time': 1171858916,\n", + " 'title': \"Alexa's Inaccurate Traffic Stats Become More Detailed\",\n", + " 'type': 'story',\n", + " 'url': 'http://mashable.com/2007/02/16/alexas-inaccurate-traffic-stats-become-more-detailed/'},\n", + " {'by': 'matt',\n", + " 'id': 79,\n", + " 'kids': [80],\n", + " 'parent': 77,\n", + " 'text': 'Launch party circuit, eh?',\n", + " 'time': 1171860599,\n", + " 'type': 'comment'},\n", + " {'by': 'pg',\n", + " 'id': 80,\n", + " 'parent': 79,\n", + " 'text': 'yet another advantage of being in the Bay Area',\n", + " 'time': 1171860682,\n", + " 'type': 'comment'},\n", + " {'by': 'justin',\n", + " 'descendants': 0,\n", + " 'id': 81,\n", + " 'score': 5,\n", + " 'time': 1171869130,\n", + " 'title': 'allfreecalls.com shut down by AT&T',\n", + " 'type': 'story',\n", + " 'url': 'http://www.techcrunch.com/2007/02/16/allfreecalls-shut-down/'},\n", + " {'by': 'solfox',\n", + " 'id': 82,\n", + " 'parent': 70,\n", + " 'text': 'dude, shoutfit!',\n", + " 'time': 1171871594,\n", + " 'type': 'comment'},\n", + " {'by': 'gaborcselle',\n", + " 'descendants': 0,\n", + " 'id': 83,\n", + " 'score': 12,\n", + " 'time': 1171878148,\n", + " 'title': \"Why Startups Don't Condense in Europe\",\n", + " 'type': 'story',\n", + " 'url': 'http://www.gaborcselle.com/blog/2006/12/why-startups-dont-condense-in-europe.html'},\n", + " {'by': 'nate',\n", + " 'descendants': 0,\n", + " 'id': 84,\n", + " 'score': 6,\n", + " 'time': 1171906562,\n", + " 'title': 'Sell more of your product by throwing out some of its features?',\n", + " 'type': 'story',\n", + " 'url': 'http://code.inklingmarkets.com/journal/2007/2/16/sell-more-of-your-product-by-throwing-out-some-of-its-features.html'},\n", + " {'by': 'nate',\n", + " 'descendants': 0,\n", + " 'id': 85,\n", + " 'kids': [85945],\n", + " 'score': 5,\n", + " 'time': 1171906738,\n", + " 'title': 'Hack your desk to remove the clutter',\n", + " 'type': 'story',\n", + " 'url': 'http://lifehacker.com/software/diy/diy-underdesk-gadget-mount-237789.php'},\n", + " {'by': 'adam_inkling',\n", + " 'descendants': 1,\n", + " 'id': 86,\n", + " 'kids': [130],\n", + " 'score': 10,\n", + " 'time': 1171907154,\n", + " 'title': 'Project management alternative to basecamp - ticketing and version '\n", + " 'control too :)',\n", + " 'type': 'story',\n", + " 'url': 'http://unfuddle.com'},\n", + " {'by': 'adam_inkling',\n", + " 'dead': True,\n", + " 'descendants': -1,\n", + " 'id': 87,\n", + " 'score': 1,\n", + " 'time': 1171907208,\n", + " 'title': 'Project management alternative to basecamp - ticketing and version '\n", + " 'control too :)',\n", + " 'type': 'story',\n", + " 'url': 'http://unfuddle.com'},\n", + " {'by': 'adam_inkling',\n", + " 'dead': True,\n", + " 'descendants': -1,\n", + " 'id': 88,\n", + " 'score': 1,\n", + " 'time': 1171907224,\n", + " 'title': 'Project management alternative to basecamp - ticketing and version '\n", + " 'control too :)',\n", + " 'type': 'story',\n", + " 'url': 'http://unfuddle.com'},\n", + " {'by': 'nate',\n", + " 'descendants': 0,\n", + " 'id': 89,\n", + " 'score': 2,\n", + " 'time': 1171907246,\n", + " 'title': 'Win A Copy Of Yojimbo - a pretty good organizer for the Mac.',\n", + " 'type': 'story',\n", + " 'url': 'http://macapper.com/2007/02/19/win-a-copy-of-yojimbo/'},\n", + " {'by': 'adam_inkling',\n", + " 'descendants': 0,\n", + " 'id': 90,\n", + " 'score': 11,\n", + " 'time': 1171907435,\n", + " 'title': 'Where all the cool kids get their icons',\n", + " 'type': 'story',\n", + " 'url': 'http://famfamfam.com'},\n", + " {'by': 'pc',\n", + " 'descendants': 0,\n", + " 'id': 91,\n", + " 'score': 5,\n", + " 'time': 1171908063,\n", + " 'title': 'SunRocket cofounders up and leave',\n", + " 'type': 'story',\n", + " 'url': 'http://www.washingtonpost.com/wp-dyn/content/article/2007/02/11/AR2007021101198.html?nav=rss_technology'},\n", + " {'by': 'greg',\n", + " 'descendants': 0,\n", + " 'id': 92,\n", + " 'score': 10,\n", + " 'time': 1171908560,\n", + " 'title': \"Track users' mouse movements on your webpages\",\n", + " 'type': 'story',\n", + " 'url': 'http://www.clicktale.com/'},\n", + " {'by': 'pc',\n", + " 'descendants': 0,\n", + " 'id': 93,\n", + " 'kids': [151],\n", + " 'score': 6,\n", + " 'time': 1171909142,\n", + " 'title': \"Google's Searchmash adds inline video\",\n", + " 'type': 'story',\n", + " 'url': 'http://www.searchmash.com/search/star+wars+kid'},\n", + " {'by': 'thecurve',\n", + " 'descendants': 0,\n", + " 'id': 94,\n", + " 'score': 11,\n", + " 'time': 1171909181,\n", + " 'title': 'Premium GMail Coming Soon - $25/yr for 6 Gigs',\n", + " 'type': 'story',\n", + " 'url': 'http://www.chron.com/disp/story.mpl/ap/fn/4552107.html'},\n", + " {'by': 'pc',\n", + " 'descendants': 0,\n", + " 'id': 95,\n", + " 'score': 8,\n", + " 'time': 1171909306,\n", + " 'title': \"YouTube: "identifying copyrighted material can't be an \"\n", + " 'automated process." Startup disagrees.',\n", + " 'type': 'story',\n", + " 'url': 'http://www.iht.com/articles/2007/02/19/business/piracy.php'},\n", + " {'by': 'thecurve',\n", + " 'descendants': 0,\n", + " 'id': 96,\n", + " 'score': 4,\n", + " 'time': 1171909460,\n", + " 'title': \"Google's Build Your Campus in 3D Competition\",\n", + " 'type': 'story',\n", + " 'url': 'http://googleblog.blogspot.com/2007/01/show-us-your-university-campus-in-3d.html'},\n", + " {'by': 'emmett',\n", + " 'descendants': 0,\n", + " 'id': 97,\n", + " 'score': 11,\n", + " 'time': 1171909762,\n", + " 'title': 'An Overview of the Five Gazillion Video Startups',\n", + " 'type': 'story',\n", + " 'url': 'http://www.readwriteweb.com/archives/online_video_index.php'},\n", + " {'by': 'farmer',\n", + " 'descendants': 0,\n", + " 'id': 98,\n", + " 'score': 6,\n", + " 'time': 1171910119,\n", + " 'title': '25 Most Interesting VoIP Startups',\n", + " 'type': 'story',\n", + " 'url': 'http://www.voip-news.com/feature/25-most-interesting-voip-startups-021207/'},\n", + " {'by': 'pg',\n", + " 'descendants': 0,\n", + " 'id': 99,\n", + " 'score': 5,\n", + " 'time': 1171910249,\n", + " 'title': 'The Google-Powered Business',\n", + " 'type': 'story',\n", + " 'url': 'http://blog.radioactiveyak.com/2006/07/googleoffice-beta-google-powered.html'}]\n" + ] + } + ], + "source": [ + "#Create a list that stores a certain number of items to work off of. We have the range of item ids from the MAX ID endpoint. I suggest that you start small and expand the dataset\n", + "\n", + "baseurl = 'https://hacker-news.firebaseio.com/v0/item/'\n", + "new_list = []\n", + "for i in range(1,100):\n", + " response1 = requests.get(baseurl + str(i) + '.json') \n", + " r = response1.json()\n", + " new_list.append(r)\n", + "print(len(new_list))\n", + "pp(new_list)" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'by': 'phyllis',\n", + " 'descendants': 0,\n", + " 'id': 3,\n", + " 'kids': [531602],\n", + " 'score': 7,\n", + " 'time': 1160419233,\n", + " 'title': 'Woz Interview: the early days of Apple',\n", + " 'type': 'story',\n", + " 'url': 'http://www.foundersatwork.com/stevewozniak.html'}\n" + ] + } + ], + "source": [ + "#print all items of user name('id') 1\n", + "def print_items(id_no):\n", + " pp(new_list[id_no])\n", + "idnum = 2\n", + "print_items(idnum)\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[57, 16, 7, 5, 7, 4, 5, 10, 4, 3, 5, 5, 5, 4, 11, 3, 2, 8, 6, 9, 5, 1, 12, 1, 2, 5, 4, 3, 3, 3, 5, 3, 5, 4, 2, 9, 4, 4, 3, 2, 7, 1, 2, 4, 12, 7, 3, 2, 2, 3, 25, 6, 13, 12, 14, 3, 7, 8, 2, 3, 1, 37, 13, 9, 8, 5, 12, 6, 5, 10, 1, 1, 2, 11, 5, 10, 6, 11, 8, 4, 11, 6, 5]\n", + "average of scores is:6.975903614457831\n" + ] + } + ], + "source": [ + "#Write a function to display the average score of all items\n", + "score_list = []\n", + "count = 0\n", + "key1 = 'score'\n", + "for i in new_list:\n", + " if key1 in i.keys():\n", + " score_list.append(i['score'])\n", + "print(score_list) \n", + "print('average of scores is:' + str(sum(score_list)/len(score_list))) \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 75, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Average score list of user phyllis is:10.666666666666666\n" + ] + } + ], + "source": [ + "#Write a function to display the average score of the items by a certain user\n", + "score_list_user = []\n", + "count = 0\n", + "key1 = 'score'\n", + "for i in new_list:\n", + " if i['by'] == 'phyllis':\n", + " score_list_user.append(i['score'])\n", + "print('Average score list of user phyllis is:' +str(sum(score_list_user)/len(score_list_user))) \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 77, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "All comments of an item 23 are ['This is interesting, but the limitations become apparent with one of their example searches: http://tinyurl.com/qqnum\\r\\n\\r\\nThis comparison shows \"early stage\" companies offering a higher salary than fortune XXX companies. However, these numbers don\\'t seem to indicate total compensation, such as benefits and stock or options.\\r\\n\\r\\nStill, a cool use of search technology.']:\n" + ] + } + ], + "source": [ + "#Write a function to display all of the comments of an item(this is a more challenging task)\n", + "all_comm_item = []\n", + "count = 0\n", + "key1 = 'score'\n", + "def all_comments(id1):\n", + " for i in new_list:\n", + " if (i['id'] == id1 and i['type'] == 'comment'):\n", + " all_comm_item.append(i['text'])\n", + " \n", + " print('All comments of an item {} are {}:'.format(id1, str(all_comm_item) ))\n", + "\n", + "id2 = 23\n", + "all_comments(id2)\n", + "\n", + "\n", + "\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 64, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2\n", + "['This is interesting, but the limitations become apparent with one of their example searches: http://tinyurl.com/qqnum\\r\\n\\r\\nThis comparison shows \"early stage\" companies offering a higher salary than fortune XXX companies', \"However, these numbers don't seem to indicate total compensation, such as benefits and stock or options.\\r\\n\\r\\nStill, a cool use of search technology.\"]\n" + ] + } + ], + "source": [ + "#Write a function to display all of the comments of an item(this is a more challenging task)\n", + "all_comm_item = []\n", + "id1 = 23\n", + "comment = ''\n", + "for i in new_list:\n", + " if (i['id'] == id1 and i['type'] == 'comment'):\n", + " #all_comm_item.append(i['text'])\n", + " #print(''.join(i['text'])) \n", + " all_comm_item = i['text'].split(\". \")\n", + "print(len(all_comm_item))\n", + "print(all_comm_item) \n", + "\n", + " \n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Item number with year 2006 is 1\n", + "Item number with year 2006 is 2\n", + "Item number with year 2006 is 3\n", + "Item number with year 2006 is 4\n", + "Item number with year 2006 is 5\n", + "Item number with year 2006 is 6\n", + "Item number with year 2006 is 7\n", + "Item number with year 2006 is 8\n", + "Item number with year 2006 is 9\n", + "Item number with year 2006 is 10\n", + "Item number with year 2006 is 11\n", + "Item number with year 2006 is 12\n", + "Item number with year 2006 is 13\n", + "Item number with year 2006 is 14\n", + "Item number with year 2006 is 15\n", + "Item number with year 2006 is 16\n", + "Item number with year 2006 is 17\n", + "Item number with year 2006 is 18\n", + "Item number with year 2006 is 19\n", + "Item number with year 2006 is 20\n", + "Item number with year 2006 is 21\n", + "Item number with year 2006 is 22\n", + "Item number with year 2006 is 23\n", + "Item number with year 2006 is 24\n", + "Item number with year 2006 is 25\n", + "Item number with year 2006 is 26\n", + "Item number with year 2006 is 27\n", + "Item number with year 2006 is 28\n", + "Item number with year 2006 is 29\n", + "Item number with year 2006 is 30\n", + "Item number with year 2006 is 31\n", + "Item number with year 2006 is 32\n", + "Item number with year 2006 is 33\n", + "Item number with year 2006 is 34\n", + "Item number with year 2006 is 35\n", + "Item number with year 2006 is 36\n", + "Item number with year 2006 is 37\n", + "Item number with year 2006 is 38\n", + "Item number with year 2006 is 39\n", + "Item number with year 2006 is 40\n", + "Item number with year 2006 is 41\n", + "Item number with year 2006 is 42\n", + "Item number with year 2006 is 43\n", + "Item number with year 2006 is 44\n", + "Item number with year 2006 is 45\n", + "Item number with year 2006 is 46\n", + "Item number with year 2006 is 47\n", + "Item number with year 2006 is 48\n", + "Item number with year 2006 is 49\n" + ] + } + ], + "source": [ + "#Write a function to display all of the items created in a certain year. You can expand on this to add additional parameters: Month, Day, etc. Any combo you choose or any single value\n", + "my_year = '2006'\n", + "count = 0\n", + "for i in new_list:\n", + " #print(i['id'])\n", + " dt1 = datetime.datetime.fromtimestamp(int(i['time'])).strftime('%Y')\n", + " if dt1 == my_year:\n", + " print('Item number with year {} is {}'.format(my_year, str(i['id'])))\n", + " else:\n", + " break\n", + " \n", + " \n", + " \n", + " \n", + " \n" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[]\n", + "number of deleted items are:0\n" + ] + } + ], + "source": [ + "#Write a function to display the total number of deleted items.\n", + "delete_list= []\n", + "count = 0\n", + "key1 = 'deleted'\n", + "for i in new_list:\n", + " if key1 in i.keys():\n", + " delete_list.append(i['deleted'])\n", + "print(delete_list) \n", + "print('number of deleted items are:' + str(len(delete_list))) \n" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 27, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 61, 62, 63, 65, 66, 72, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]\n", + "number of live items are:85\n" + ] + } + ], + "source": [ + "#Write a function to display the total number of live items\n", + "live_list= []\n", + "count = 0\n", + "key1 = 'dead'\n", + "for i in new_list:\n", + " if key1 not in i.keys() or i['dead'] != True:\n", + " live_list.append(i['id'])\n", + "print(live_list) \n", + "print('number of live items are:' + str(len(live_list))) \n" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 3, 1, 2, 1, 1, 1]\n", + "3\n", + "Items with most number of comments:[41]\n" + ] + } + ], + "source": [ + "#Write a function to find which item has the most total comments\n", + "most_comm_item = []\n", + "most_item_id = []\n", + "comment = ''\n", + "len_list = []\n", + "for i in new_list:\n", + " if i['type'] == 'comment':\n", + " #all_comm_item.append(i['text'])\n", + " #print(''.join(i['text'])) \n", + " most_comm_item = i['text'].split(\". \")\n", + " len_list.append(len(most_comm_item))\n", + " \n", + "print(len_list)\n", + "max_len = max(len_list)\n", + "print(max_len)\n", + "\n", + "for i in new_list:\n", + " if i['type'] == 'comment':\n", + " most_comm_item = i['text'].split(\". \")\n", + " if len(most_comm_item) == max(len_list):\n", + " most_item_id.append(i['id'])\n", + "\n", + "print('Items with most number of comments:' +str(most_item_id)) \n" + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 3, 1, 2, 1, 1, 1]\n", + "3\n", + "Users who made most number of comments:['starklysnarky']\n" + ] + } + ], + "source": [ + "#Write a function to find which user made the most comments.\n", + "most_comm_item_user = []\n", + "most_item_user = []\n", + "comment = ''\n", + "len_list = []\n", + "for i in new_list:\n", + " if i['type'] == 'comment':\n", + " #all_comm_item.append(i['text'])\n", + " #print(''.join(i['text'])) \n", + " most_comm_item_user = i['text'].split(\". \")\n", + " len_list.append(len(most_comm_item_user))\n", + " \n", + "print(len_list)\n", + "max_len = max(len_list)\n", + "print(max_len)\n", + "\n", + "for i in new_list:\n", + " if i['type'] == 'comment':\n", + " most_comm_item_user = i['text'].split(\". \")\n", + " if len(most_comm_item_user) == max(len_list):\n", + " most_item_user.append(i['by'])\n", + "\n", + "print('Users who made most number of comments:' +str(most_item_user)) \n" + ] + }, + { + "cell_type": "code", + "execution_count": 74, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'22', '13', '00', '11', '09', '14', '18', '04', '01', '10', '17', '23', '02', '21', '12', '16', '15'}\n", + "{'22': 11, '13': 9, '00': 1, '11': 12, '09': 2, '14': 7, '18': 4, '04': 2, '01': 1, '10': 5, '17': 1, '23': 8, '02': 3, '21': 5, '12': 8, '16': 5, '15': 15}\n", + "the hour that most users made comments is : 15\n" + ] + } + ], + "source": [ + "#Write a function to see which hour of the day is the most common for a user to post an item\n", + "hour_list = []\n", + "for i in new_list:\n", + " hour_list.append(datetime.datetime.fromtimestamp(int(i['time'])).strftime('%H'))\n", + "#using set function to group list data and print as dictionary\n", + "print(set(hour_list))\n", + "#to get count of each element in the hour_list in dictionary format\n", + "d = {x:hour_list.count(x) for x in set(hour_list)}\n", + "#to print the dictionary with hour as key and value as counter\n", + "print(d)\n", + "print('the hour that most users made comments is : ' +str(max(set(hour_list), key = hour_list.count)))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.0" + }, + "orig_nbformat": 4, + "vscode": { + "interpreter": { + "hash": "8b70a4ea831fd5b9b770fab33cac3f0a285469b7491720cba6f6d634f19e8405" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/json_python_practice.py b/json_python_practice.py new file mode 100644 index 0000000..83b8ad2 --- /dev/null +++ b/json_python_practice.py @@ -0,0 +1,13 @@ +import json +import requests +from pprint import pp + +response = requests.get("http://jsonplaceholder.typicode.com/todos") + +todos = json.loads(response.text) +print(type(todos)) +print(todos) + +for item in todos: + print(item) + diff --git a/list_length.py b/list_length.py new file mode 100644 index 0000000..ad2264f --- /dev/null +++ b/list_length.py @@ -0,0 +1,5 @@ + +#Length - Create a function that takes a list as an argument and returns the length of the list. For example length([1,2,3,4]) should return 4 +def lenlist(list1): + return len(list1) +len1 = print(lenlist([1,2,3,4])) diff --git a/list_suavmimax.py b/list_suavmimax.py new file mode 100644 index 0000000..34259fc --- /dev/null +++ b/list_suavmimax.py @@ -0,0 +1,23 @@ + +#Ultimateaalyze - Create a function that takes a list as an argument and returns a dictionary that has the sumTotal, average, minimum, maximum ad length of the list. +def d_suavmima(list1): + sumlist = sum(list1) + avglist = sum(list1)/len(list1) + minlist = min(list1) + maxlist = max(list1) + lenlist = len(list1) + list2 = ['sumlist', 'avglist', 'minlist', 'maxlist', 'lenlist'] + list3 = [sum(list1), sum(list1)/len(list1), min(list1), max(list1), len(list1)] + i = iter(list2) + j = iter(list3) + dic1 = dict(zip(i, j)) + return dic1 + +dict1 = print(d_suavmima([1,2,3,4])) + + + + + + + \ No newline at end of file diff --git a/make_it_big.py b/make_it_big.py new file mode 100644 index 0000000..20c0b56 --- /dev/null +++ b/make_it_big.py @@ -0,0 +1,10 @@ +#Biggie Size - Given a list, write a function that changes all positive numbers in the list to "big". Example: make_it_big([-1, 3, 5, -5]) returns that same list, #changed to [-1, "big", "big", -5]. +def make_it_big(array): + for i in range(len(array)): + if array[i]>0: + array[i]= "big" + return array +print(make_it_big([-1, 3, 5, -5])) + + + diff --git a/max_list.py b/max_list.py new file mode 100644 index 0000000..30debc2 --- /dev/null +++ b/max_list.py @@ -0,0 +1,11 @@ + +#Maximum - Create a function that takes a list as an argument and returns the maximum value in the list. If the passed list is empty, have the function return false. #For example maximum([1,2,3,4]) should return 4; maximum([-1,-2,-3]) should return -1. +def max_list(list1): + if list1 == []: + return False + else: + return max(list1) + +max1 = print(max_list([1,2,3,4])) +max2 = print(max_list([-1,-2,-3])) +max3 = print(max_list([])) diff --git a/min_list.py b/min_list.py new file mode 100644 index 0000000..ad0926a --- /dev/null +++ b/min_list.py @@ -0,0 +1,11 @@ + +#Minimum - Create a function that takes a list as an argument and returns the minimum value in the list. If the passed list is empty, have the function return false. #For example minimum([1,2,3,4]) should return 1; minimum([-1,-2,-3]) should return -3. +def min_list(list1): + if list1 == []: + return False + else: + return min(list1) + +min1 = print(min_list([1,2,3,4])) +min2 = print(min_list([-1,-2,-3])) +min3 = print(min_list([])) diff --git a/movies.xml b/movies.xml new file mode 100644 index 0000000..363a7a3 --- /dev/null +++ b/movies.xml @@ -0,0 +1,74 @@ + + + + + DVD + 1981 + PG + + Archaeologist and adventurer Indiana Jones + is hired by the U.S. government to find the Ark of the + Covenant before the Nazis + + + + DVD,Online + 1984 + PG + None provided + + + Blu-ray + 1985 + PG + Marty McFly + + + + + VHS + 1992 + PG13 + NA. + + + Online + 1992 + R + WhAtEvER I Want!!!?! + + + + dvd, digital + 2000 + PG-13 + Two mutants come to a private academy for their kind whose resident superhero team must + oppose a terrorist organization with similar powers + + + + + + + DVD + 1979 + R + """"""""" + + + + + DVD + 1986 + PG13 + Funny movie about a funny guy + + + blue-ray + 2000 + Unrated + psychopathic Bateman + + + + \ No newline at end of file diff --git a/palindrome.py b/palindrome.py new file mode 100644 index 0000000..a9c48d6 --- /dev/null +++ b/palindrome.py @@ -0,0 +1,12 @@ +#Ispalindrome- Given a string, write a python function to check if it is palindrome or not. A string is said to be palindrome if the reverse of the string is the same as string. For example, “radar” is a palindrome, but “radix” is not a palindrome. +def palindromchk(str1): + revstr1 = "" + for i in str1: + revstr1 = i + revstr1 + if (str1 == revstr1): + return 'Yes' + else: + return 'No' + +chk1 = print(palindromchk('12321')) + diff --git a/prob_of_day1.py b/prob_of_day1.py new file mode 100644 index 0000000..a0da720 --- /dev/null +++ b/prob_of_day1.py @@ -0,0 +1,62 @@ +#Problem1 +def sortfirstnames(list1): + sortedlist = [] + + for i in list1: + sortedlist.append(i.split()) + list1 = [] + + + for i in sorted(sortedlist, key=lambda x: x[0]): + list1.append(' '.join(i)) + + + return list1 + +list2 = ["Benjamin Witter", "Sammi Grego", "Valerie Boss"] +print("Ascending by the first name formatted with first name first") +print(sortfirstnames(list2)) + +#Problem2 +def sortlastnames(list3): + sortedlast = [] + + + for i in list3: + sortedlast.append(i.split()) + list3 = [] + + for i in sorted(sortedlast, key=lambda x: x[-1]): + list3.append(' '.join(i)) + + + return list3 + +teachers = ["Benjamin Witter", "Sammi Grego", "Valerie Boss"] +print("Ascending by the last name formatted with first name first") +print(sortlastnames(teachers)) + + +#Problem3 +def sortlastnames(list4): + sortedlastdesc = [] + swaplist = [] + + + for i in list4: + swaplist.append(i.split()[::-1]) + print(swaplist) + list4 = [] + + + for i in sorted(swaplist, key=lambda x: x[0]): + list4.append(' '.join(i)) + + + return list4 + + +teachers3 = ["Benjamin Witter", "Sammi Grego", "Valerie Boss"] +print("Descending by the last name formatted with last name first") +print(sortlastnames(teachers3)) + diff --git a/problem_of_day2.py b/problem_of_day2.py new file mode 100644 index 0000000..3bed4de --- /dev/null +++ b/problem_of_day2.py @@ -0,0 +1,2 @@ +tuple1 = ("apple", "banana", "cherry") +print(type(tuple1)) \ No newline at end of file diff --git a/regex_excercises.ipynb b/regex_excercises.ipynb new file mode 100644 index 0000000..dbbccf4 --- /dev/null +++ b/regex_excercises.ipynb @@ -0,0 +1,1341 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "mtZQDWxxos3J" + }, + "source": [ + "# 30 REGULAR EXPRESSION EXERCISES!" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "6jEYHJH3os3Q" + }, + "source": [ + "Regex, you never know when they might come in handy. It's one of the \"good programmer\"'s fundamental yet a few people actually masters them.\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 92, + "metadata": { + "id": "lbrLwTw4r9jM" + }, + "outputs": [], + "source": [ + "import datetime" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "metadata": { + "id": "ghj1l7sQos3R", + "trusted": true + }, + "outputs": [], + "source": [ + "import re" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "_tO-OHUBos3T" + }, + "source": [ + "1) Write a Python program to check that a string contains only a certain set of characters (in this case a-z, A-Z and 0-9)." + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "cXaMTykzos3V", + "outputId": "a73c9558-3ac4-44e9-ad0f-685a8f627983", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n" + ] + } + ], + "source": [ + "# Solution\n", + "def is_allowed_specific_char(text):\n", + " # Define a regex pattern\n", + " pattern = r'[a-zA-Z][0-9]'\n", + " # Use the pattern to search for ....\n", + " match = re.search(pattern, text)\n", + " if match:\n", + " return 'True' \n", + " else:\n", + " return 'False'\n", + " \n", + "a = print(is_allowed_specific_char(\"ABCDEFabcdef123450\")) # True\n", + "b = print(is_allowed_specific_char(\"*&%@#!}{\")) # False" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "2oae-B8tos3W" + }, + "source": [ + "2) Write a Python program that matches a string that has an a followed by zero or more b's" + ] + }, + { + "cell_type": "code", + "execution_count": 95, + "metadata": { + "id": "1LHEOOMkos3X", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found a match!\n", + "Found a match!\n", + "Found a match!\n", + "Not matched!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pattern = r'a(b*)'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\n", + "\n", + "print(text_match(\"ac\")) # Found a match!\n", + "print(text_match(\"abc\")) # Found a match!\n", + "print(text_match(\"abbc\")) # Found a match!\n", + "print(text_match(\"bbc\")) # Not matched" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "AQRTCmJZos3Z" + }, + "source": [ + "3) Write a Python program that matches a string that has an a followed by one or more b's" + ] + }, + { + "cell_type": "code", + "execution_count": 96, + "metadata": { + "id": "H1X2NPQYos3a", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found a match!\n", + "Found a match!\n", + "Not matched!\n", + "Not matched!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pattern = r'a(b+)'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\n", + "\n", + "print(text_match(\"ab\")) # Found a match!\n", + "print(text_match(\"abc\")) # Found a match!\n", + "print(text_match(\"ac\")) # Not matched\n", + "print(text_match(\"bbc\")) # Not matched" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "E902uTcRos3a" + }, + "source": [ + "4) Write a Python program that matches a string that has an a followed by zero or one 'b'" + ] + }, + { + "cell_type": "code", + "execution_count": 97, + "metadata": { + "id": "xeva0fRNos3b", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found a match!\n", + "Found a match!\n", + "Found a match!\n", + "Found a match!\n", + "Found a match!\n", + "Not matched!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pattern = r'a(b){0,1}'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\n", + " \n", + "print(text_match(\"ab\")) # Found a match!\n", + "print(text_match(\"abc\")) # Found a match!\n", + "print(text_match(\"abbc\")) # Found a match!\n", + "print(text_match(\"aabbc\")) # Found a match!\n", + "print(text_match(\"ac\")) # Found a match!\n", + "print(text_match(\"bbc\")) # Not matched" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "whnPaW2kos3c" + }, + "source": [ + "5) Write a Python program that matches a string that has an a followed by three 'b'" + ] + }, + { + "cell_type": "code", + "execution_count": 98, + "metadata": { + "id": "0c_7S3h_os3d", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found a match!\n", + "Found a match!\n", + "Not matched!\n", + "Not matched!\n", + "Not matched!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pattern = r'a(b){3}'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\n", + "\n", + "print(text_match(\"abbb\")) # Found a match!\n", + "print(text_match(\"aabbbbbc\")) # Found a match!\n", + "print(text_match(\"aabbc\")) # Not matched\n", + "print(text_match(\"ac\")) # Not matched\n", + "print(text_match(\"bbc\")) # Not matched" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "V4AsjJiCos3e" + }, + "source": [ + "6) Write a Python program that matches a string that has an a followed by two to three 'b'." + ] + }, + { + "cell_type": "code", + "execution_count": 99, + "metadata": { + "id": "VjA5CXi1os3e", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Not matched!\n", + "Found a match!\n", + "Found a match!\n", + "Not matched!\n", + "Not matched!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pattern = r'a(b){2,3}'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\n", + "\n", + "print(text_match(\"ab\")) # Not matched\n", + "print(text_match(\"aabbbbbc\")) # Found a match!\n", + "print(text_match(\"aabbc\")) # Found a match!\n", + "print(text_match(\"ac\")) # Not matched\n", + "print(text_match(\"bbc\")) # Not matched" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "kX7sLRavos3f" + }, + "source": [ + "7) Write a Python program to find sequences of lowercase letters joined with a underscore." + ] + }, + { + "cell_type": "code", + "execution_count": 100, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "rbxk35n7os3g", + "outputId": "3ddca986-1bd6-45f1-902c-eee85e8853e8", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found a match!\n", + "Not matched!\n", + "Not matched!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pattern = r'^[a-z]+_{1}[a-z]+'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\n", + "\n", + "print(text_match(\"aab_cbbbc\")) # Found a match!\n", + "print(text_match(\"aab_Abbbc\")) # Not matched\n", + "print(text_match(\"Aaab_abbbc\")) # Not matched" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "H4sat-Rfos3g" + }, + "source": [ + "8) Write a Python program to find the sequences of one upper case letter followed by lower case letters." + ] + }, + { + "cell_type": "code", + "execution_count": 101, + "metadata": { + "id": "oeUiVHGVos3h", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Not matched!\n", + "Found a match!\n", + "Found a match!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pattern = r'([A-Z]{1}[a-z]{1,})'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\n", + "\n", + "print(text_match(\"aab_cbbbc\")) # Not matched\n", + "print(text_match(\"aab_Abbbc\")) # Found a match!\n", + "print(text_match(\"Aaab_abbbc\")) # Found a match!" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "RgNBl7aEos3h" + }, + "source": [ + "9) Write a Python program that matches a string that has an 'a' followed by anything, ending in 'b'." + ] + }, + { + "cell_type": "code", + "execution_count": 102, + "metadata": { + "id": "YbCsos7Jos3i", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Not matched!\n", + "Not matched!\n", + "Found a match!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pattern = r'a{1}.+b{1}$'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\n", + "\n", + "print(text_match(\"aabbbbd\")) # Not matched\n", + "print(text_match(\"aabAbbbc\")) # Not matched\n", + "print(text_match(\"accddbbjjjb\")) # Found a match!" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "0oHZnx0Gos3i" + }, + "source": [ + "10) Write a Python program that matches a word at the beginning of a string." + ] + }, + { + "cell_type": "code", + "execution_count": 103, + "metadata": { + "id": "1tlZiGMaos3j", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found a match!\n", + "Not matched!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pattern = r'^\\w+'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\n", + "\n", + "\n", + "print(text_match(\"The quick brown fox jumps over the lazy dog.\")) # Found a match!\n", + "print(text_match(\" The quick brown fox jumps over the lazy dog.\")) # Not matched" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "mG0jspZFos3j" + }, + "source": [ + "11) Write a Python program that matches a word at the end of string, with optional punctuation." + ] + }, + { + "cell_type": "code", + "execution_count": 104, + "metadata": { + "id": "cmhnhbZnos3j", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found a match!\n", + "Not matched!\n", + "Not matched!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pattern = r'\\w+\\S*?$'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\n", + " \n", + "\n", + "print(text_match(\"The quick brown fox jumps over the lazy dog.\")) # Found a match!\n", + "print(text_match(\"The quick brown fox jumps over the lazy dog. \")) # Not matched\n", + "print(text_match(\"The quick brown fox jumps over the lazy dog \")) # Not matched" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "DBgbyYHXos3k" + }, + "source": [ + "12) Write a Python program that matches a word containing 'z'" + ] + }, + { + "cell_type": "code", + "execution_count": 105, + "metadata": { + "id": "WxuMRyCbos3k", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found a match!\n", + "Not matched!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pattern = r'\\w+z{1}\\w+'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\n", + "\n", + " \n", + "print(text_match(\"The quick brown fox jumps over the lazy dog.\")) # Found a match!\n", + "print(text_match(\"Python Exercises.\")) # Not matched" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "V_9_nk8Kos3l" + }, + "source": [ + "13) Write a Python program that matches a word containing 'z', not at the start or end of the word." + ] + }, + { + "cell_type": "code", + "execution_count": 106, + "metadata": { + "id": "VEhCCh7yos3l", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found a match!\n", + "Not matched!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pattern = r'\\w+z{1}\\w+'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\n", + "\n", + "print(text_match(\"The quick brown fox jumps over the lazy dog.\")) # Found a match!\n", + "print(text_match(\"Python Exercises.\")) # Not matched" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "L0lV07OCos3m" + }, + "source": [ + "14) Write a Python program to match a string that contains only upper and lowercase letters, numbers, and underscores." + ] + }, + { + "cell_type": "code", + "execution_count": 107, + "metadata": { + "id": "E9NcoWDhos3m", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Not matched!\n", + "Found a match!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def text_match(text):\n", + " pattern = r'^\\w+$'\n", + " if re.search(pattern, text):\n", + " return 'Found a match!'\n", + " else:\n", + " return'Not matched!'\n", + "\n", + "print(text_match(\"The quick brown fox jumps over the lazy dog.\")) # Not matched\n", + "print(text_match(\"Python_Exercises_1\")) # Found a match!" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "EvKJ8WOoos3n" + }, + "source": [ + "15) Write a Python program where a string will start with a specific number. " + ] + }, + { + "cell_type": "code", + "execution_count": 108, + "metadata": { + "id": "nWp5QysTos3n", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n" + ] + } + ], + "source": [ + "# Solution\n", + "def match_num(text):\n", + " pattern = r'^[5]\\w*'\n", + " if re.search(pattern, text):\n", + " return 'True'\n", + " else:\n", + " return'False'\n", + " \n", + "print(match_num('5-2345861')) # True\n", + "print(match_num('6-2345861')) # False" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "r_I1cbvios3t" + }, + "source": [ + "16) Write a Python program to remove leading zeros from an IP address" + ] + }, + { + "cell_type": "code", + "execution_count": 109, + "metadata": { + "id": "cRLX4hc3os3u", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "216.8.94.196\n" + ] + } + ], + "source": [ + "# Solution\n", + "def rewrite_ip(ip1, ip2):\n", + " ip2 = re.sub('\\.[0]*', '.', ip1)\n", + " return ip2\n", + "\n", + "ip3 = \"216.08.094.196\"\n", + "ip4 = \"\"\n", + "string = rewrite_ip(ip3, ip4)\n", + "print(string) # 216.8.94.196" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ZCn9r3o2os3u" + }, + "source": [ + "17) Write a Python program to check for a number at the end of a string." + ] + }, + { + "cell_type": "code", + "execution_count": 110, + "metadata": { + "id": "Q5t9U6n8os3u", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "False\n", + "True\n" + ] + } + ], + "source": [ + "# Solution\n", + "def end_num(text):\n", + " pattern = r'\\d$'\n", + " if re.search(pattern, text):\n", + " return 'True'\n", + " else:\n", + " return'False'\n", + "\n", + "print(end_num('abcdef')) # False\n", + "print(end_num('abcdef6')) # True" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "0tM4eW8Wos3v" + }, + "source": [ + "18) Write a Python program to search the numbers (0-9) of length between 1 to 3 in a given string. " + ] + }, + { + "cell_type": "code", + "execution_count": 111, + "metadata": { + "id": "7QGwzVxDos3v", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "12\n", + "13\n", + "345\n" + ] + } + ], + "source": [ + "# Solution\n", + "def print_digits(string, pattern):\n", + " pattern = r'(\\d{1,3})'\n", + " string2 = string.split() \n", + " for i in string2:\n", + " if re.search(pattern, i):\n", + " print(i.replace(',', ''))\n", + "\n", + "\n", + "string3 = \"Exercises number 1, 12, 13, and 345 are important\"\n", + "pattern2 = \"\"\n", + "print_digits(string3, pattern2)\n", + "# Number of length 1 to 3\n", + "# 1\n", + "# 12\n", + "# 13\n", + "# 345" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "43nNb7d_os3v" + }, + "source": [ + "19) Write a Python program to search some literals strings in a string. \n", + "Sample text : 'The quick brown fox jumps over the lazy dog.'\n", + "Searched words : 'fox', 'dog', 'horse'" + ] + }, + { + "cell_type": "code", + "execution_count": 112, + "metadata": { + "id": "KIVHxWN9os3w", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Searching for \"fox\" in \"The quick brown fox jumps over the lazy dog.\"->\n", + "Matched!\n", + "Searching for \"dog\" in \"The quick brown fox jumps over the lazy dog.\"->\n", + "Matched!\n", + "Searching for \"horse\" in \"The quick brown fox jumps over the lazy dog.\"->\n", + "Not matched!\n" + ] + } + ], + "source": [ + "# Solution\n", + "def print_match(patterns, text):\n", + " for i in patterns:\n", + " print('Searching for ' +'\"' +str(i) +'\"' +' in ' +'\"' +text +'\"' +'->')\n", + " if re.search(i, text): \n", + " print('Matched!')\n", + " else:\n", + " print('Not matched!')\n", + "\n", + "\n", + "patterns = [ 'fox', 'dog', 'horse' ]\n", + "text = 'The quick brown fox jumps over the lazy dog.'\n", + "print_match(patterns, text)\n", + "# Searching for \"fox\" in \"The quick brown fox jumps over the lazy dog.\" ->\n", + "# Matched!\n", + "# Searching for \"dog\" in \"The quick brown fox jumps over the lazy dog.\" ->\n", + "# Matched!\n", + "# Searching for \"horse\" in \"The quick brown fox jumps over the lazy dog.\" ->\n", + "# Not Matched!" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "XjdROmMhos3w" + }, + "source": [ + "20) Write a Python program to search a literals string in a string and also find the location within the original string where the pattern occurs\n", + "\n", + "Sample text : 'The quick brown fox jumps over the lazy dog.'\n", + "Searched words : 'fox'" + ] + }, + { + "cell_type": "code", + "execution_count": 113, + "metadata": { + "id": "vWViImYios3w", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found \"fox\" in \"The quick brown fox jumps over the lazy dog.\" from 16 to 19\n" + ] + } + ], + "source": [ + "# Solution\n", + "def print_match_location(pattern, text):\n", + " match1 = re.search(r'(f{1}\\w+)', text)\n", + " if match1:\n", + " start = match1.start()\n", + " end = match1.end() \n", + " print('Found ' +'\"' +pattern +'\"'+' in ' +' \"The quick brown fox jumps over the lazy dog.\" ' +' from ' +str(start) +' to ' +str(end))\n", + " else:\n", + " print('No match')\n", + " \n", + "\n", + "\n", + "pattern = 'fox'\n", + "text = 'The quick brown fox jumps over the lazy dog.'\n", + "print_match_location(pattern, text)\n", + "# Found \"fox\" in \"The quick brown fox jumps over the lazy dog.\" from 16 to 19" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "lLRotk-Oos3x" + }, + "source": [ + "21) Write a Python program to find the substrings within a string.\n", + "\n", + "Sample text :\n", + "\n", + "'Python exercises, PHP exercises, C# exercises'\n", + "\n", + "Pattern :\n", + "\n", + "'exercises'\n", + "\n", + "Note: There are three instances of exercises in the input string." + ] + }, + { + "cell_type": "code", + "execution_count": 114, + "metadata": { + "id": "XU8613rTos3x", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found \"exercises\"\n", + "Found \"exercises\"\n", + "Found \"exercises\"\n" + ] + } + ], + "source": [ + "# Solution\n", + "def find_all_matches(pattern, text):\n", + " text1 = text.split()\n", + " for i in text1:\n", + " match = re.search(r'(e{1}\\w+)', i)\n", + " if match:\n", + " print('Found ' +'\"' +pattern +'\"')\n", + "\n", + "\n", + "text = 'Python exercises, PHP exercises, C# exercises'\n", + "pattern = 'exercises'\n", + "find_all_matches(pattern, text)\n", + "# Found \"exercises\"\n", + "# Found \"exercises\"\n", + "# Found \"exercises\"" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "uvAY6v7Oos3x" + }, + "source": [ + "22) Write a Python program to find the occurrence and position of the substrings within a string." + ] + }, + { + "cell_type": "code", + "execution_count": 115, + "metadata": { + "id": "UFg6rXJnos3y", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Found \"exercises\" at 7 16\n", + "Found \"exercises\" at 22 31\n", + "Found \"exercises\" at 36 45\n" + ] + } + ], + "source": [ + "# Solution\n", + "def find_all_matches_location(pattern, text):\n", + " for m in re.finditer(pattern, text):\n", + " print('Found ' +'\"' +pattern +'\"' +' at ' +str(m.start()) +' ' +str(m.end()))\n", + " \n", + "\n", + "text = 'Python exercises, PHP exercises, C# exercises'\n", + "pattern = 'exercises'\n", + "find_all_matches_location(pattern, text)\n", + "# Found \"exercises\" at 7:16\n", + "# Found \"exercises\" at 22:31\n", + "# Found \"exercises\" at 36:45" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Sb3zKxE-os3y" + }, + "source": [ + "23) Write a Python program to replace whitespaces with an underscore and vice versa." + ] + }, + { + "cell_type": "code", + "execution_count": 125, + "metadata": { + "id": "nUxR5Xvjos3z", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Python_Exercises\n", + "Pytho Exercises\n" + ] + } + ], + "source": [ + "text = 'Python Exercises'\n", + "\n", + "# Your code\n", + "text1 = re.sub('\\s', '_', text)\n", + "print(text1) # Python_Exercises\n", + "\n", + "\n", + "# Your code\n", + "text2 = re.sub('\\w\\_', ' ', text1)\n", + "print(text2) # Python Exercises" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "grqoW-pgos3z" + }, + "source": [ + "24) Write a Python program to extract year, month and date from a an url. " + ] + }, + { + "cell_type": "code", + "execution_count": 117, + "metadata": { + "id": "K2-pacqmos3z", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('2016', '09', '02')]\n" + ] + } + ], + "source": [ + "# Solution\n", + "def extract_date(url):\n", + " return re.findall(r'/(\\d{4})/(\\d{1,2})/(\\d{1,2})/', url)\n", + " \n", + " \n", + "url1= \"https://www.washingtonpost.com/news/football-insider/wp/2016/09/02/odell-beckhams-fame-rests-on-one-stupid-little-ball-josh-norman-tells-author/\"\n", + "print(extract_date(url1)) # [('2016', '09', '02')]" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gyD5AZW7os3z" + }, + "source": [ + "25) Write a Python program to convert a date of yyyy-mm-dd format to dd-mm-yyyy format." + ] + }, + { + "cell_type": "code", + "execution_count": 118, + "metadata": { + "id": "bo-jpYqZos30", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Original date in YYY-MM-DD Format: 2026-01-02\n", + "New date in DD-MM-YYYY Format: 02-01-2026\n" + ] + } + ], + "source": [ + "# Solution\n", + "def change_date_format(dt):\n", + " return re.sub(r'(\\d{4})-(\\d{1,2})-(\\d{1,2})', '\\\\3-\\\\2-\\\\1', dt)\n", + "\n", + "\n", + "dt1 = \"2026-01-02\"\n", + "print(\"Original date in YYY-MM-DD Format: \",dt1) # Original date in YYY-MM-DD Format: 2026-01-02\n", + "print(\"New date in DD-MM-YYYY Format: \",change_date_format(dt1)) # New date in DD-MM-YYYY Format: 02-01-2026\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "IQ2SNL-gos30" + }, + "source": [ + "26) Write a Python program to match if any words from a list of words starting with letter 'P'." + ] + }, + { + "cell_type": "code", + "execution_count": 127, + "metadata": { + "id": "2hP4Twe0os31", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "('Python', 'PHP')\n" + ] + } + ], + "source": [ + "# Sample strings.\n", + "def print_words_starting_with_P(words):\n", + " for i in words:\n", + " match1 = re.match('(P\\w+)\\W(P\\w+)', i)\n", + " if match1:\n", + " print(match1.groups())\n", + "\n", + "words = [\"Python PHP\", \"Java JavaScript\", \"c c++\"]\n", + "print_words_starting_with_P(words) # ('Python', 'PHP')" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "MEUMKMn0os31" + }, + "source": [ + "27) Write a Python program to separate and print the numbers of a given string." + ] + }, + { + "cell_type": "code", + "execution_count": 120, + "metadata": { + "id": "xG8c9PtUos32", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "# 10\n", + "# 20\n", + "# 30\n" + ] + } + ], + "source": [ + "# Solution\n", + "def print_all_numbers(text):\n", + " i = text.split()\n", + " for j in i:\n", + " match1 = re.search(r'\\d', j)\n", + " if match1:\n", + " print('# ' +j.replace(',', ''))\n", + " else:\n", + " pass\n", + "# Sample string.\n", + "text = \"Ten 10, Twenty 20, Thirty 30\"\n", + "print_all_numbers(text)\n", + "# 10\n", + "# 20\n", + "# 30" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "krdKu3Mvos32" + }, + "source": [ + "28) Write a Python program to find all words starting with 'a' or 'e' in a given string." + ] + }, + { + "cell_type": "code", + "execution_count": 121, + "metadata": { + "id": "3hsR5278os32", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['example', 'eates', 'an', 'ayList', 'apacity', 'elements', 'elements', 'are', 'en', 'added', 'ayList', 'and', 'ayList', 'ed', 'accordingly']\n", + "None\n" + ] + } + ], + "source": [ + "# Solution\n", + "def get_all_words_containing_a_or_e(text):\n", + " list1 = []\n", + " list1 = re.findall(r'[ae]\\w+', text)\n", + " print(list1)\n", + "\n", + " \n", + "# Input.\n", + "text = \"The following example creates an ArrayList with a capacity of 50 elements. Four elements are then added to the ArrayList and the ArrayList is trimmed accordingly.\"\n", + "print(get_all_words_containing_a_or_e(text))\n", + "# ['example', 'eates', 'an', 'ayList', 'apacity', 'elements', 'elements', 'are', 'en', 'added', 'ayList', 'and', 'ayList', 'ed', 'accordingly']" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "CxfIZ5o9os33" + }, + "source": [ + "29) Write a Python program to separate and print the numbers and their position of a given string." + ] + }, + { + "cell_type": "code", + "execution_count": 131, + "metadata": { + "id": "XIuiRvmAos33", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "50 \n", + "Index Position: 62\n" + ] + } + ], + "source": [ + "# Solution\n", + "def print_all_numbers_and_their_position(text):\n", + " for i in re.finditer('(\\d{1,}\\s)', text):\n", + " print(i.group(0))\n", + " print('Index Position: ' +str(i.start()))\n", + " \n", + "\n", + "# Input.\n", + "text = \"The following example creates an ArrayList with a capacity of 50 elements. Four elements are then added to the ArrayList and the ArrayList is trimmed accordingly.\"\n", + "print_all_numbers_and_their_position(text)\n", + "# 50\n", + "# Index position: 62" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "JguO0fUjos34" + }, + "source": [ + "30) Write a Python program to abbreviate 'Road' as 'Rd.' in a given string." + ] + }, + { + "cell_type": "code", + "execution_count": 123, + "metadata": { + "id": "VJE1Xm2Bos34", + "trusted": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "21 Ramkrishna Rd\n" + ] + } + ], + "source": [ + "# Solution\n", + "def abbreviate_road(street):\n", + " return re.sub('Road{1}', 'Rd', street)\n", + "\n", + "street = '21 Ramkrishna Road'\n", + "\n", + "print(abbreviate_road(street)) # 21 Ramkrishna Rd.\n" + ] + } + ], + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3", + "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.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)]" + }, + "vscode": { + "interpreter": { + "hash": "8b70a4ea831fd5b9b770fab33cac3f0a285469b7491720cba6f6d634f19e8405" + } + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/reverse_list.py b/reverse_list.py new file mode 100644 index 0000000..ae8aaef --- /dev/null +++ b/reverse_list.py @@ -0,0 +1,14 @@ + +#ReverseList - Create a function that takes a list as a argument and return a list in a reversed order. Do this without creating a empty temporary list. For example #reverse([1,2,3,4]) should return [4,3,2,1]. This challenge is known to appear during basic technical interviews. +def reverse_list(list1): + left = 0 + right = len(list1) - 1 + while (left < right): + temp = list1[left] + list1[left] = list1[right] + list1[right] = temp + left+=1 + right-=1 + return list1 + +revlist1 = print(reverse_list([1,2,3,4,5])) diff --git a/sum_list.py b/sum_list.py new file mode 100644 index 0000000..6c36ad7 --- /dev/null +++ b/sum_list.py @@ -0,0 +1,8 @@ + +#SumTotal - Create a function that takes a list as an argument and returns the sum of all the values in the list. For example sum_total([1,2,3,4]) should return 10 +def sumlist(lista): + sumtotal = 0 + for i in lista: + sumtotal = sumtotal + i + return sumtotal +a = print(sumlist([1, 2, 3,4])) \ No newline at end of file