{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Bonjour Alexis, il fait 12 degrés\n" ] } ], "source": [ "# string interpolation\n", "\n", "temp = 12\n", "nom = \"Alexis\"\n", "print(\"Bonjour {}, il fait {} degrés\".format(nom, temp))\n", "\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 0.3333333333333333 \n", " 0.33 \n" ] } ], "source": [ "\n", "print(\" {} \".format(1/3))\n", "print(\" {:.2f} \".format(1/3))\n" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "equipe = ['Achmed', 'Rania', 'Sofiane', 'Asma', 'Chafik']\n" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Achmed'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# premier element\n", "equipe[0]" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Rania', 'Sofiane', 'Asma']" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "equipe[1:4]" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Achmed', 'Rania', 'Sofiane']" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "equipe[:3]" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "ename": "IndexError", "evalue": "list index out of range", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mequipe\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: list index out of range" ] } ], "source": [ "equipe[5]" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Chafik'" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "equipe[-1]" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Asma'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "equipe[-2]" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": true }, "outputs": [], "source": [ "?equipe.append()" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": true }, "outputs": [], "source": [ "equipe.insert(2, \"Alexis\")" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Achmed', 'Rania', 'Alexis', 'Sofiane', 'Asma', 'Chafik']" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "equipe" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": true }, "outputs": [], "source": [ "vide = []" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "la liste est vide\n" ] } ], "source": [ "if len(vide) == 0:\n", " print(\"la liste est vide\")" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vide == []" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not vide" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "vide == False" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[5, 2]\n" ] } ], "source": [ "a = [1,2]\n", "b = a\n", "a[0] = 5\n", "print(b)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[5, 2]\n", "[1, 2]\n" ] } ], "source": [ "# avec copy\n", "\n", "a = [1,2]\n", "b = a.copy()\n", "a[0] = 5\n", "print(a)\n", "print(b)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['fait', 'beau', 'chaud', \"aujourd'hui\"]" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "phrase = \"Il fait BEAU et CHaud Aujourd'hui\"\n", "\n", "[ m.lower() for m in phrase.split(' ') if len(m) > 3 ]\n" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['fait', 'beau', 'chaud', \"aujourd'hui\"]\n" ] } ], "source": [ "mots = []\n", "liste_mots = phrase.split(' ')\n", "for m in liste_mots:\n", " if len(m) > 3:\n", " mots.append(m.lower())\n", "print(mots) " ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "fait beau chaud aujourd'hui\n" ] } ], "source": [ "liste_mots = ['fait', 'beau', 'chaud', \"aujourd'hui\"]\n", "print(' '.join(liste_mots))" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "fait-beau-chaud-aujourd'hui\n" ] } ], "source": [ "print('-'.join(liste_mots))" ] }, { "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.6.2" } }, "nbformat": 4, "nbformat_minor": 2 }