All checks were successful
		
		
	
	continuous-integration/drone/push Build is passing
				
			
		
			
				
	
	
		
			222 lines
		
	
	
		
			5.9 KiB
		
	
	
	
		
			TeX
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			222 lines
		
	
	
		
			5.9 KiB
		
	
	
	
		
			TeX
		
	
	
		
			Executable File
		
	
	
	
	
| \documentclass[a4paper,10pt]{article}
 | |
| \usepackage{myXsim}
 | |
| \usepackage{minted}
 | |
| 
 | |
| \author{Benjamin Bertrand}
 | |
| \title{Python et micro:bit - Cours}
 | |
| \date{Septembre 2022}
 | |
| 
 | |
| \pagestyle{empty}
 | |
| 
 | |
| \begin{document}
 | |
| 
 | |
| \maketitle
 | |
| 
 | |
| \setcounter{section}{1}
 | |
| \section{Corpus élémentaire de Python}
 | |
| 
 | |
| \subsection{Affectation}
 | |
| 
 | |
| \begin{definition}{Affectation}
 | |
|     \textbf{L'affectation de variable}, représenté en python par le symbole \texttt{=}, est l'opération qui va associé une valeur (pas nécessairement numérique) à un nom de variable.
 | |
| \end{definition}
 | |
| 
 | |
| Pour suivre l'affectation de variable, on peut compléter un tableau d'état
 | |
| 
 | |
| \begin{multicols}{2}
 | |
|     \begin{minted}[bgcolor=base3,linenos]{python}
 | |
|        a = 23
 | |
|        a = a + 1
 | |
|        b = 10
 | |
|        a = b
 | |
|     \end{minted}
 | |
| 
 | |
|     \begin{tabular}{|c|p{2cm}|p{2cm}|}
 | |
|         \hline
 | |
|         Instruction & a & b \\
 | |
|         \hline
 | |
|         ligne 1 & 23 & \\
 | |
|         \hline
 | |
|         ligne 2 & 24 & \\
 | |
|         \hline
 | |
|         ligne 3 & 24 & 10 \\
 | |
|         \hline
 | |
|         ligne 4 & 10 & 10\\
 | |
|         \hline
 | |
|     \end{tabular}
 | |
| \end{multicols}
 | |
| 
 | |
| \subsection{Types de bases}
 | |
| 
 | |
| Vous serez amené à utiliser essentiellement 4 types de base:
 | |
| 
 | |
| \begin{center}
 | |
|     \begin{tabular}{p{2cm}*{4}{p{4cm}}}
 | |
|         Nom & Les entiers signés & Les flottants & Les strings & Les booléens \\
 | |
|         Type & int & float & str & bool \\
 | |
|         exemple &
 | |
|         \mintinline[bgcolor=base3]{python}{2, -2, 0} &
 | |
|         \mintinline[bgcolor=base3]{python}{2.2, -3.14} &
 | |
|         \mintinline[bgcolor=base3]{python}{"2", "zaer"} &
 | |
|         \mintinline[bgcolor=base3]{python}{True, False} \\
 | |
| 
 | |
|         Opérateurs &
 | |
|         \texttt{+, -, *, //, \%, **}&
 | |
|         \texttt{+, -, *, /,  **}&
 | |
|         \texttt{+}&
 | |
|         \texttt{or, and, not} \\
 | |
|     \end{tabular}
 | |
| \end{center}
 | |
| 
 | |
| \afaire{décrire l'action des opérateurs}
 | |
| 
 | |
| 
 | |
| \begin{itemize}
 | |
|     \item Pour connaitre le type d'une variable, on peut utiliser la fonction \mintinline{python}{type}
 | |
| 
 | |
|         \textbf{Exemple:}
 | |
|         \begin{center}
 | |
|             \begin{minipage}{0.5\linewidth}
 | |
|                 \begin{minted}[bgcolor=base3,linenos]{python}
 | |
| >>> a = "aer"
 | |
| >>> type(a)
 | |
| str
 | |
|                 \end{minted}
 | |
|             \end{minipage}
 | |
|         \end{center}
 | |
|     \item On peut transformer d'une variable avec les fonctions \mintinline{python}{int}, \mintinline{python}{float},\mintinline{python}{str}, \mintinline{python}{bool},
 | |
| 
 | |
|         \textbf{Exemples:}
 | |
|         \begin{multicols}{4}
 | |
|             \begin{minted}[bgcolor=base3]{python}
 | |
| >>> int("2")
 | |
| 2
 | |
| >>> int(2.2)
 | |
| 2
 | |
|             \end{minted}
 | |
| 
 | |
|             \begin{minted}[bgcolor=base3]{python}
 | |
| >>> float(2)
 | |
| 2.0
 | |
| >>> float("2.2")
 | |
| 2.2
 | |
|             \end{minted}
 | |
| 
 | |
|             \begin{minted}[bgcolor=base3]{python}
 | |
| >>> str(2)
 | |
| "2"
 | |
| >>> str(2.2)
 | |
| "2.2"
 | |
|             \end{minted}
 | |
| 
 | |
|             \begin{minted}[bgcolor=base3]{python}
 | |
| >>> bool(0)
 | |
| False
 | |
| >>> bool(1)
 | |
| True
 | |
|             \end{minted}
 | |
|         \end{multicols}
 | |
|         \end{center}
 | |
| \end{itemize}
 | |
| 
 | |
| \subsection{Outils d'interactions}
 | |
| 
 | |
| \begin{itemize}
 | |
|     \item Afficher à l'écran: on utilise la fonction \mintinline{python}{print}
 | |
|         \begin{minted}[bgcolor=base3,linenos]{python}
 | |
| print("Coucou")
 | |
| a = 2.3
 | |
| print(a)
 | |
|         \end{minted}
 | |
|     \item Demander une information à l'utilisateur: on utilise la fonction \mintinline{python}{input}
 | |
|                 \begin{minted}[bgcolor=base3,linenos]{python}
 | |
| reponse = input("Quel est ton nom?")
 | |
| print("Bonjour ", reponse)
 | |
|                 \end{minted}
 | |
|     Attention la fonction \mintinline{python}{input} renvoie toujours une chaine de caractère (même si l'on rentre un nombre).
 | |
| \end{itemize}
 | |
| 
 | |
| \subsection{Conditions}
 | |
| 
 | |
| Pour s'adapter le comportement d'un programme à différentes situations, on pourra utiliser les mots clés \mintinline{python}{if/elif/else}
 | |
| 
 | |
| \begin{multicols}{2}
 | |
| \textbf{Exemple:}
 | |
| 
 | |
|     \begin{minted}[bgcolor=base3,linenos]{python}
 | |
| age = str(input{"Quel age as tu?"})
 | |
| 
 | |
| if age < 2: # Condition 1
 | |
|     print("Tu es un bébé") # block 1
 | |
| elif age < 18: # Condition 2
 | |
|     print("Tu n'es pas majeur") # block 2
 | |
|     print("Tu peux pas passer le permis") # block2
 | |
| else:
 | |
|     print("Tu peux passer le permis") # block3
 | |
| 
 | |
| print("J'ai plus rien à dire")
 | |
|     \end{minted}
 | |
| 
 | |
| \columnbreak
 | |
| 
 | |
| Remarques:
 | |
| \begin{itemize}
 | |
|     \item Seul le mot clé \mintinline{python}{if} est obligatoire.
 | |
|     \item Il peut y avoir autant de \mintinline{python}{elif} que vous souhaitez
 | |
|     \item À la première condition validée, uniquement le block indenté en dessous sera exécuté.
 | |
|     \item Les conditions sont de type booléen (vrai ou faux). Attention, Python donne parfois des valeurs booléennes à d'autres types.
 | |
| \end{itemize}
 | |
| \end{multicols}
 | |
| 
 | |
| 
 | |
| \begin{multicols}{2}
 | |
|     \paragraph{Principaux opérateurs sur les \mintinline{python}{int} ou \mintinline{python}{float}}~
 | |
| 
 | |
|     \begin{tabular}{cc}
 | |
|         \mintinline{python}{x == y} & \cdots \\
 | |
|         \mintinline{python}{x != y} & \cdots \\
 | |
|         \mintinline{python}{x < y} & \cdots \\
 | |
|         \mintinline{python}{x > y} & \cdots \\
 | |
|         \mintinline{python}{x <= y} & \cdots \\
 | |
|         \mintinline{python}{x >= y} & \cdots \\
 | |
|     \end{tabular}
 | |
| 
 | |
|     \columnbreak
 | |
| 
 | |
|     \paragraph{Principaux opérateur sur les \mintinline{python}{bool}}~
 | |
| 
 | |
|     \begin{tabular}{cc}
 | |
|         \mintinline{python}{E and F} & \cdots \\
 | |
|         \mintinline{python}{E or F} & \cdots \\
 | |
|         \mintinline{python}{not E} & \cdots \\
 | |
|     \end{tabular}
 | |
| 
 | |
|     \vfill
 | |
| 
 | |
|     \paragraph{Principaux opérateur \mintinline{python}{str}}~
 | |
| 
 | |
|     \begin{tabular}{cc}
 | |
|         \mintinline{python}{c in S} & \cdots \\
 | |
|     \end{tabular}
 | |
| \end{multicols}
 | |
|     \afaire{Décrire la comparaison effectuée par chaque opérateur}
 | |
| 
 | |
| \paragraph{Exemples}
 | |
| \begin{center}
 | |
|     \begin{minipage}{0.5\linewidth}
 | |
|         \begin{minted}[bgcolor=base3,linenos]{python}
 | |
|             >>> 3 < 5
 | |
|             ...
 | |
|             >>> (3 < 5) and (5 == 2*2+1)
 | |
|             ...
 | |
|             >>> not (5 <= 5)
 | |
|             ...
 | |
|             >>> "a" in "uiopup"
 | |
|             ...
 | |
|          \end{minted}
 | |
|      \end{minipage}
 | |
| \end{center}
 | |
|  \afaire{Compléter l'exemple précédent avec True ou False}
 | |
| 
 | |
| \end{document}
 |