{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "id": "adbc3cfe", "metadata": { "id": "adbc3cfe" }, "source": [ "# Conjuntos [1] \n", "Un SET es un conjunto no ordenado de elementos." ] }, { "cell_type": "code", "execution_count": 1, "id": "205a5f99", "metadata": { "id": "205a5f99", "outputId": "143a551d-67b4-4e02-f404-4c3eb6498494", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "set()" ] }, "metadata": {}, "execution_count": 1 } ], "source": [ "conjunto = set() # creación de un conjunto vacío\n", "conjunto" ] }, { "cell_type": "code", "execution_count": 2, "id": "2605b16d", "metadata": { "id": "2605b16d", "outputId": "696a04c3-c7cd-46f4-e6d2-30b794628459", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "{'espadas', 'oros', 'copas', 'bastos'}\n" ] } ], "source": [ "palos = {\"oros\", \"copas\", \"bastos\", \"espadas\"} # un SET es un conjunto no ordenado de elementos\n", "print(palos)" ] }, { "cell_type": "code", "execution_count": 3, "id": "9c6b4455", "metadata": { "id": "9c6b4455", "outputId": "e61227a7-ef09-471e-8944-726fe7f6cc47", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "set" ] }, "metadata": {}, "execution_count": 3 } ], "source": [ "type(palos)" ] }, { "cell_type": "code", "execution_count": 4, "id": "598a0409", "metadata": { "id": "598a0409", "outputId": "29ae560b-71b2-4d06-f04e-03f2761ec3d6", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "True\n" ] } ], "source": [ "print(\"copas\" in palos) # True" ] }, { "cell_type": "code", "execution_count": 5, "id": "77a23e68", "metadata": { "id": "77a23e68", "outputId": "9628ec21-1f8d-4564-c7f2-e26a87ba4050", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{'bastos', 'copas', 'espadas', 'oros', 'picas'}" ] }, "metadata": {}, "execution_count": 5 } ], "source": [ "palos.add(\"picas\") # para añadir un elemento\n", "palos" ] }, { "cell_type": "code", "execution_count": 6, "id": "a6868afe", "metadata": { "id": "a6868afe", "outputId": "3cbb259a-3110-47a0-9fc0-8f955eebaf13", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{'bastos',\n", " 'copas',\n", " 'corazones',\n", " 'espadas',\n", " 'oros',\n", " 'picas',\n", " 'rombos',\n", " 'tréboles'}" ] }, "metadata": {}, "execution_count": 6 } ], "source": [ "palos.update([\"corazones\", \"tréboles\", \"rombos\"]) # para añadir varios elementos\n", "palos" ] }, { "cell_type": "code", "execution_count": 7, "id": "0b3a4b83", "metadata": { "id": "0b3a4b83", "outputId": "224cbf96-d927-416f-f313-b812b5bcee2c", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "8\n" ] } ], "source": [ "print(len(palos)) # longitud del conjunto" ] }, { "cell_type": "code", "execution_count": 8, "id": "114bb5fc", "metadata": { "id": "114bb5fc", "outputId": "b20f7118-eadc-4185-ec1d-d7d29bd410b8", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{'bastos', 'copas', 'corazones', 'espadas', 'oros', 'rombos', 'tréboles'}" ] }, "metadata": {}, "execution_count": 8 } ], "source": [ "palos.remove(\"picas\") # elimina un elemento, si no existe da error\n", "palos" ] }, { "cell_type": "code", "execution_count": 9, "id": "a61c7785", "metadata": { "id": "a61c7785", "outputId": "ddcc33d3-d759-46f2-fd34-ee759fbfb2a1", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "{'bastos', 'copas', 'corazones', 'espadas', 'oros', 'tréboles'}" ] }, "metadata": {}, "execution_count": 9 } ], "source": [ "palos.discard(\"rombos\") # elimina un elemento, si no existe no da error\n", "palos" ] }, { "cell_type": "code", "execution_count": 10, "id": "2e2b9473", "metadata": { "id": "2e2b9473", "outputId": "7ccb3176-fb07-42f3-8f49-ef59ac44005e", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "{'oros', 'tréboles', 'bastos', 'corazones', 'copas'}\n", "espadas\n" ] } ], "source": [ "carta = palos.pop() # elimina un elemento, no se sabe cuál y va cambiando\n", "print(palos)\n", "print(carta) # la variable asignada recoge el elemento eliminado" ] }, { "cell_type": "code", "execution_count": 11, "id": "262e2e02", "metadata": { "id": "262e2e02" }, "outputs": [], "source": [ "del(palos) # elimina el conjunto\n", "#print(palos) # da error ya que hemos eliminado el conjunto" ] } ], "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.8.8" }, "colab": { "name": "0260_conjuntos1.ipynb", "provenance": [], "include_colab_link": true } }, "nbformat": 4, "nbformat_minor": 5 }