APPRENDRE LE PYTHON #2 ? LES VARIABLES

Graven - Développement
7 May 201810:50

Summary

TLDRDans cette deuxième vidéo consacrée à l'apprentissage du langage Python, Graven aborde la notion de variables, élément clé pour stocker temporairement des valeurs de divers types. Il explique les règles pour nommer ces variables, comme l'interdiction des espaces et l'utilisation de minuscules. Graven montre comment créer des variables pour stocker des chaînes de caractères, des nombres entiers et décimaux, et les booléens. Il illustre également comment afficher et modifier ces valeurs, ainsi que les concaténer pour créer des messages dynamiques. Pour conclure, il propose un exercice pratique où l'on calcule la moyenne de trois notes saisies par l'utilisateur, mettant en œuvre les concepts vus.

Takeaways

  • 🔑 Les variables en Python permettent de stocker temporairement des valeurs, qui peuvent être du texte, des nombres, etc.
  • ✏️ Pour nommer une variable, il faut respecter certaines conventions : pas d'espaces, utiliser des underscores, écrire en minuscules et éviter les caractères spéciaux.
  • 📝 Les valeurs stockées dans les variables peuvent être modifiées pendant l'exécution du programme.
  • 📈 On peut créer des variables pour stocker des chaînes de caractères (texte), des nombres entiers, des nombres décimaux et des booléens (vrai ou faux).
  • 💬 La fonction `print` permet d'afficher la valeur d'une variable dans la console.
  • 🔄 Il est possible de changer la valeur d'une variable existante en lui affectant une nouvelle valeur.
  • 🔢 Pour effectuer des calculs avec des nombres, on peut utiliser des opérateurs comme `+`, `-`, `*`, `/`, etc.
  • 🔗 La concaténation de chaînes de caractères avec des valeurs de variables est possible en utilisant le signe `+`.
  • 🛒 L'exemple de script montre comment calculer la moyenne de trois notes saisies par l'utilisateur.
  • 🛠️ Lors de l'entrée de données par l'utilisateur, il est nécessaire de convertir les chaînes de caractères en nombres entiers pour effectuer des calculs.

Q & A

  • Qu'est-ce qu'une variable en programmation Python?

    -Une variable en Python est un élément qui permet de stocker temporairement une valeur, qui peut être du texte, des nombres, des caractères, etc.

  • Comment se fait-il de commenter le code en Python?

    -Pour commenter le code en Python, il suffit d'ajouter le signe # devant chaque ligne de code.

  • Quelles sont les règles à respecter lors de la nomination d'une variable en Python?

    -Les règles incluent l'interdiction de mettre d'espaces, l'utilisation de minuscules et de soulignés (_) au lieu des espaces, et l'évitement des caractères spéciaux et des nombres au début du nom de la variable.

  • Comment créer une variable qui stocke un texte en Python?

    -Pour créer une variable qui stocke un texte, on utilise des guillemets pour entourer le texte, et on l'assigne à une variable avec un signe égal.

  • Quel est le type de données pour les nombres entiers en Python?

    -Les nombres entiers en Python sont stockés sous forme d'entiers (int).

  • Comment afficher la valeur d'une variable en Python?

    -Pour afficher la valeur d'une variable, on utilise la fonction print suivi du nom de la variable.

  • Comment modifier la valeur d'une variable existante en Python?

    -Pour modifier la valeur d'une variable existante, on réassigne une nouvelle valeur à la variable avec un signe égal.

  • Quels sont les opérateurs de base que l'on peut utiliser avec les variables en Python?

    -Les opérateurs de base en Python incluent l'addition (+), la multiplication (*), et d'autres opérateurs arithmétiques.

  • Comment faire pour concaténer des chaînes de caractères et des valeurs numériques en Python?

    -Pour concaténer des chaînes de caractères et des valeurs numériques, il faut d'abord convertir les valeurs numériques en chaînes de caractères en utilisant la fonction str.

  • Comment calculer la moyenne de trois notes en Python?

    -Pour calculer la moyenne de trois notes, on additionne les trois valeurs, on divise le résultat par trois, et on affiche la moyenne avec la fonction print.

  • Quelle est la différence entre les chaînes de caractères et les nombres en Python?

    -Les chaînes de caractères sont entourées de guillemets et peuvent contenir du texte, tandis que les nombres sont des valeurs numériques qui peuvent être des entiers (int) ou des décimaux (float).

Outlines

00:00

💻 Introduction aux variables en Python

Dans ce paragraphe, l'animateur Graven présente les bases de la programmation en Python en abordant la notion de variables. Il explique que les variables sont des éléments qui permettent de stocker temporairement des valeurs, qui peuvent être du texte ou des nombres. Les variables sont caractérisées par leur capacité à changer de valeur au cours de l'exécution d'un programme. L'animateur insiste sur l'importance de respecter certaines conventions lors de la nomination des variables, telles que l'utilisation d'un underscore pour remplacer les espaces, l'écriture en minuscules et l'évitement des caractères spéciaux. Il illustre ensuite la création de variables de différents types : chaînes de caractères, nombres entiers, décimaux et booléens. Enfin, il montre comment afficher et modifier les valeurs des variables ainsi que les utiliser dans des imprimantes pour créer des messages personnalisés.

05:05

🔢 Manipulation des variables et opérations

Dans ce deuxième paragraphe, l'animateur continue l'exploration des variables en expliquant comment changer les valeurs d'une variable existante et comment effectuer des opérations arithmétiques avec elles. Il montre comment ajouter, multiplier et même concaténer des chaînes de caractères avec des valeurs numériques, en utilisant la fonction str() pour convertir des nombres en chaînes. L'animateur crée ensuite un programme pratique pour calculer la moyenne de trois notes fournies par l'utilisateur, abordant les problèmes liés à la conversion des entrées de l'utilisateur de chaînes en nombres entiers. Il encourage les spectateurs à s'entraîner sur ce concept en réalisant un exercice où ils doivent calculer le nouveau solde d'un portefeuille après un achat.

10:08

🎬 Conclusion de la vidéo

Dans ce dernier paragraphe, l'animateur Graven conclut la vidéo en remerciant les spectateurs pour leur attention et en les encourageant à aimer, commenter et partager la vidéo. Il invite également les spectateurs à s'abonner à la chaîne pour ne pas manquer les prochaines vidéos. Le paragraphe se termine sur une note ludique avec une petite plaisanterie de Graven avant de dire au revoir, promettant de revoir les spectateurs dans une prochaine vidéo. Le paragraphe est subtitré par MAKSSOUW de la chaîne Mod Channel.

Mindmap

Keywords

💡Variables

Les variables sont des éléments qui permettent de stocker temporairement des valeurs, qui peuvent être des textes, des nombres, des caractères, etc. Elles sont essentielles en programmation car elles permettent de manipuler et de modifier des données au cours de l'exécution d'un programme. Dans le script, l'auteur introduit les variables en les décrivant comme des éléments qui peuvent changer, illustrant cela par la création de variables telles que 'username', 'age' et 'wallet'.

💡Commentaires

Les commentaires sont des lignes de code qui ne sont pas exécutées par le programme mais servent à expliquer le code pour les autres développeurs ou pour soi-même. Dans le script, l'auteur mentionne l'utilisation du signe '#' pour commenter le code, permettant ainsi de laisser des notes sans affecter l'exécution du programme.

💡Nommage des variables

Le nommage des variables est une règle importante en programmation pour assurer la compréhension et la maintenance du code. Dans le script, l'auteur explique que les noms des variables ne doivent pas contenir d'espaces, doivent être en minuscules et éviter les caractères spéciaux, soulignant l'importance de respecter ces conventions.

💡Valeurs

Les valeurs attribuées aux variables sont des données qui peuvent être modifiées pendant l'exécution d'un programme. Dans le script, l'auteur montre comment assigner des valeurs à des variables (par exemple, 'username' = 'Graven') et comment modifier ces valeurs plus tard (par exemple, 'age' = 25).

💡Types de données

Les types de données désignent les sortes d'informations qu'une variable peut stocker, comme des entiers, des décimaux ou des chaînes de caractères. Le script mentionne plusieurs types de données, notamment les chaînes de caractères ('username') et les entiers ('age'), et introduit également les nombres décimaux ('wallet') et les booléens ('is_happu').

💡Opérateurs

Les opérateurs sont des symboles qui permettent d'effectuer des opérations sur les variables et les valeurs. Dans le script, l'auteur utilise des opérateurs arithmétiques comme '+' pour ajouter des valeurs ('age' = 'age' + 1) et le modificateur d'affectation ('=') pour mettre à jour les valeurs des variables.

💡Fonction print

La fonction 'print' est utilisée en Python pour afficher des informations dans la console. Le script montre comment utiliser cette fonction pour afficher les valeurs des variables ('print(username)') et pour construire des messages en concaténant des chaînes de caractères et des valeurs de variables.

💡Conversion de types

La conversion de types est le processus de transformation d'une valeur d'un type de données à un autre. Dans le script, l'auteur explique comment convertir des chaînes de caractères en entiers (avec 'int()') et des entiers en chaînes de caractères (avec 'str()') pour effectuer des opérations ou des affichages corrects.

💡Entrée utilisateur

L'entrée utilisateur est une méthode permettant de collecter des données directement auprès de l'utilisateur lors de l'exécution d'un programme. Le script montre comment utiliser la fonction 'input()' pour demander des valeurs à l'utilisateur et les stocker dans des variables.

💡Calcul de moyenne

Le calcul de moyenne est une opération courante en programmation, souvent utilisée pour déterminer une valeur moyenne à partir de plusieurs valeurs. Dans le script, l'auteur illustre comment calculer la moyenne de trois notes en les additionnant et en divisant par trois, puis en affichant le résultat.

Highlights

Introduction to the concept of variables in Python.

Variables are elements used to temporarily store values such as text or numbers.

Variables can change during the execution of a program, hence the name 'variable'.

Commenting code in Python is done using the # symbol.

Rules for naming variables: no spaces, use underscores, lowercase letters, and avoid special characters.

Demonstration of creating a variable named 'username' with a string value.

Explanation of variable assignment using the equal sign.

Creating an 'age' variable with an integer value.

Introduction to decimal numbers and the creation of a 'wallet' variable.

Introduction to boolean variables with 'TRUE' or 'FALSE' values.

Using the 'print' function to display the value of a variable.

Changing the value of an existing variable during program execution.

Using arithmetic operators to modify variable values.

Concatenating strings with variable values to create sentences.

Error handling when concatenating strings with non-string types.

Conversion of integers to strings using the 'str' function.

Creating a program to calculate the average of three grades entered by the user.

Using the 'input' function to get user input in the console.

Converting string input to integers using the 'int' function.

Final demonstration of calculating and displaying the student's average grade.

Assignment for viewers to practice creating a program that calculates the new wallet balance after a purchase.

Encouragement for viewers to like, comment, share, and subscribe for more content.

Transcripts

play00:00

Hi everyone, this is Graven for this second episode about Python language learning

play00:04

in the first part we had seen the basics and the different prerequisites needed

play00:08

to design programs and other scripts with this language. Today I propose to attack a first notion that will deal with

play00:15

VARIABLES, a variable is what it is simply an element that allows you to temporarily store a value, it can of the text of the numbers,

play00:23

character, etc. etc. And what we can note is that these variables

play00:27

they are not fixed as their names indicate they can change, vary during the execution of the program

play00:33

Now that we've seen the theoretical part let's go to practice we'll start by commenting on the code we did in the previous video

play00:40

for that you simply need to add the sign #

play00:43

in front of each line of code. From now on they will no longer be taken into account when running the program

play00:49

to create a variable it will take place in three stages .. At first you will have to choose him a

play00:54

name, something that will help identify it

play00:57

textually, because if you call any variable abcdefg

play01:00

you will not find it in your code, so I'm going to call it user name for example

play01:06

but I do not really have the right to do that in fact when we give a name to our

play01:10

variable we will have to respect a series of rules and conventions to which developers must formalize to evolve together in the same direction

play01:18

first rule you do not have the right to put spaces

play01:20

the name of the variable must be one and the same word for that you simply need

play01:24

instead of spaces to put a small underscore (_) either on your keyboard or the dash of the 8, second rule

play01:31

the entire variable must be written in lower case so you will write username

play01:37

also you do not have the right to put special characters

play01:41

%, @, ..

play01:43

they will not be taken into account and this might not make your variable work so you can not put values

play01:49

numerical in front of the name of a variable and the top of the top it would be to write

play01:52

all of your English code instead of putting username I will rather write

play01:57

username so these are rules that you will be able to master as you go along, but it is very important to note them first.

play02:03

In the second time you will put here an equal that will not put saying that this variable will be equal to something it affects him

play02:10

a value for example I can put him Graven text so why I put it in quotation marks

play02:15

since that's what's called a string I have several characters g, r, a, v, e, n

play02:20

, .. which end to end form a string of characters which we will also call

play02:25

STRING as soon as you are going to want to write some text in your code you will have to put the text

play02:30

surrounded by these quotation marks so we will be able to comment already this first variable to say what it does, Creation of a variable 'username'

play02:38

with values, the word, Graven

play02:43

we follow with the creation of a second

play02:45

variable which this time will store an integer 50, 30, 200 so for that you will skip the lines we will create a new

play02:52

variable whose name is age and will be equal to what I wish for example me is 19 years

play02:58

you can also comment on Creating an age variable with the value 19

play03:07

It is also possible to make decimal numbers for example 42.7

play03:11

12,3 etc etc for that we will create a new variable that will be called

play03:16

wallet which will be for example the wallet of a person and it will be equal to 125.7 €

play03:21

so here it will be the wallet

play03:26

of a person and finally the last type of variables that I will introduce you, this is what we call the boolean

play03:31

it will give us two possibilities of answer is true with the value "TRUE"

play03:35

be wrong with the value "FALSE" We will come back in more detail in the next video for now I want to create a simple call is_happu

play03:43

and which will be equal to true by default, the same can be said here! You now have a small global idea

play03:49

types of variables that you will be able to use in your programs

play03:52

what i'm asking you this is going to be in keeping only two, the string of characters (username)

play03:56

and 19 which corresponds to the age we will already see how to display them and

play04:01

for that I will use the print function that I presented to you in the last video and we will start by trying to display

play04:07

username I run the program and it affects us well Graven so no difficulty at this level

play04:13

if you now want to display the age I have several possibilities

play04:17

either I redo a print and I put here

play04:19

"age", like this or so I can very well in the same PRINT, added here a comma to put a second parameters

play04:27

who will be here the age, in these cases if I run I would have Graven 19

play04:32

well now let's see how to change the value of an already existing variable

play04:36

I told you earlier that the interest of the variable is precisely that it was not fixed can be changed,

play04:41

vary during the execution of the program that is to say that by default here is equal to 19

play04:47

but we could very well give it another lower value for example here I can say that the age is now equal to 25

play04:53

so what I come to do is that as the variable age and already existing it will come to overwrite this value

play04:59

with the news that is 25, so if I do a print of the age

play05:05

I run at the very beginning the age is 19 Then I change the age value with 25 and I display it, we can see that it has updated well

play05:13

we can comment on that level

play05:15

show username and age

play05:18

Change the value of

play05:20

age with 25 finally

play05:25

poster

play05:26

the new

play05:28

value

play05:29

now how to do from this variable age that is worth 25 to have as a result

play05:35

What are we going to do technically we should say to ourselves:

play05:40

+1 "and there I'll have 26 now just add age = front and it's just going

play05:46

crushed the old age value by the new one that corresponds to the result of age + 1 be

play05:52

26, we will re-display this again and now one should have 26 at the end super

play05:58

know that it also works with other operators we can very well here for example makes age x age the age equal to 25

play06:05

so it'll be 25 x 25 and if I run the program we'll have

play06:10

625 for the history of strings it's exactly the same thing we can very well first display the value of the username

play06:17

change it meanwhile by another value for example here Youtube and re-display this value username

play06:25

And here's the result

play06:26

first we had Graven and then the value of exchanged by Youtube and that the card we will remove a little bit the print we have

play06:32

made to start on good bases and now from all the variables that we did, I propose to make a sentence

play06:38

so here we go to make a Print and put for example

play06:42

"Hi" here we will put the pseudonym of the person so we will simply added here a "+"

play06:47

that will allow to inject the value of the username, so it will put here Hi and there last value is Youtube so Hi Youtube

play06:55

we have this result, we can continue we add a +, we re-enter the game

play07:00

textual and there for example I will put: "you have" + age + "years!"

play07:05

What we just did here is what is called concatenation

play07:09

we associate hard values ​​with variables that can just change so we will test now and

play07:15

we have an error why he tells us we do not have the right to concaton in a string of elements

play07:22

which are not strings

play07:24

actually the age here is an integer so in fact it should just be converted into a string

play07:31

you're going to encompass it with a str function

play07:34

which will convert it into a string of characters. So I run and there is more this error

play07:39

Hi Youtube, you are 125 years old! so it works perfectly

play07:43

so now that we did that I propose to create a real program that averages three notes

play07:47

that have been returned beforehand by the user so we will start

play07:51

here not harvested a first note

play07:53

that for that I will create a new variable which will be called note 1 and which will be equal to the input function which will allow

play07:59

to recover a first value in console we will put in quotation marks a text which will make it possible to ask it, Enter the first note

play08:07

then we will harvest the second note

play08:10

same note 2 = input Enter the second note we continue

play08:17

note 3 = input Enter the last note and now that we have these three values ​​we will calculate the average

play08:26

of the average so I'm going to create a variable

play08:29

results that will store the result that to have this result will add up

play08:33

the note 1 + the note 2 + the note 3 and the result of this calculation so I'll encompass it in parentheses will have to

play08:41

to be divided by three to just have an average so there I will display

play08:45

the result with the function print the result is +

play08:50

result, the problem here is that we will have two major problems

play08:54

first of all what returned by the console entry it's going to be a string

play08:58

so it's not going to work since we want integers it will therefore be necessary to convert them in integer, .. For that you go

play09:05

encompass your input with an Int function that will make it possible to transform it into the same number

play09:10

and like this and the same for the last value the result or so can put here the average of the student is from

play09:18

and as here we want a string and that by default it is an integer we can do the same procedure

play09:23

but this time we want to convert it to string so string, I run the program

play09:28

Enter the first note we will put 10, Enter the second note

play09:31

20, Enter the 3rd note 14. And there, the average of the student and 14,666 ....

play09:37

so it works

play09:38

perfectly for those who want to train on this first concept I gave you a first small practical work to do for next time you

play09:45

go there if reap a value it will be a person's wallet so be careful to give it a correct name

play09:52

then you will create a product worth 50 euros and I want you to post the new

play10:00

value of the purse of the person who bought, after his purchase

play10:04

you will have in descriptions the corrected of this small practical work

play10:08

as well as a quiz for those who wish to train on this first notion so here is a little bit what I wanted to show you today in

play10:14

Anyway I hope you enjoyed this video, if so do not hesitate as usual at Liker, comment and Share

play10:19

subscribe to the channel so you do not miss the next videos

play10:22

in any case it was Graven! Hi everyone and see you next time! eh eh

play10:27

Subtitling: MAKSSOUW (Mod Channel)

Rate This

5.0 / 5 (0 votes)

Related Tags
PythonProgrammationVariablesDébutantsApprentissageTutorielCodeDéveloppementLogiciel
Do you need a summary in English?