2021-2022/2nd/13_Programmation/2E_variables.ipynb

445 lines
8.4 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "2c522bc1",
"metadata": {},
"source": [
"# Étape 2: Variables, affectation et type"
]
},
{
"cell_type": "markdown",
"id": "7966e3c2",
"metadata": {},
"source": [
"### Trois types de variables\n",
"\n",
"Pour nous (humain) les trois variables qui suivent sont itdentiques"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "d366babc",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = 1\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "87873ff4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.0"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b = 1.0\n",
"b"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "98f34ab7",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'1'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c = \"1\"\n",
"c"
]
},
{
"cell_type": "markdown",
"id": "a1296b4f",
"metadata": {},
"source": [
"Mais pour Python ce sont trois choses très différentes. Le premier est un **entier**, le deuxième est un **flottant** (nombre à virgule) et le dernier est une **chaine de caractères**."
]
},
{
"cell_type": "markdown",
"id": "a65d6da0",
"metadata": {},
"source": [
"1. Trier les variables suivantes en fonction de leur type"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "86571601",
"metadata": {},
"outputs": [],
"source": [
"a = 3\n",
"b = 4.5\n",
"c = \"coucou\"\n",
"d = \"6\"\n",
"e = 8\n",
"f = 5.0\n",
"g = \"09\"\n",
"h = \"0.4\""
]
},
{
"cell_type": "raw",
"id": "708fea37",
"metadata": {},
"source": [
"Les entiers:\n",
"Les flottants:\n",
"Les chaines de caractères:"
]
},
{
"cell_type": "markdown",
"id": "1cb8c216",
"metadata": {},
"source": [
"Il est possible de transformer les types. Pour cela, Python utilise les trois fonctions suivantes\n",
"\n",
"- `int(...)`\n",
"- `str(...)`\n",
"- `float(...)`\n",
"\n",
"2. Quelques exemples d'utilisation de la fonction `int(...)`. Les exécuter."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d5643413",
"metadata": {},
"outputs": [],
"source": [
"int(a)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "8cce59f4",
"metadata": {},
"outputs": [],
"source": [
"int(b)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "67595ac0",
"metadata": {},
"outputs": [],
"source": [
"int(c)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "730d8ec6",
"metadata": {},
"outputs": [],
"source": [
"int(d)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3288e311",
"metadata": {},
"outputs": [],
"source": [
"int(g)"
]
},
{
"cell_type": "markdown",
"id": "fa9adefe",
"metadata": {},
"source": [
"En quoi la fonction `int(...)` tranforme-t-elle les variables?"
]
},
{
"cell_type": "raw",
"id": "46b49780",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"id": "8ef9fb28",
"metadata": {},
"source": [
"3. Expérimenter la fonction `float(...)`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d22272b4",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "5b822091",
"metadata": {},
"source": [
"En quoi la fonction `float(...)` tranforme-t-elle les variables?"
]
},
{
"cell_type": "raw",
"id": "2cc489e3",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"id": "5da8293d",
"metadata": {},
"source": [
"3. Expérimenter la fonction `str(...)`."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "332877f7",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "da5cd3ea",
"metadata": {},
"source": [
"En quoi la fonction `str(...)` tranforme-t-elle les variables?"
]
},
{
"cell_type": "raw",
"id": "8bcf3f94",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"id": "3f3ea6f9",
"metadata": {},
"source": [
"4. La fonction `input(...)` affiche du texte et attend un réponse."
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "afc36b4e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Comment tu t'appelles?fjdsklmq\n"
]
},
{
"data": {
"text/plain": [
"'fjdsklmq'"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"reponse = input(\"Comment tu t'appelles?\")\n",
"reponse"
]
},
{
"cell_type": "markdown",
"id": "f7e096e1",
"metadata": {},
"source": [
"Quel est le type de la variable `reponse`"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9518e549",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "7aba1ac5",
"metadata": {},
"source": [
"## Exercices\n",
"\n",
"Dans les éxercices suivants vous devrez coder un programme qui fait ce qu'il est décrit. Vous pouvez les faire dans l'ordre que vous souhaitez."
]
},
{
"cell_type": "markdown",
"id": "47fa675c",
"metadata": {},
"source": [
"### Variation d'une grandeur\n",
"\n",
"Écrire un programme qui\n",
"\n",
" demande la valeur initiale\n",
" demande le taux d'évolution\n",
" affiche la valeur finale"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d885f2a5",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "a368cdbe",
"metadata": {},
"source": [
"## Taux de variation\n",
"\n",
"Écrire un programme qui\n",
"\n",
" demande la valeur initiale\n",
" demande la valeur finale\n",
" affiche le taux d'évolution\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "05f03257",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "b540018d",
"metadata": {},
"source": [
"### Coordonnées du milieu\n",
"\n",
"Écrire un programme qui\n",
"\n",
" demande l'abscisse du point A\n",
" demande l'ordonnée du point A\n",
" demande l'abscisse du point B\n",
" demande l'ordonnée du point B\n",
" stock l'abscisse du milieu du segment [AB]\n",
" stock l'ordonnée du milieu du segment [AB]\n",
" affiche les coordonnées du milieu du segment [AB]\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4ad529dc",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "fdb3ba0c",
"metadata": {},
"source": [
"### Distance entre deux points\n",
"\n",
"Écrire un programme qui\n",
"\n",
" demande l'abscisse du point A\n",
" demande l'ordonnée du point A\n",
" demande l'abscisse du point B\n",
" demande l'ordonnée du point B\n",
" affiche la distance AB\n",
" \n",
"Pour calculer la racine carré, vous aurez besoin d'importer la fonction `sqrt`. Pour cela, votre programme devra commencer par la ligne suivante\n",
"\n",
" from math import sqrt"
]
}
],
"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.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}