{ "cells": [ { "cell_type": "markdown", "metadata": { "nbsphinx": "hidden" }, "source": [ "[prev: Un point sur les méthodes](methods.ipynb) | [home](../index.ipynb) | [next: *Unpacking*](unpacking.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Utilisation avancée des indices\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Indices négatifs" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "e\n", "c\n", "i\n", "d\n", "n\n", "i\n" ] } ], "source": [ "x = 'indice'\n", "\n", "print(x[-1])\n", "print(x[-2])\n", "print(x[-3])\n", "print(x[-4])\n", "print(x[-5])\n", "print(x[-6])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## A slice of ham" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Syntaxe de l'indexation par tranche:\n", "``` python\n", "conteneur[start:stop:step] # de start à stop par saut de step\n", "conteneur[start:stop] # step = 1\n", "conteneur[start:] # de start à la fin\n", "conteneur[:stop] # du début à stop\n", "```\n", "\n", "\n", "Attention :\n", "\n", "- `start` est inclue\n", "- `stop` est exclue" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x = list(range(10))\n", "x" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2, 5]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[2:8:3]" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[:4]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[4, 5, 6, 7, 8, 9]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[4:]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[::-1]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7]" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[:-2]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Affectation par slice" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['X', 1, 'X', 3, 'X', 5, 'X', 7, 'X', 9]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x[::2] = ['X'] * 5\n", "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercices" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Ecrire le corps de la fonction suivante:\n", "``` python\n", "def construire_matrice(valeurs, nb_lignes):\n", " ''' renvoie la matrice composée de `valeurs` à `nb_lignes` lignes.\n", " \n", " Une matrice est une liste de listes.\n", " `valeurs` est une liste contenant les valeurs de la matrice : d'abord la 1ère ligne, puis la seconde, ...\n", " `nb_lignes` est un entier indiquant le nombre de lignes de la matrice retournée.\n", " '''\n", "```\n", "\n", "Enrichir cette fonction avec un nouvel argument : `construire_matrice(valeurs, nb_lignes, valeurs_rangees_par_lignes=True)`" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.10.12" } }, "nbformat": 4, "nbformat_minor": 4 }