diff --git a/BasicPython_9.ipynb b/BasicPython_9.ipynb new file mode 100644 index 0000000..a9f1b01 --- /dev/null +++ b/BasicPython_9.ipynb @@ -0,0 +1,753 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Python_9.ipynb", + "provenance": [], + "collapsed_sections": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "ebXZoCN0rQF4", + "colab_type": "text" + }, + "source": [ + "# **Practice Programms**\n", + "### Lists and tuples\n", + "### Dictionary and Sets" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "40Nolxl3U7HD", + "colab_type": "text" + }, + "source": [ + "1. Create a tuple containing the names of five countries and display the whole tuple. Ask the user to enter one of the countries that have been shown to them and then display the index number (i.e. position in the list) of that item in the tuple." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "MBv5X9dNhm8G", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 70 + }, + "outputId": "1c6956ca-628d-40df-c305-7ef390a921b5" + }, + "source": [ + "country_tuple = (\"France\",\"England\",\"Spain\",\"Germany\",\"Australia\") \n", + "print(country_tuple) \n", + "country = input(\"Please enter one of the countries from above: \") \n", + "print(country, \"has index number\",country_tuple.index(country)) \n" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "('France', 'England', 'Spain', 'Germany', 'Australia')\n", + "Please enter one of the countries from above: Germany\n", + "Germany has index number 3\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Sd0StOBlhnQY", + "colab_type": "text" + }, + "source": [ + "2. Add to previous program to ask the user to enter a number and display the country in that position." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "zlwpRiQbhxhY", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 140 + }, + "outputId": "19175aef-dbec-4990-f2eb-575314c02474" + }, + "source": [ + "country_tuple = (\"France\",\"England\",\"Spain\",\"Germany\",\"Australia\") \n", + "print(country_tuple) \n", + "print() \n", + "country = input(\"Please enter one of the countries from above: \") \n", + "print(country, \"has index number\",country_tuple.index(country)) \n", + "print() \n", + "num = int(input(\"Enter a number betwen 0 and 4: \")) \n", + "print(country_tuple[num]) \n" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "('France', 'England', 'Spain', 'Germany', 'Australia')\n", + "\n", + "Please enter one of the countries from above: France\n", + "France has index number 0\n", + "\n", + "Enter a number betwen 0 and 4: 4\n", + "Australia\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "sW_0TrVuhx9J", + "colab_type": "text" + }, + "source": [ + "3. Create a list of two sports. Ask the user what their favourite sport is and add this to the end of the list. Sort the list and display it." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "1MksQrAGVL4U", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "2b8748bb-0d35-4ea8-f561-24c086330fdb" + }, + "source": [ + "sports_list = [\"tennis\",\"football\"] \n", + "sports_list.append(input(\"What is your favourite sport? \")) \n", + "print(sports_list)\n", + "sports_list.sort() \n", + "print(sports_list)\n" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "What is your favourite sport? cricket\n", + "['tennis', 'football', 'cricket']\n", + "['cricket', 'football', 'tennis']\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "L0U5-hwMh0p7", + "colab_type": "text" + }, + "source": [ + "4. Create a list of six school subjects. Ask the user which of these subjects they don’t like. Delete the subject they have chosen from the list before you display the list again." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "qgyKpj3xh5EW", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "499d7612-db84-43bf-c505-5959295fea4e" + }, + "source": [ + "subject_list = [\"maths\",\"english\",\"computing\",\"history\",\"science\",\"spanish\"] \n", + "print(subject_list) \n", + "dislike = input(\"Which of these subjects do you dislike? \") \n", + "getrid = subject_list.index(dislike) \n", + "del subject_list[getrid] \n", + "print(subject_list) \n" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "['maths', 'english', 'computing', 'history', 'science', 'spanish']\n", + "Which of these subjects do you dislike? spanish\n", + "['maths', 'english', 'computing', 'history', 'science']\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "9SPZzFLch5Zs", + "colab_type": "text" + }, + "source": [ + "5. Ask the user to enter four of their favourite foods and store them in a dictionary so that they are indexed with numbers starting from 1. Display the dictionary in full, showing the index number and the item. Ask them which they want to get rid of and remove it from the list. Sort the remaining data and display the dictionary." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "JFk7r2VjmtBI", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "5ee00554-a3a2-4e27-f1b2-22a0b60c6d94" + }, + "source": [ + "food_dictionary = {} \n", + "food1 = input(\"Enter a food you like: \") \n", + "food_dictionary[1] = food1\n", + "food2 = input(\"Enter another food you like: \")\n", + "food_dictionary[2] = food2\n", + "food3 = input(\"Enter another food you like: \")\n", + "food_dictionary[3] = food3\n", + "food4 = input(\"Enter another food you like: \")\n", + "food_dictionary[4] = food4\n", + "print(food_dictionary) \n", + "dislike = int(input(\"Which of these do you want to get rid of? \")) \n", + "del food_dictionary[dislike] \n", + "print(food_dictionary) \n", + "print(sorted(food_dictionary.values())) \n" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "{1: 'burger', 2: 'pizza', 3: 'momos'}\n", + "['burger', 'momos', 'pizza']\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "QJRiuUqCiOw-", + "colab_type": "text" + }, + "source": [ + "6. Enter a list of ten colours.\n", + "Ask the user for a starting\n", + "number between 0 and 4\n", + "and an end number\n", + "between 5 and 9. Display\n", + "the list for those colours\n", + "between the start and end\n", + "numbers the user input." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "IuagIaT0iW_x", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "b33b7131-0823-41f6-defd-405966ba717b" + }, + "source": [ + "colours = [\"red\",\"blue\",\"green\",\"black\",\"white\",\"pink\",\"grey\",\"purple\",\"yellow\",\"brown\"] \n", + "start = int(input(\"Enter a starting number (0-4): \")) \n", + "end = int(input(\"Enter an end number (5-9): \")) \n", + "print(colours[start:end]) \n" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Enter a starting number (0-4): 2\n", + "Enter an end number (5-9): 5\n", + "['green', 'black', 'white']\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "BpcEQS1xiXHZ", + "colab_type": "text" + }, + "source": [ + "7. Create a list of four three-digit\n", + "numbers. Display the list to the\n", + "user, showing each item from\n", + "the list on a separate line. Ask\n", + "the user to enter a three-digit\n", + "number. If the number they\n", + "have typed in matches one in\n", + "the list, display the position of\n", + "that number in the list,\n", + "otherwise display the message\n", + "“That is not in the list”." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "-6Fe_EtX3Fk0", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 119 + }, + "outputId": "c2c12d21-feca-43bc-9c17-cd788a625022" + }, + "source": [ + "nums = [123, 345, 456, 567]\n", + "for i in nums:\n", + " print(i)\n", + "\n", + "newNum = int(input(\"Enter a 3-digit number: \"))\n", + "\n", + "if newNum in nums:\n", + " index = nums.index(newNum)\n", + " print(f\"The number is found at {index+1}\")\n", + "else:\n", + " print(\"The new number is not found in the list\")\n" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "123\n", + "345\n", + "456\n", + "567\n", + "Enter a 3-digit number: 345\n", + "The number is found at 2\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "X9Yh_ppMiblk", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 121 + }, + "outputId": "97e40deb-b47f-477a-8b8c-47a37b7ff4be" + }, + "source": [ + "nums = [123,345,234,765] \n", + "for i in nums: \n", + " print (i) \n", + " \n", + "selection = int(input(\"Enter a number from the list: \")) \n", + "\n", + "if selection in nums: \n", + " print(selection,\"is in position\",nums.index(selection)) \n", + "else:\n", + " print(\"That is not in the list\") \n" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "123\n", + "345\n", + "234\n", + "765\n", + "Enter a number from the list: 234\n", + "234 is in position 2\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "F5aTS1oDibvj", + "colab_type": "text" + }, + "source": [ + "8. Ask the user to enter the names of three people they want to\n", + "invite to a party and store them in a list. After they have entered\n", + "all three names, ask them if they want to add another. If they do,\n", + "allow them to add more names until they answer “no”. When\n", + "they answer “no”, display how many people they have invited to\n", + "the party." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "yL70a6Vn4mK3", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 170 + }, + "outputId": "6bee988a-76a4-4c02-c3dd-2f44c66795a9" + }, + "source": [ + "invitees = []\n", + "\n", + "for i in range(3):\n", + " person = input(\"Enter the person name: \")\n", + " invitees.append(person)\n", + "print(invitees)\n", + "\n", + "more = input(\"Do you want to invite more: \").lower()\n", + "\n", + "while more == 'yes':\n", + " person = input(\"Enter the person name: \")\n", + " invitees.append(person)\n", + " more = input(\"Do you want to invite more: \").lower()\n", + "else:\n", + " print(\"You have invited the following:\")\n", + " print(invitees)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Enter the person name: mitali\n", + "Enter the person name: rupal\n", + "Enter the person name: ishabh\n", + "['mitali', 'rupal', 'ishabh']\n", + "Do you want to invite more: yes\n", + "Enter the person name: kunal\n", + "Do you want to invite more: anshu\n", + "You have invited the following:\n", + "['mitali', 'rupal', 'ishabh', 'kunal']\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "waGe9aLqiT6Q", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 138 + }, + "outputId": "cfadccaf-e746-4a37-b3e1-a2d622184361" + }, + "source": [ + "namel = input(\"Enter a name of somebody you want to invite to your party: \") \n", + "name2 = input(\"Enter another name: \") \n", + "name3 = input(\"Enter a third name: \") \n", + "party = [namel,name2,name3] \n", + "another = input(\"Do you want to invite another (y/n): \") \n", + "while another == \"y\": \n", + " newname = party.append(input(\"Enter another name: \")) \n", + " another = input(\"Do you want to invite another (y/n): \") \n", + " \n", + "print(\"You have\", len(party), \"people coming to your party\") \n" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Enter a name of somebody you want to invite to your party: x\n", + "Enter another name: y\n", + "Enter a third name: z\n", + "Do you want to invite another (y/n): y\n", + "Enter another name: a\n", + "Do you want to invite another (y/n): n\n", + "You have 4 people coming to your party\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "oQC1gEGaif9I", + "colab_type": "text" + }, + "source": [ + "9. Change previous program so that once the user has completed their list of names, display the\n", + "full list and ask them to type in one of the names on the list. Display the position of that\n", + "name in the list. Ask the user if they still want that person to come to the party. If they\n", + "answer “no”, delete that entry from the list and display the list again." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "eALtmYeN6rrz", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 238 + }, + "outputId": "4ae76bb7-9e9d-4aea-e023-e5570d932052" + }, + "source": [ + "invitees = []\n", + "\n", + "for i in range(3):\n", + " person = input(\"Enter the person name: \")\n", + " invitees.append(person)\n", + "print(invitees)\n", + "\n", + "more = input(\"Do you want to invite more: \").lower()\n", + "\n", + "while more == 'yes':\n", + " person = input(\"Enter the person name: \")\n", + " invitees.append(person)\n", + " more = input(\"Do you want to invite more: \").lower()\n", + "else:\n", + " print(\"You have invited the following:\")\n", + " print(invitees)\n", + "\n", + "newPerson = input(\"Enter one of the persons from this list: \")\n", + "print(invitees.index(newPerson))\n", + "choice = input(\"still want that person to come to the party: \").lower() # no\n", + "if choice == 'no':\n", + " del invitees[invitees.index(newPerson)]\n", + "\n", + "print(invitees)\n" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Enter the person name: rupal\n", + "Enter the person name: mitali\\\n", + "Enter the person name: rishabh\n", + "['rupal', 'mitali\\\\', 'rishabh']\n", + "Do you want to invite more: yes\n", + "Enter the person name: Muneendra\n", + "Do you want to invite more: no\n", + "You have invited the following:\n", + "['rupal', 'mitali\\\\', 'rishabh', 'Muneendra']\n", + "Enter one of the persons from this list: Muneendra\n", + "3\n", + "still want that person to come to the party: no\n", + "['rupal', 'mitali\\\\', 'rishabh']\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "UTOFs0ahimGp", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 294 + }, + "outputId": "d10d2650-7338-4e9a-ba38-8909003a0ffd" + }, + "source": [ + "namel = input(\"Enter a name of somebody you want to invite to your party: \") \n", + "name2 = input(\"Enter another name: \") \n", + "name3 = input(\"Enter a third name: \") \n", + "party = [namel,name2,name3] \n", + "another = input(\"Do you want to invite another (y/n): \") \n", + "while another == \"y\": \n", + " newname = party.append(input(\"Enter another name: \")) \n", + " another = input(\"Do you want to invite another (y/n): \") \n", + " \n", + "print(\"You have\", len(party), \"people coming to your party\") \n", + "print (party) \n", + "selection = input(\"Enter one of the names: \") \n", + "print(selection,\"is in position\",party.index(selection),\"on the list\") \n", + "stillcome = input(\"Do you still want them to come (y/n): \") \n", + "if stillcome == \"n\": \n", + " party.remove(selection) \n", + " print (party) \n" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Enter a name of somebody you want to invite to your party: x\n", + "Enter another name: y\n", + "Enter a third name: z\n", + "Do you want to invite another (y/n): y\n", + "Enter another name: a\n", + "Do you want to invite another (y/n): y\n", + "Enter another name: b\n", + "Do you want to invite another (y/n): y\n", + "Enter another name: c\n", + "Do you want to invite another (y/n): n\n", + "You have 6 people coming to your party\n", + "['x', 'y', 'z', 'a', 'b', 'c']\n", + "Enter one of the names: a\n", + "a is in position 3 on the list\n", + "Do you still want them to come (y/n): n\n", + "['x', 'y', 'z', 'b', 'c']\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "CXLsIs-uimL7", + "colab_type": "text" + }, + "source": [ + "10. Create a list containing the titles of\n", + "four TV programmes and display\n", + "them on separate lines. Ask the\n", + "user to enter another show and a\n", + "position they want it inserted into\n", + "the list. Display the list again,\n", + "showing all five TV programmes in\n", + "their new positions." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Ua2uEQV0ipmm", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 225 + }, + "outputId": "658edd5b-64c4-475e-9c5d-ee6f0969f13d" + }, + "source": [ + "tv = [\"Task Master\",\"Top Gear\",\"The Big Bang Theory\",\"How I Met Your Mother\"] \n", + "for i in tv: \n", + " print (i) \n", + " \n", + "print() \n", + "newtv = input(\"Enter another TV show: \") \n", + "position = int(input(\"Enter a number between 0 and 3: \")) \n", + "tv.insert(position,newtv) \n", + "for i in tv: \n", + " print (i) \n" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Task Master\n", + "Top Gear\n", + "The Big Bang Theory\n", + "How I Met Your Mother\n", + "\n", + "Enter another TV show: Ramayan\n", + "Enter a number between 0 and 3: 2\n", + "Task Master\n", + "Top Gear\n", + "Ramayan\n", + "The Big Bang Theory\n", + "How I Met Your Mother\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "g2kzeAbeipr4", + "colab_type": "text" + }, + "source": [ + "11. Create an empty list called \"nums\".\n", + "Ask the user to enter numbers.\n", + "After each number is entered, add\n", + "it to the end of the nums list and\n", + "display the list. Once they have\n", + "entered three numbers, ask them if\n", + "they still want the last number they\n", + "entered saved. If they say \"no\",\n", + "remove the last item from the list.\n", + "Display the list of numbers." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "emb9_c-CitiG", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 156 + }, + "outputId": "b3b42031-6d0b-43bb-ee55-4478c05b9914" + }, + "source": [ + "nums = [] \n", + "count = 0 \n", + "while count <3: \n", + " num = int(input(\"Enter a number: \")) \n", + " nums.append(num) \n", + " print (nums) \n", + " count = count + 1 \n", + " \n", + "lastnum = input(\"Do you want the last number saved (y/n): \") \n", + "\n", + "if lastnum == \"n\": \n", + " nums.remove(num) \n", + "\n", + "print (nums) \n" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "Enter a number: 5\n", + "[5]\n", + "Enter a number: 3\n", + "[5, 3]\n", + "Enter a number: 6\n", + "[5, 3, 6]\n", + "Do you want the last number saved (y/n): n\n", + "[5, 3]\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file