{ "cells": [ { "cell_type": "markdown", "metadata": { "nbsphinx": "hidden" }, "source": [ "[prev: Utilisation avancée des indices](indexing-tricks.ipynb) | [home](../index.ipynb) | [next: Le retour des fonctions](function-arguments.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Unpacking\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "mes_donnees = ('Mickey', 'Mouse')" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('Mouse', 'Mickey')" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ok mais un fastidieux\n", "prenom = mes_donnees[0]\n", "nom = mes_donnees[1]\n", "\n", "nom, prenom" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "('Mouse', 'Mickey')" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# mieux : unpacking !\n", "prenom, nom = mes_donnees\n", "\n", "nom, prenom" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "un\n", "deux\n", "trois\n" ] } ], "source": [ "a, b, c = 'un', 'deux', 'trois'\n", "\n", "print(a)\n", "print(b)\n", "print(c)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2 3 5 8 13 21 34 55 89 144 " ] } ], "source": [ "# échanger deux variables\n", "\n", "# calcul de la suite de Fibonacci\n", "a, b = 0, 1\n", "while b < 100:\n", " a, b = b, a+b\n", " print(b, end=' ')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercices" ] } ], "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 }