APPRENDRE LE PYTHON #2 ? LES VARIABLES
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
💻 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.
🔢 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.
🎬 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
💡Commentaires
💡Nommage des variables
💡Valeurs
💡Types de données
💡Opérateurs
💡Fonction print
💡Conversion de types
💡Entrée utilisateur
💡Calcul de moyenne
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
Hi everyone, this is Graven for this second episode about Python language learning
in the first part we had seen the basics and the different prerequisites needed
to design programs and other scripts with this language. Today I propose to attack a first notion that will deal with
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,
character, etc. etc. And what we can note is that these variables
they are not fixed as their names indicate they can change, vary during the execution of the program
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
for that you simply need to add the sign #
in front of each line of code. From now on they will no longer be taken into account when running the program
to create a variable it will take place in three stages .. At first you will have to choose him a
name, something that will help identify it
textually, because if you call any variable abcdefg
you will not find it in your code, so I'm going to call it user name for example
but I do not really have the right to do that in fact when we give a name to our
variable we will have to respect a series of rules and conventions to which developers must formalize to evolve together in the same direction
first rule you do not have the right to put spaces
the name of the variable must be one and the same word for that you simply need
instead of spaces to put a small underscore (_) either on your keyboard or the dash of the 8, second rule
the entire variable must be written in lower case so you will write username
also you do not have the right to put special characters
%, @, ..
they will not be taken into account and this might not make your variable work so you can not put values
numerical in front of the name of a variable and the top of the top it would be to write
all of your English code instead of putting username I will rather write
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.
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
a value for example I can put him Graven text so why I put it in quotation marks
since that's what's called a string I have several characters g, r, a, v, e, n
, .. which end to end form a string of characters which we will also call
STRING as soon as you are going to want to write some text in your code you will have to put the text
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'
with values, the word, Graven
we follow with the creation of a second
variable which this time will store an integer 50, 30, 200 so for that you will skip the lines we will create a new
variable whose name is age and will be equal to what I wish for example me is 19 years
you can also comment on Creating an age variable with the value 19
It is also possible to make decimal numbers for example 42.7
12,3 etc etc for that we will create a new variable that will be called
wallet which will be for example the wallet of a person and it will be equal to 125.7 €
so here it will be the wallet
of a person and finally the last type of variables that I will introduce you, this is what we call the boolean
it will give us two possibilities of answer is true with the value "TRUE"
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
and which will be equal to true by default, the same can be said here! You now have a small global idea
types of variables that you will be able to use in your programs
what i'm asking you this is going to be in keeping only two, the string of characters (username)
and 19 which corresponds to the age we will already see how to display them and
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
username I run the program and it affects us well Graven so no difficulty at this level
if you now want to display the age I have several possibilities
either I redo a print and I put here
"age", like this or so I can very well in the same PRINT, added here a comma to put a second parameters
who will be here the age, in these cases if I run I would have Graven 19
well now let's see how to change the value of an already existing variable
I told you earlier that the interest of the variable is precisely that it was not fixed can be changed,
vary during the execution of the program that is to say that by default here is equal to 19
but we could very well give it another lower value for example here I can say that the age is now equal to 25
so what I come to do is that as the variable age and already existing it will come to overwrite this value
with the news that is 25, so if I do a print of the age
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
we can comment on that level
show username and age
Change the value of
age with 25 finally
poster
the new
value
now how to do from this variable age that is worth 25 to have as a result
What are we going to do technically we should say to ourselves:
+1 "and there I'll have 26 now just add age = front and it's just going
crushed the old age value by the new one that corresponds to the result of age + 1 be
26, we will re-display this again and now one should have 26 at the end super
know that it also works with other operators we can very well here for example makes age x age the age equal to 25
so it'll be 25 x 25 and if I run the program we'll have
625 for the history of strings it's exactly the same thing we can very well first display the value of the username
change it meanwhile by another value for example here Youtube and re-display this value username
And here's the result
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
made to start on good bases and now from all the variables that we did, I propose to make a sentence
so here we go to make a Print and put for example
"Hi" here we will put the pseudonym of the person so we will simply added here a "+"
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
we have this result, we can continue we add a +, we re-enter the game
textual and there for example I will put: "you have" + age + "years!"
What we just did here is what is called concatenation
we associate hard values with variables that can just change so we will test now and
we have an error why he tells us we do not have the right to concaton in a string of elements
which are not strings
actually the age here is an integer so in fact it should just be converted into a string
you're going to encompass it with a str function
which will convert it into a string of characters. So I run and there is more this error
Hi Youtube, you are 125 years old! so it works perfectly
so now that we did that I propose to create a real program that averages three notes
that have been returned beforehand by the user so we will start
here not harvested a first note
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
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
then we will harvest the second note
same note 2 = input Enter the second note we continue
note 3 = input Enter the last note and now that we have these three values we will calculate the average
of the average so I'm going to create a variable
results that will store the result that to have this result will add up
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
to be divided by three to just have an average so there I will display
the result with the function print the result is +
result, the problem here is that we will have two major problems
first of all what returned by the console entry it's going to be a string
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
encompass your input with an Int function that will make it possible to transform it into the same number
and like this and the same for the last value the result or so can put here the average of the student is from
and as here we want a string and that by default it is an integer we can do the same procedure
but this time we want to convert it to string so string, I run the program
Enter the first note we will put 10, Enter the second note
20, Enter the 3rd note 14. And there, the average of the student and 14,666 ....
so it works
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
go there if reap a value it will be a person's wallet so be careful to give it a correct name
then you will create a product worth 50 euros and I want you to post the new
value of the purse of the person who bought, after his purchase
you will have in descriptions the corrected of this small practical work
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
Anyway I hope you enjoyed this video, if so do not hesitate as usual at Liker, comment and Share
subscribe to the channel so you do not miss the next videos
in any case it was Graven! Hi everyone and see you next time! eh eh
Subtitling: MAKSSOUW (Mod Channel)
Ver Más Videos Relacionados
Python : les nombres
Les variables - PYTHON - 1ERE - Spé NSI
Fractions décimales et nombres décimaux CM1 - CM2 - 6ème - Cycle 3 - Maths
LE COURS : Les puissances - Quatrième - Troisième
Encadrer une fraction entre deux entiers consécutifs CM1 - CM2 - 6ème Cycle 3 - Mathématiques Maths
Notion tutoriel : Organiser ses vidéos Youtube et retenir l'essentiel grâce à Notion ▶️🏷️
5.0 / 5 (0 votes)