{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

Programmation Python de listes en compréhension

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Lorsque l'on crée une liste en compréhension, on n'ajoute pas les éléments à la liste à l'aide d'une énumération mais en les caractérisant par des propriétés mathématiques ou logiques." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "L=[i**2 for i in range(0,21)] # cette liste des premiers carrés est constituée en compréhension" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "L=[] \n", "for i in range(0,21):\n", " L.append(i**2) # Cette même liste est construite par ajout d'éléments" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "T=[i**2 for i in range(0,21) if i%2==0] # Cette liste ne contient les carrés que des nombres pairs\n" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "

Exercice 1 :

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Un article coûte 50 euros en 2020. Il augmente de 2% tous les ans pendant 10 ans.\n", "\n", "1) Construire une liste en python qui contient les prix de cet article chaque année entre 2020 et 2030." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2) Faire calculer la différence de prix entre 2020 et 2030." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Exercice 2 :

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Les cinq variables ci-contre sont des listes définies en compréhension.\n", "Pour chacune d'elle, donner ses éléments :" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "L1=[i for i in range(20)]\n", "L2=[10*i for i in range(3,8,2)]\n", "L3=[i//2 for i in range(51)]\n", "L4=[i+j for i in range(2) for j in range(3)]\n", "L5=[elet for elet in L2 if elet//10==5]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

Exercice 3 :

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compléter la liste en compréhension suivante représentant un échantillon de 500 lancers d'un dé à six faces numérotées de 1 à 6." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "import random\n", "L=[random.randint(1,6) for i in range(0,500)]" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "

Exercice 4 :

" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1) Créer les listes suivantes en compréhension :" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "H=[20,21,22,23,24,25,26,29,39,30,31,32,33,34,35,36,37,38,39,40]\n", "M=[60,63,66,69,72,75,78,81,84,87,90]\n", "N=[1,9,25,49,81,121,169]\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2) Créer les listes en compréhension suivantes :" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "a) la liste des nombres entiers naturels impairs inférieurs à 1000." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "b) la liste des nombres entiers relatifs multiples de 5 et compris entre -100 et 100 :" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "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.4.5" } }, "nbformat": 4, "nbformat_minor": 2 }