{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python. Mémo" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Variable, types" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### entier" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "a = 5" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "int" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### flottant" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "b = 110.2" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "float" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### chaîne de caractères" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "c = 'bonjour' #chaine de caracteres (string)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "str" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(c)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### liste" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = [5,4,8,9] # liste (tableau)\n", "type(d)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### booléen" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "e = ( 5 == 4+1 ) # booleen\n", "print(e)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "bool" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(e)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "print(not e) # negation" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "print( (4 <= 5) or (4 > 7)) # ou" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "False\n" ] } ], "source": [ "print( (4 <= 5) and (4 > 7)) # et" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Entrée" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "int convertit la saisie en un entier (sinon la saisie est une chaine)." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Saisir votre âge : 25\n" ] } ], "source": [ "age = int(input('Saisir votre âge : '))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Si ... alors ... sinon" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Vous êtes majeur\n" ] } ], "source": [ "if age < 18:\n", " print('Tu es mineur')\n", "else:\n", " print('Vous êtes majeur')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Tant que ... faire ..." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "fin\n" ] } ], "source": [ "k = 0\n", "while k < 5:\n", " print(k)\n", " k = k+1\n", "print('fin')\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Pour i de ... à ..." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n" ] } ], "source": [ "for i in range(5):\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# Listes" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[]\n", "[0, 0, 0, 0]\n", "500\n", "4\n", "2\n", "300\n" ] } ], "source": [ "L1 = [] # liste vide\n", "L2 = [0]*4\n", "L3 = [4, 500, 8, 10]\n", "L4 = [[1,2,300,4] , [5,6,7,8]]\n", "print(L1)\n", "print(L2)\n", "print(L3[1])\n", "print(len(L3))\n", "print(len(L4))\n", "print(L4[0][2])\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Fonctions" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "17" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def f(x):\n", " return x+10\n", "\n", "f(7)" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def g(a,b):\n", " if a>b :\n", " return a\n", " else:\n", " return b\n", " \n", "g(4,7)\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Calculs" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "34" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "4 + 3*10" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1024" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "2**10 # puissance" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.6666666666666667" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5/3 # division" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5//3 #division entiere (quotient de la division euclienne de 5 par 3)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "5 % 3 #reste de la division euclidienne de 5 par 3" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.4142135623730951" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from math import sqrt #racine carree\n", "sqrt(2)" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from math import floor #partie entière\n", "floor(2.35)" ] } ], "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.8.3" } }, "nbformat": 4, "nbformat_minor": 2 }