{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": { "id": "Q9t2yNzealaw" }, "source": [ "# Contar si" ], "id": "Q9t2yNzealaw" }, { "cell_type": "markdown", "metadata": { "id": "SzNKkeH9alax" }, "source": [ "Dada una lista contar los elementos que cumplen un par de condiciones." ], "id": "SzNKkeH9alax" }, { "cell_type": "code", "execution_count": 2, "metadata": { "id": "bgE-CUb3alay", "outputId": "8513ad6d-dd3a-43ed-fa8f-bbd1bd88f849", "colab": { "base_uri": "https://localhost:8080/" } }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[98, 39, 88, 72, 41, 85, 96, 25, 95, 40, 62, 50, 47, 65, 38]\n", "¿Cuántos son menores de 50 e impares? 4\n" ] } ], "source": [ "import random\n", "\n", "randomlist = random.sample(range(10, 100), 15) # lista de 15 enteros aleatorios entre 10 y 99\n", "print(randomlist)\n", "\n", "n = sum(1 for i in randomlist if i<50 and i%2==1) # lo que se suma es un generator object\n", "print('¿Cuántos son menores de 50 e impares?', n)" ], "id": "bgE-CUb3alay" } ], "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.5" }, "colab": { "name": "0480_contar_si.ipynb", "provenance": [], "include_colab_link": true } }, "nbformat": 4, "nbformat_minor": 5 }