{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Types de données simples" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Nombres entiers" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = 1\n", "type(a)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a+1" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "b = 2" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a+b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Nombres en virgule flottante" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.5" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a/2" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(a/2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "division entière :" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a//2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Booléens" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a == b" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a != b" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "bool" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(a>0)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Cool!\n" ] } ], "source": [ "if True:\n", " print('Cool!')" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "z = a==b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Comment faire des contrôles sur le type ?" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(z) == bool" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "isinstance(z, bool)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Chaînes de caractères" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "b = \"ceci est une chaîne de caractères\"" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ceci est une chaîne de caractères'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(b)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "c = \"\"\"\n", "Ceci est une châine de caractères\n", "sur plusieurs lignes\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'\\nCeci est une châine de caractères\\nsur plusieurs lignes\\n'" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Ceci est une châine de caractères\n", "sur plusieurs lignes\n", "\n" ] } ], "source": [ "print(c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Opérations sur les chaînes de caractères" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ceci est une chaîne de caractères !'" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b + \" !\"" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'!!!'" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"!\"*3" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'!!!'" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "3*\"!\"" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "33" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(b)" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['__add__',\n", " '__class__',\n", " '__contains__',\n", " '__delattr__',\n", " '__dir__',\n", " '__doc__',\n", " '__eq__',\n", " '__format__',\n", " '__ge__',\n", " '__getattribute__',\n", " '__getitem__',\n", " '__getnewargs__',\n", " '__gt__',\n", " '__hash__',\n", " '__init__',\n", " '__init_subclass__',\n", " '__iter__',\n", " '__le__',\n", " '__len__',\n", " '__lt__',\n", " '__mod__',\n", " '__mul__',\n", " '__ne__',\n", " '__new__',\n", " '__reduce__',\n", " '__reduce_ex__',\n", " '__repr__',\n", " '__rmod__',\n", " '__rmul__',\n", " '__setattr__',\n", " '__sizeof__',\n", " '__str__',\n", " '__subclasshook__',\n", " 'capitalize',\n", " 'casefold',\n", " 'center',\n", " 'count',\n", " 'encode',\n", " 'endswith',\n", " 'expandtabs',\n", " 'find',\n", " 'format',\n", " 'format_map',\n", " 'index',\n", " 'isalnum',\n", " 'isalpha',\n", " 'isascii',\n", " 'isdecimal',\n", " 'isdigit',\n", " 'isidentifier',\n", " 'islower',\n", " 'isnumeric',\n", " 'isprintable',\n", " 'isspace',\n", " 'istitle',\n", " 'isupper',\n", " 'join',\n", " 'ljust',\n", " 'lower',\n", " 'lstrip',\n", " 'maketrans',\n", " 'partition',\n", " 'replace',\n", " 'rfind',\n", " 'rindex',\n", " 'rjust',\n", " 'rpartition',\n", " 'rsplit',\n", " 'rstrip',\n", " 'split',\n", " 'splitlines',\n", " 'startswith',\n", " 'strip',\n", " 'swapcase',\n", " 'title',\n", " 'translate',\n", " 'upper',\n", " 'zfill']" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(b)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'CECI EST UNE CHAÎNE DE CARACTÈRES'" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.upper()" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['ceci', 'est', 'une', 'chaîne', 'de', 'caractères']" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b.split()" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'ceci, est, une, chaîne, de, caractères'" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\", \".join( b.split() )" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Formatage de chaînes de caractères" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Oui, ceci est une chaîne de caractères!'" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Oui, %s!\" % (b) # very old style" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Oui, ceci est une chaîne de caractères!'" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"Oui, {0}!\".format(b) # old style, Python 2.6+" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Oui, ceci est une chaîne de caractères!'" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\"{0}, {1}!\".format(\"Oui\", b) " ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Oui, ceci est une chaîne de caractères!'" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f\"Oui, {b}!\" # new style, Python 3.6+" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "my_num = 1.23456" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Voici un numéro avec 3 chiffres décimaux: 1.235'" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f\"Voici un numéro avec 3 chiffres décimaux: {my_num:.3f}\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Pour plus d'info, cf. https://pyformat.info/, https://realpython.com/python-f-strings/" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Conversions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "N.B. : typage dynamique **fort**" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "unsupported operand type(s) for +: 'int' and 'str'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m<ipython-input-35-bd58363a63fc>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: unsupported operand type(s) for +: 'int' and 'str'" ] } ], "source": [ "a + b" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'1 est une chaîne de caractère'" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "str(a) + \" est une chaîne de caractère\"" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "c = \"10\"" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "11" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "int(c) + 1" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(0)" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(1)" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 41, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(\"test\")" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(3.14)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bool(0.0)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "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.7.5" } }, "nbformat": 4, "nbformat_minor": 2 }