From a7b9ae40f255fba41188dbacc6259ee59e6de481 Mon Sep 17 00:00:00 2001 From: snigdhaasharma28 <45250240+snigdhaasharma28@users.noreply.github.com> Date: Thu, 22 Nov 2018 09:25:28 +0530 Subject: [PATCH] Add files via upload --- snigdhaaa 13.ipynb | 401 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 401 insertions(+) create mode 100644 snigdhaaa 13.ipynb diff --git a/snigdhaaa 13.ipynb b/snigdhaaa 13.ipynb new file mode 100644 index 0000000..39801ad --- /dev/null +++ b/snigdhaaa 13.ipynb @@ -0,0 +1,401 @@ +{ + "cells": [ + { + "metadata": {}, + "cell_type": "markdown", + "source": "# Lists" + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "## Lists\n- A list is a collection of items, that is stored in a variable. \n- The items should be related in some way, but there are no restrictions on what can be stored in a list." + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "### Syntax\nlist-name = [list of comma seperated values]" + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "### Slicing with list\n\nindex start with 0\n\n listname[index_number]\n\n listname[start_index:end_index]\n\n listname[:end_index]\n\n listname[start_index:]\n\n\nnegative index: \n listname[:-index] \n\n\n### list methods\n- append()\n- len()\n- insert(index,value)\n- sort()\n- reverse()\n- pop(), pop(index)\n- min(), max(), sum()\n\n\nWe can change the contents of the lists after its creation\n\nlistname[index]=newvalue" + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "#### Task 1: Run below code and try to make some changes to the code" + }, + { + "metadata": { + "trusted": true + }, + "cell_type": "code", + "source": "# list creation\ncountries = ['India', 'Australia', 'USA']\n\n# print whole list\nprint(countries)\n\n# Accessing list elements\nprint(\"Value at 1 index: \",countries[1])\n\n# assignning new value to index\ncountries[0]='Africa'\n\nprint(\"New value at index 0: \",countries[0])", + "execution_count": 2, + "outputs": [ + { + "output_type": "stream", + "text": "['India', 'Australia', 'USA']\nValue at 1 index: Australia\nNew value at index 0: Africa\n", + "name": "stdout" + } + ] + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "### Iterating lists" + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "#### Task 2: Run below code and try to make some changes to the code" + }, + { + "metadata": { + "scrolled": true, + "trusted": true + }, + "cell_type": "code", + "source": "\nhybridlists=['My name is',\" Anudit. \"\n 'I am in ',\n 3,\n ' Semester', '. My GPA is ', 10.0]\n\nprint(hybridlists)\n\n# we can access the elements of list using loops\n\nfor item in hybridlists:\n print(item, end=\"\")", + "execution_count": 8, + "outputs": [ + { + "output_type": "stream", + "text": "['My name is', ' Anudit. I am in ', 3, ' Semester', '. My GPA is ', 10.0]\nMy name is Anudit. I am in 3 Semester. My GPA is 10.0", + "name": "stdout" + } + ] + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "#### Task 3: Run below code and try to make some changes to the code" + }, + { + "metadata": { + "scrolled": false, + "trusted": true + }, + "cell_type": "code", + "source": "# print the index number with value\n\ncountries = ['India', 'Australia', 'Africa']\n\nfor index, country in enumerate(countries):\n place = str(index)\n print(\"Index: \" + place + \", Value: \" + country.title())\n \n# find index of\nindex = countries.index('Africa') \nprint(\"\\n Index of India is: \",index)", + "execution_count": 10, + "outputs": [ + { + "output_type": "stream", + "text": "Index: 0, Value: India\nIndex: 1, Value: Australia\nIndex: 2, Value: Africa\n\n Index of India is: 2\n", + "name": "stdout" + } + ] + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "#### Task 4: Run below code and try to make some changes to the code" + }, + { + "metadata": { + "trusted": true + }, + "cell_type": "code", + "source": "# Remove the last country from the list by index or by value\ndel countries[1]\n\nprint(countries)", + "execution_count": 14, + "outputs": [ + { + "output_type": "error", + "ename": "IndexError", + "evalue": "list assignment index out of range", + "traceback": [ + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", + "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# Remove the last country from the list by index or by value\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32mdel\u001b[0m \u001b[0mcountries\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mcountries\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", + "\u001b[0;31mIndexError\u001b[0m: list assignment index out of range" + ] + } + ] + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "### List comprehension (with for loop)" + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "#### Task 5: Run below code and try to make some changes to the code" + }, + { + "metadata": { + "trusted": true + }, + "cell_type": "code", + "source": "# without comprehension\n\ncubes = []\nfor x in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:\n cubes.append(x ** 3) \n \nprint(cubes)", + "execution_count": 16, + "outputs": [ + { + "output_type": "stream", + "text": "[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]\n", + "name": "stdout" + } + ] + }, + { + "metadata": { + "trusted": true + }, + "cell_type": "code", + "source": "# with comprehension\n \ncubes= [x**3 for x in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]]\nprint(cubes)", + "execution_count": 17, + "outputs": [ + { + "output_type": "stream", + "text": "[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]\n", + "name": "stdout" + } + ] + }, + { + "metadata": { + "trusted": true + }, + "cell_type": "code", + "source": "numbers=[1,2,3,4,5,6,7,8,9,10]\nlist=[x for x in numbers]\nlist", + "execution_count": 18, + "outputs": [ + { + "output_type": "execute_result", + "execution_count": 18, + "data": { + "text/plain": "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]" + }, + "metadata": {} + } + ] + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "#### Program 1: Write a python program which does the following\n- Make a list that includes four job types , such as 'programmer', 'truck driver' and so on\n- Use the *list.index()* function to find the index of one job type in your list.\n- Use the *in* function to show that this job type is in your list.\n- Use the *append()* function to add a new job type to your list.\n- Use the *insert()* function to add a new job type \"Python Programmer\" at the beginning of the list.\n- Use a loop to show all the job types in your list." + }, + { + "metadata": { + "trusted": true + }, + "cell_type": "code", + "source": "jobTypes = [\"programmer\", \"truck driver\", \"engineer\", \"doctor\"]\n\nprint(jobTypes[0])\n\nfor i in jobTypes:\n if (i == \"programmer\"):\n print(\"Yes!\")\n \njobTypes.append(\"Architect\")\njobTypes.insert(0, \"Python Progrogrammer\")\n\nprint(jobTypes)", + "execution_count": 24, + "outputs": [ + { + "output_type": "stream", + "text": "programmer\nYes!\n['Python Progrogrammer', 'programmer', 'truck driver', 'engineer', 'doctor', 'Architect']\n", + "name": "stdout" + } + ] + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "#### Problem 2: Write a python program for following\n\n- Make a list that includes the names of five famous people.\n- Remove each person from the list, one at a time, using the four methods given below:\n - Del item from the list by del: del listname[index]\n - Remove the item from the list by remove method: listname.remove('itemname')\n - Using pop() method, It removes the last item from the list: listname.pop() \n - Pop any item from the list by index: listname: listname.pop(index)\n- Print out a message that there are no famous people left in your list, and print your list to prove that it is empty." + }, + { + "metadata": { + "trusted": true + }, + "cell_type": "code", + "source": "people = [\"Ada Lovelce\",\"Grace Hopper\",\"Linus Trovaldis\",\"Bjarne Strousturp\", \"Allan Turing\"]\nprint(people)\ndel people[0]\nprint(people)\npeople.remove(\"Grace Hopper\")\nprint(people)\npeople.pop()\nprint(people)\npeople.pop(1)\nprint(people)", + "execution_count": 29, + "outputs": [ + { + "output_type": "stream", + "text": "['Ada Lovelce', 'Grace Hopper', 'Linus Trovaldis', 'Bjarne Strousturp', 'Allan Turing']\n['Grace Hopper', 'Linus Trovaldis', 'Bjarne Strousturp', 'Allan Turing']\n['Linus Trovaldis', 'Bjarne Strousturp', 'Allan Turing']\n['Linus Trovaldis', 'Bjarne Strousturp']\n['Linus Trovaldis']\n", + "name": "stdout" + } + ] + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "#### Program 3: Write a python program which does the following\n\n- Store the first ten letters of the alphabet in a list.\n- Use a slice to print out the first three letters of the alphabet.\n- Use a slice to print out any three letters from the middle of your list.\n- Use a slice to print out the letters from any point in the middle of your list, to the end.\n" + }, + { + "metadata": { + "trusted": true + }, + "cell_type": "code", + "source": "st = input()\nprint(st[0:3])\nprint(st[2:5])\nprint(st[2:])", + "execution_count": 34, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": "hellothere\nhel\nllo\nllothere\n" + } + ] + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "#### Program 4: Write a python program for following\n\n- Your goal in this exercise is to prove that copying a list protects the original list.\n- Make a list with three people's names in it.\n- Use a slice to make a copy of the entire list.\n- Add at least two new names to the new copy of the list.\n- Make a loop that prints out all of the names in the original list, along with a message that this is the original list.\n- Make a loop that prints out all of the names in the copied list, along with a message that this is the copied list." + }, + { + "metadata": { + "trusted": true + }, + "cell_type": "code", + "source": "people = [\"Ada Lovelce\",\"Grace Hopper\",\"Linus Trovaldis\"]\npeople2[:] = people[:]\npeople2.append([\"Bjarne Strousturp\", \"Allan Turing\"])\nprint(\"List 1 : \", people)\nprint(\"List 2 : \", people2)\n ", + "execution_count": 37, + "outputs": [ + { + "output_type": "stream", + "text": "List 1 : ['Ada Lovelce', 'Grace Hopper', 'Linus Trovaldis']\nList 2 : ['Ada Lovelce', 'Grace Hopper', 'Linus Trovaldis', ['Bjarne Strousturp', 'Allan Turing']]\n", + "name": "stdout" + } + ] + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "#### Program 5: Write a python program for following\n\n- Create a list to store the marks of top 15 students obtained by the students in Python Quiz held on 21/08/2018. Maxmimum marks=15\n- Print the maximum marks obtained by the student from the list\n- Print the minimum marks obtained by the student\n- Find the sum of all the marks\n- Find the average of the marks\n- Reverse the original list and display using for loop\n" + }, + { + "metadata": { + "trusted": true + }, + "cell_type": "code", + "source": "scores = [12, 1, 4, 12, 14, 5, 8, 4, 7, 2, 4 ,9, 10, 2, 11 ,12]\nprint(max(scores))\nprint(min(scores))\nsum = 0\nfor i in scores:\n sum+=i\nprint(\"Sum : \", sum)\nprint(\"Average : \", sum /15)\nfor i in range(0, 16):\n print(scores[15-i], end =\" \")", + "execution_count": 49, + "outputs": [ + { + "output_type": "stream", + "text": "14\n1\nSum : 117\nAverage : 7.8\n12 11 2 10 9 4 2 7 4 8 5 14 12 4 1 12 ", + "name": "stdout" + } + ] + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "#### Program 6: Write a program in Python to create a list which contains n integers entered by the user and delete all odd numbers from the list. Example: If initial contents of the list is=[1,2,3,4,5,6,7,8,9,10], the output would be=[2,4,6,8,10]." + }, + { + "metadata": { + "trusted": true + }, + "cell_type": "code", + "source": "l = []\nn = int(input())\nfor i in range(0, n):\n t = int(input())\n if(t%2 == 0):\n l.append(t)\n\nprint(l)", + "execution_count": 51, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": "4\n12\n34\n7\n22\n[12, 34, 22]\n" + } + ] + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "## Optional Questions" + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "#### Program 7: Write a python program for following.\n- Create an initial empty list named \"countries\" \n- Read the names of n countries from the user and add them to country list:\n- Use for loop to display all the names of the countries from the list and add \"is Great\" after the country name if it is \"India\"" + }, + { + "metadata": { + "trusted": true + }, + "cell_type": "code", + "source": "countries = []\nn = int(input())\nfor i in range(0, n):\n t = input()\n countries.append(t)\n\ncountries.insert(countries.index(\"India\"), \"India is great\")\nprint(countries)", + "execution_count": 53, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": "3\nUSA\nIndia\nSingapore\n['USA', 'India is great', 'India', 'Singapore']\n" + } + ] + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "### Program 8: Write a program in Python to create a list which contains n integers entered by the user and add 2 to each of the even numbers in the list. Example: If initial contents of the list is=[1,2,3,4,5,6,7,8,9,10], the output would be=[1,4,3,6,5,8,7,10,9,12]" + }, + { + "metadata": { + "trusted": true + }, + "cell_type": "code", + "source": "l = []\nn = int(input())\nfor i in range(0, n):\n t = int(input())\n l.append(t)\nprint(l)\nfor p in range(0, n):\n if(l[p]%2 == 0):\n l[p] +=2\nprint(l)", + "execution_count": 57, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": "3\n1\n2\n3\n[1, 2, 3]\n[1, 4, 3]\n" + } + ] + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "### Program 9: Write a Python program to find the list in a list of lists whose sum of elements is the highest.\nSample lists: [[1,2,3], [4,5,6], [10,11,12], [7,8,9]]\nExpected Output: [10, 11, 12]" + }, + { + "metadata": { + "trusted": true + }, + "cell_type": "code", + "source": "l = [[1,2,3], [4,5,6], [10,11,12], [7,8,9]]\nm = -999999\nmi=9999\nfor i in l:\n s = 0\n for p in i:\n s+=p\n if (s > m):\n m = s\n mi = l.index(i)\n\nprint(l[mi])", + "execution_count": 60, + "outputs": [ + { + "output_type": "stream", + "text": "[10, 11, 12]\n", + "name": "stdout" + } + ] + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "#### Program 10: Write a python program which does the following\n\n- Make a list of the most important words you have learned in programming so far.\n- Make a corresponding list of definitions. Fill your list with 'definition'.\n- Use a for loop to print out each word and its corresponding definition.\n- Maintain this program until you get to the section on Python's Dictionaries." + }, + { + "metadata": { + "trusted": true + }, + "cell_type": "code", + "source": "one = [\"Comments\", \"Functions\", \"len()\"]\ntwo = [\"Augmenting code with human readable descriptions can help document design decisions.\", \"Python functions can be used to abstract pieces of code to use elsewhere.\", \"Using len(some_object) returns the number of top-level items contained in the object being queried.\"]\n\nfor i in range(0, len(one)):\n print(one[i], \" : \", two[i])", + "execution_count": 62, + "outputs": [ + { + "output_type": "stream", + "text": "Comments : Augmenting code with human readable descriptions can help document design decisions.\nFunctions : Python functions can be used to abstract pieces of code to use elsewhere.\nlen() : Using len(some_object) returns the number of top-level items contained in the object being queried.\n", + "name": "stdout" + } + ] + } + ], + "metadata": { + "kernelspec": { + "name": "python3", + "display_name": "Python 3", + "language": "python" + }, + "language_info": { + "mimetype": "text/x-python", + "nbconvert_exporter": "python", + "name": "python", + "file_extension": ".py", + "version": "3.5.4", + "pygments_lexer": "ipython3", + "codemirror_mode": { + "version": 3, + "name": "ipython" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} \ No newline at end of file