{ "cells": [ { "cell_type": "markdown", "metadata": { "nbsphinx": "hidden" }, "source": [ "| [home](../index.ipynb) | [next: *print*](print.ipynb)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Les types de base\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* int, float, bool, string\n", "* utilisation des opérations arithmétiques : `+ - * / ** %`\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Type entier" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "42" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# un entier\n", "42" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# addition\n", "2 + 2" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "6" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# multiplication\n", "3 * 2" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.5" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# division\n", "5 / 2" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# modulo\n", "5 % 2" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# division entière\n", "5 // 2" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "64" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# puissance\n", "4 ** 3" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# priorité de '*/' sur '+-'\n", "5 * 2 - 1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Type flottant" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3.14" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# un flottant\n", "3.14" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5500.0" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# notation scientifique\n", "5.5e3" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.0" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# un flottant 'à valeur entière'\n", "5." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2.5" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# division\n", "5. / 2." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.0" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# puissance non entière\n", "25 ** .5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Conversion entier - flottant" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "12.0" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(12)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(5.0)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(5.2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Type booléen" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# vrai\n", "True" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# faux\n", "False" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Les opérateurs logiques `& |` sont disponibles.\n", "\n", "**Mais** on préfèrera `and or not` pour cela." ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "not False" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True and False" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "True or False" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Attention aux priorités entre opérateurs\n", "not True and False" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Les parenthèses sont là pour lever le doute ...\n", "not (True and False)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ... utilisons les !\n", "(not True) and False" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Les booléens sont typiquement le résultat d'une comparaison\n", "```\n", "== != > < <= >=\n", "```" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2 == 2" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2.0 == 2" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3 < 0" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# on peut tester un intervalle ainsi ...\n", "0 < 3 < 5" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# ... plutôt que\n", "(0 < 3) and (3 < 5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Conversion en booléen : Faux si zéro, Vrai sinon." ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(0)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(-3)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(3.14)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Conversion d'un booléen en nombre : 0 ou 1" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(True)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.0" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float(False)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# et donc ?\n", "True + 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Chaines de caractères" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'bonjour'" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# simple quote\n", "'bonjour'" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'au revoir'" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# double quote\n", "\"au revoir\"" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"j'aime les pommes\"" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# on mélange\n", "\"j'aime les pommes\"" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"j'aime les pommes\"" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'j\\'aime les pommes'" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'bonjour\\nau \"revoir\"\\nj\\'aime les pommes\\n'" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# saut de ligne\n", "'''bonjour\n", "au \"revoir\"\n", "j'aime les pommes\n", "'''" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bonjour\n", "au \"revoir\"\n", "j'aime les pommes\n", "\n" ] } ], "source": [ "# oui, on peut faire plus joli\n", "print('''bonjour\n", "au \"revoir\"\n", "j'aime les pommes\n", "''')" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "bonjour\n", "au \"revoir\"\n", "j'aime les pommes\n", "\n" ] } ], "source": [ "print('bonjour\\nau \"revoir\"\\nj\\'aime les pommes\\n')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "L'arithmétique du caractère" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'bonjour'" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# addition\n", "'bon' + 'jour'" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'toto'" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# la mutliplication\n", "'to' * 2" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'tata'" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# dans l'autre sens\n", "2 * 'ta'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Mais tout n'est pas permis" ] }, { "cell_type": "code", "execution_count": 46, "metadata": { "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "TypeError", "evalue": "can only concatenate str (not \"int\") to str", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[46], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43m1\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m+\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m2\u001b[39;49m\n", "\u001b[1;31mTypeError\u001b[0m: can only concatenate str (not \"int\") to str" ] } ], "source": [ "'1' + 2" ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "tags": [ "raises-exception" ] }, "outputs": [ { "ename": "TypeError", "evalue": "can't multiply sequence by non-int of type 'str'", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[1;32mIn[47], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mta\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mto\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\n", "\u001b[1;31mTypeError\u001b[0m: can't multiply sequence by non-int of type 'str'" ] } ], "source": [ "'ta' * 'to'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Conversion en nombre" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "-12" ] }, "execution_count": 48, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int('-12')" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-123400.0" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "float('-1.234E+5')" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(float('3.14'))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Conversion en booléen : Faux si '', Vrai sinon" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool('')" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool('0')" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool('False')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Conversion en chaîne de caractère" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'-12'" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(-12)" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'3.14'" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(3.14)" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'-123400.0'" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(-1.234E+5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercice" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Utiliser l'interpréteur interactif comme une calculette à int, float et string.\n", "\n", "(*Collectionner les erreurs pour gagner des points bonus*)" ] } ], "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.9" } }, "nbformat": 4, "nbformat_minor": 4 }