Modul 3 von 11 · Text
Creating variables
Creating variables
What can you put inside a variable?
Anything.
You can use a variable to store any value of any of the already presented kinds, and many more of the ones we haven't shown you yet.
The value of a variable is what you have to put into it. It can vary as often as you need or want. It can be an integer one moment, and a float a moment later, eventually becoming a string.
Let's talk now about two important things p how variables are created, and how to put values inside them (or rather - how to give or pass values to them).
REMEMBER
A variable comes into existence as a result of assigning a value to it. Unlike in other languages, you don't need to declare it in any special way.
If you assign any value to a nonexistent variable, the variable will be automatically created. You don't need to do anything else.
The creation (or otherwise -its syntax) is extremely simple: just use the name of the desired variable, then the equal sign (=) and the value you want to put into the variable.
You can put anything inside a variable.
Take a look at the snippet:
var = 1
print(var)
It consists of two simple instructions:
- The first of them creates a variable named var, and assigns a literal with an integer value equal to 1.
- The second prints the value of the newly created variable to the console.
Note: print() has yet another side to it - it can handle variables too. Do you know what the output of the snippet will be?
1