Este vídeo pertenece al curso Programmieren lernen mit Python de openHPI. ¿Quiere ver más?
An error occurred while loading the video player, or it takes a long time to initialize. You can try clearing your browser cache. Please try again later and contact the helpdesk if the problem persists.
Scroll to current position
- 00:00Welcome to the next lesson.
- 00:03In this one we want to deal with variables.
- 00:06First of all, introduce yourselves, that you have a box.
- 00:09In this box we can now pack for example a T-shirt and label them with the name clothing.
- 00:15Now, when we open the box of clothes, is in this one the T-shirt.
- 00:19But we can also take the T-shirt out of the box and put some pants in instead.
- 00:25Now if we reopen the box of clothes, the T-shirt is no longer in this one, but the pants.
- 00:30Let us look at the same example again in Python:
- 00:33We got our box back. and want to put the T-shirt inside first.
- 00:38So in Python we write clothing, then an is-equal, and then the text "T-shirt".
- 00:44We are talking about assignment in this is-alike.
- 00:48So first we have the variable name, that is, the labeling of the box, then the assignment of the
- 00:54and then the contents of the box. that is, here the value with the text "T-shirt".
- 00:59Just like in the box, we can take the T-shirt out and can put some pants in instead.
- 01:05In Python we write about it clothing is-equal and then again the text "pants."
- 01:10What is important is that we as well as in the box replace the contents.
- 01:16That means we take the T-shirt out and will not be able to access them in the further course of Python, so we overwrote it.
- 01:24But we can not only set variables in Python, but also read the value of this variable quite simply.
- 01:32For example, if we want to output the text "T-shirt", we can simply write print "T-shirt" and receive the desired edition.
- 01:40Another possibility is, however, our previously defined variable kleidung, to which we have assigned the value "T-shirt".
- 01:48So we can then write: print clothing, that is, we pass the variable kleidung to the function print.
- 01:56If we now execute the whole thing, the variable name kleidung is replaced by the value of this variable, i.e. the text "T-Shirt", and we're holding the "T-shirt" issue.
- 02:08It is important that you do not write clothing in quotation marks, because otherwise the program will think that it is a text and not the name of the variable.
- 02:17Also, once we use it, the variable is is not consumed, but can still be used.
- 02:25We can use a variable, for example, to create a second variable.
- 02:31First, the same example again:
- 02:33So we have the variable clothing with the value "T-shirt" as in line 1.
- 02:38In line 2 we then create a second variable, so a second garment, which we also assign to the value "T-shirt".
- 02:48For this we write: second_clothing = and then the name of the first variable clothing.
- 02:55So we first read the content of the variable kleidung and assign this content to the second variable named second_clothing.
- 03:05So we make a copy.
- 03:08In the third line we can then output this value and receive the T-shirt as an edition as desired.
- 03:15On the other hand, if we try to use a variable, to which we have not previously assigned a value, we get an error.
- 03:23You can imagine the whole thing like this like you're looking for a box with that name, but it doesn't exist.
- 03:29Python then gives us a NameError, a so-called name mistake.
- 03:33This means that we have tried to use a variable, to which we have previously assigned no value at all.
- 03:40There are a few rules to be observed when naming variables:
- 03:46Variables should always be written in lower case and without spaces, that is, instead of a space, simply use an underscore.
- 03:54This way you can still get an overview of a variable name as we have illustrated here by way of example.
- 04:01And please do not use umlauts like ä, ö or ü and also no special characters.
- 04:07It is also important to know that for variable names in Python upper and lower case is crucial.
- 04:15That is, the three examples of variable names, which you see here on the slide, all describe different variables.
- 04:21We will always use the first option, since this is the standard in Python, so to speak.
- 04:27It is also helpful to name the variables as meaningful as possible, so that you still know, especially in longer programs, in which variable you have stored which value.
- 04:38In this video we have shown you variables.
- 04:42These are used to store individual values in Python under a freely selectable name and retrieve it later.
- 04:51To create a variable, we write the variable name then Is-Equal and then the value such as the text "T-Shirt", which we have used several times.
- 05:00And you can change the content of a variable at any time and assign a new value.
To enable the transcript, please select a language in the video player settings menu.