{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "Tuples.ipynb", "version": "0.3.2", "provenance": [], "collapsed_sections": [], "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": { "id": "ExAd7QXKS8IA", "colab_type": "text" }, "source": [ "# Python Tuples from Scratch !!!" ] }, { "cell_type": "markdown", "metadata": { "id": "2PATKKu7S9T8", "colab_type": "text" }, "source": [ "## Let us understand the basic concepts of python tuples." ] }, { "cell_type": "markdown", "metadata": { "id": "vnTm90QcS-wm", "colab_type": "text" }, "source": [ "\n", "![alt text](https://www.gangboard.com/blog/wp-content/uploads/2019/05/Tuples-in-Python.jpg)\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "9LWiB87FTCrd", "colab_type": "text" }, "source": [ "Before starting I would like to tell you, I will write this tutorial in a question-and-answer format because it will be helpful especially during interviews. And throughout the tutorial, I will work only on a single example to maintain the uniformity." ] }, { "cell_type": "markdown", "metadata": { "id": "fpYfSSajTFIC", "colab_type": "text" }, "source": [ "## 1) What is a tuple and why is it used?\n", "\n", "A tuple is a data structure which is used store heterogeneous data meaning data of different types. A tuple is used to group together related data such as a dog name, height, weight, breed, and color (I love dogs). A tuple comprises several values separated by commas.\n", "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "MvwPhGybTM4F", "colab_type": "text" }, "source": [ "## 2) How to create a tuple?\n", "\n", "A tuple can be created by enclosing the values with a pair of brackets and separated by commas for instance:\n", "\n", "**Let us create a tuple called \"tuple\", and put in some values including both string and integer data this can be done as shown below:**" ] }, { "cell_type": "code", "metadata": { "id": "CEn0LjTuTRYw", "colab_type": "code", "outputId": "b8006c3e-e776-42fb-d5c1-38e7bfea9691", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Creating a tuple and storing the values\n", "tuple = (\"Today's\", \"Date\", \"Is\", 15, \"August\", 2019)\n", "print(tuple)" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "(\"Today's\", 'Date', 'Is', 15, 'August', 2019)\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "CSU0hTgeTT8z", "colab_type": "code", "outputId": "24d2b254-e4ff-4b0f-d109-2edda29f91af", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "print(type(tuple))" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "WGKtDIi3TZTy", "colab_type": "text" }, "source": [ "As seen above the tuple can hold heterogeneous data (Hence proved :))\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "uBKsjhYNTZ0-", "colab_type": "text" }, "source": [ "## 3) How do we access the values of a tuple?\n", "\n", "Indexing can do this. The values of tuples can be accessed by their index to get the value. To do this, all you have to do is just place the number (index value) inside the pair of square brackets along with the name of the tuple." ] }, { "cell_type": "code", "metadata": { "id": "aYIA3euLTgMf", "colab_type": "code", "outputId": "c01cc817-5cc5-49e6-9095-ba6baa4d6bd3", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Accessing the values of a tuple\n", "print(tuple[2])" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "Is\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "vcosUrpuThxV", "colab_type": "code", "outputId": "29423d67-f24d-42f4-bbab-46b9c6ac069e", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "print(tuple[5])" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "2019\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "PObeuKb6Tjxn", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "Kyt6Fp1BTl3Q", "colab_type": "text" }, "source": [ "## 4) How to nest or combine two tuples?\n", "\n", "Just placing the old tuple next to the newly created tuple separated by a comma can do nesting of two or more tuples." ] }, { "cell_type": "code", "metadata": { "id": "SvGoUPNSTi8Y", "colab_type": "code", "outputId": "f79d78dd-2892-4f7e-e4a8-8e8e8ee369e3", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Nesting two tuples as one tuple\n", "tuple1 = (\"And\", \"its\", \"Thursday\")\n", "print(tuple1)" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "('And', 'its', 'Thursday')\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "jJMFCXWVTpuM", "colab_type": "code", "outputId": "138d57c6-5204-4350-81f9-6f8a6b5aece6", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "nest = tuple, tuple1\n", "print(nest)" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "((\"Today's\", 'Date', 'Is', 15, 'August', 2019), ('And', 'its', 'Thursday'))\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "zF69Rw3tTs5Z", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "fdeiFiODTvVa", "colab_type": "text" }, "source": [ "## 5) Are tuples immutable or mutable?\n", "\n", "Before answering the question you should know that immutable values are unchangeable and mutable values are changeable. Now let's answer the question, the answer is tuples are immutable, yes once a tuple is created we can never change their values. Don't believe me, I'll prove it to you." ] }, { "cell_type": "code", "metadata": { "id": "tN20V0OiTsvK", "colab_type": "code", "outputId": "2b3a90f1-d8e9-468c-93ea-7e443887d312", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Tuples are immutable in nature\n", "print(tuple[0])" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "Today's\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "mbp7AyC9T0kh", "colab_type": "code", "outputId": "5af5bfed-375c-4914-f4d1-0155d37e9cf5", "colab": { "base_uri": "https://localhost:8080/", "height": 183 } }, "source": [ "# Changing the value of the 0th index to \"Hi\"\n", "tuple[0] = (\"Hi\")\n", "print(tuple)" ], "execution_count": 0, "outputs": [ { "output_type": "error", "ename": "TypeError", "evalue": "ignored", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mtuple\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0;34m\"Hi\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtuple\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mTypeError\u001b[0m: 'tuple' object does not support item assignment" ] } ] }, { "cell_type": "markdown", "metadata": { "id": "nXBMPvk6T2vH", "colab_type": "text" }, "source": [ "Hence the above proves that tuples are immutable in nature.\n", "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "BzKAgWk-T3lO", "colab_type": "text" }, "source": [ "## 6) Can tuples store identical data within them?\n", "\n", "Yes, tuples can store identical data within them, we can store many identical values inside a tuple. For example:" ] }, { "cell_type": "code", "metadata": { "id": "8hQPCAOzT6xN", "colab_type": "code", "outputId": "e00ccf68-9b46-4142-d7b4-557b25d25ded", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Storing identical data with a tuple\n", "tuple = (\"Today's\", \"Date\", \"Is\", 15, \"August\", 2019)\n", "print(tuple)" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "(\"Today's\", 'Date', 'Is', 15, 'August', 2019)\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "CR3hc5M_T8a0", "colab_type": "code", "outputId": "75460865-af29-40d4-af99-3d520fad55ae", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "tuple = [(\"Today's\", \"Date\", \"Is\", 15, \"August\", 2019), (\"Today's\", \"Date\", \"Is\", 15, \"August\", 2019)]\n", "print(tuple)" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "[(\"Today's\", 'Date', 'Is', 15, 'August', 2019), (\"Today's\", 'Date', 'Is', 15, 'August', 2019)]\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "oziSAUQ4T9s3", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "LNnzlQ9DT-Qg", "colab_type": "text" }, "source": [ "## 7) How can you loop through a tuple?\n", "\n", "This question is rather direct using a looping construct we can loop through a tuple. Below I will use a for loop and iterate through the values inside a tuple, you can similarly use other looping constructs and get the results." ] }, { "cell_type": "code", "metadata": { "id": "sUp7xaonUBH2", "colab_type": "code", "outputId": "d791f154-b301-4b41-c2fe-8e099ccf5024", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "tuple = (\"Today's\", \"Date\", \"Is\", 15, \"August\", 2019)\n", "print(tuple)" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "(\"Today's\", 'Date', 'Is', 15, 'August', 2019)\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "IchzQ00AUCha", "colab_type": "code", "outputId": "f8138ead-5e0b-41da-f162-248f4da59495", "colab": { "base_uri": "https://localhost:8080/", "height": 121 } }, "source": [ "for i in tuple:\n", " print(i)" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "Today's\n", "Date\n", "Is\n", "15\n", "August\n", "2019\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "hOVPeQLpUHDt", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "83OQeTFUUNVd", "colab_type": "text" }, "source": [ "## 8) How to access the index of the tuple using a loop?\n", "\n", "We can do this using a for loop with a enumerate function. Enumerate is a built-in function of Python. It allows us to loop over something and have an automatic counter, to know more about enumerating read its complete documentation of it [here](http://book.pythontips.com/en/latest/enumerate.html). For example:" ] }, { "cell_type": "code", "metadata": { "id": "SLfMS1rlUPyE", "colab_type": "code", "outputId": "9f6d8085-c0b8-4013-95a0-5cd8de56c5b0", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Accessing the index of the tuple using enumerate function.\n", "tuple = (\"Today's\", \"Date\", \"Is\", 15, \"August\", 2019)\n", "print(tuple)" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "(\"Today's\", 'Date', 'Is', 15, 'August', 2019)\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "CH4lnPrUURew", "colab_type": "code", "outputId": "6d97a02e-acba-43be-de9a-ec07f24e525e", "colab": { "base_uri": "https://localhost:8080/", "height": 121 } }, "source": [ "for counter, value in enumerate(tuple):\n", " print(counter, value)" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "0 Today's\n", "1 Date\n", "2 Is\n", "3 15\n", "4 August\n", "5 2019\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "za8kp3k8US-2", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "t0uEPJ4xUTj4", "colab_type": "text" }, "source": [ "## 9) Can we remove the values or items from the tuples?\n", "I think now you can answer this question easily. The answer is No, you cannot remove the values or items from the tuples but you can delete the tuple completely. This is because tuples are immutable. For example:" ] }, { "cell_type": "code", "metadata": { "id": "g63FVH2AUWRC", "colab_type": "code", "outputId": "61c93790-2a0d-4292-a7a1-a48ba67512f3", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Deleting an entire tuple using del\n", "tuple = (\"Today's\", \"Date\", \"Is\", 15, \"August\", 2019)\n", "tuple" ], "execution_count": 0, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "(\"Today's\", 'Date', 'Is', 15, 'August', 2019)" ] }, "metadata": { "tags": [] }, "execution_count": 82 } ] }, { "cell_type": "code", "metadata": { "id": "pMHd_mKGUYoy", "colab_type": "code", "outputId": "435b8b5c-f66f-4507-8174-cf1eebbfebae", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "del tuple\n", "print(tuple)" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "DZWE8BfiUVMI", "colab_type": "text" }, "source": [ "As seen above the entire tuple can be deleted using del, so when we print the tuple you can see that there are no elements in it.\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "Hgz7YsIEUbAJ", "colab_type": "text" }, "source": [ "## 10) How to count the number of times the values have appeared in the tuple?\n", "\n", "This can be done by using the count method of the tuple, the count method returns the number of times a value has appeared in a tuple. For example:" ] }, { "cell_type": "code", "metadata": { "id": "rhaaX7uGUekT", "colab_type": "code", "outputId": "293218e1-7a1d-4669-ddb4-5dcd9903d90c", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Counting the number of times a value has appeared in the tuple\n", "tuple = (\"Today's\", \"Date\", \"Is\", 15, \"August\", 2019, \"And\", \"Day\", \"Is\", \"Thursday\")\n", "print(tuple)" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "(\"Today's\", 'Date', 'Is', 15, 'August', 2019, 'And', 'Day', 'Is', 'Thursday')\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "CbE_wV8nUgav", "colab_type": "code", "outputId": "a608afc6-b522-42b1-e173-d78c28157b6c", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "print(tuple.count(\"Is\"))" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "2\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "38-P90rJUhyr", "colab_type": "code", "outputId": "f2a31de9-f344-48ec-e788-8d656f8fd9d5", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "print(tuple.count(15))" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "1\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "wqijgAvtUifK", "colab_type": "text" }, "source": [ "So in the above, the value \"Is\" has appeared 2 times and similarly, 15 has appeared 1 time.\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "reYMx25kUkqq", "colab_type": "text" }, "source": [ "## 11) How to get the index of a value in a tuple?\n", "This can be done by using the index method of the tuple, the index method searches for the first occurrence of the value, and return its position. For example:" ] }, { "cell_type": "code", "metadata": { "id": "zD6ligy0UnSi", "colab_type": "code", "outputId": "59b1afbe-67c6-4198-bd96-b165501d52bf", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Counting the number of times a value has appeared in the tuple\n", "tuple = (\"Today's\", \"Date\", \"Is\", 15, \"August\", 2019, \"And\", \"Day\", \"Is\", \"Thursday\")\n", "print(tuple)" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "(\"Today's\", 'Date', 'Is', 15, 'August', 2019, 'And', 'Day', 'Is', 'Thursday')\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "F0rAhAjPUpF5", "colab_type": "code", "outputId": "f07a44ef-f70e-4bf2-92c9-200ba5ec303b", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "print(tuple.index(\"Date\"))" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "1\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "oVkryza4Uqkl", "colab_type": "code", "outputId": "339dbd26-7c95-4139-90af-2d233fb67932", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "print(tuple.index(\"August\"))" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "4\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "E9o6Is6-Ur59", "colab_type": "code", "outputId": "1330617d-167e-4ed4-cb09-4e4e35d201f0", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "print(tuple.index(\"Is\"))" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "2\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "us9GYofcUmPo", "colab_type": "text" }, "source": [ "As seen above the index method returns the position of the value, but in the last case of \"Is\", the index method returns the first occurrence of \"Is\" in the tuple and return its position.\n", "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "1KB4xmUWUyYo", "colab_type": "text" }, "source": [ "## 13) How to check if the value is present inside a tuple or not?\n", "\n", "This can be check using the \"in\" keyword within an if statement. For example, let us check whether August is present in the tuple." ] }, { "cell_type": "code", "metadata": { "id": "-ghSRr_yU0QF", "colab_type": "code", "outputId": "87912e59-4ae3-496e-c8f3-c993fb0de9ce", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Checking for the values present in the tuple\n", "tuple = (\"Today's\", \"Date\", \"Is\", 15, \"August\", 2019)\n", "print(tuple)" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "(\"Today's\", 'Date', 'Is', 15, 'August', 2019)\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "RLCOlvthVCpg", "colab_type": "code", "outputId": "bb6052da-8305-487a-d3e2-b9537bbfd09c", "colab": { "base_uri": "https://localhost:8080/", "height": 52 } }, "source": [ "# Case 1:\n", "if \"August\" in tuple:\n", " print(True)\n", " print(tuple.index(\"August\"))\n", "else:\n", " print(False)" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "True\n", "4\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "cslZQS9FVETj", "colab_type": "code", "outputId": "a2bb28b0-4a47-48ad-d751-a2c5f98b2281", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "# Case 2:\n", "if \"September\" in tuple:\n", " print(True)\n", " print(tuple.index(\"September\"))\n", "else:\n", " print(False)" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "False\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "0_-CU2ziVF5O", "colab_type": "text" }, "source": [ "As seen above \"August\" is present in position 4 of the tuple, but whereas \"September\" is not present in the tuple.\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "Z2ZMSwdtVH03", "colab_type": "text" }, "source": [ "**Hence above are the very important techniques or function of Tuples in Python written in a question-and-answer format (It is very useful usually during programming interviews). I referred to some examples from[ Python Tuples](https://docs.python.org/3/tutorial/datastructures.html). I have written this tutorial a simple way such that everybody can understand and master the concepts of Tuples in Python without prior programming knowledge or experience. If you guys have some doubts in the code, the comment section is all yours.**" ] }, { "cell_type": "markdown", "metadata": { "id": "rqSEKZDWVM2J", "colab_type": "text" }, "source": [ "## Thank you." ] } ] }