2.1.4.1 Variables - data-shaped boxes
What are variables?
It seems fairly obvious that Python should allow you to encode literals carrying number and text values.
You already know that you can do some arithmetic operations with these numbers: add, subtract, etc. You'll be doing that many times.
But it's quite a normal question to ask how to store the results of these operations, in order to use them in other operations, and so on.
How do you save the intermediate results, and use them again to produce subsequent ones?
Python will help you with that. It offers special "boxes" (containers) for that purpose, and these boxes are called variables - the name itself suggests that the content of these containers can be varied in (almost) any way.
What does every Python variable have?
- a name;
- a value (the content of the container)
Let's start with the issues related to a variable's name.
Variables do not appear in a program automatically. As developer, you must decide how many and which variables to use in your programs.
You must also name them.
If you want to give a name to a variable, you must follow some strict rules:
- the name of the variable must be composed of upper-case or lower-case letters, digits, and the character
_
(underscore) - the name of the variable must begin with a letter;
- the underscore character is a letter;
- upper- and lower-case letters are treated as different (a little differently than in the real world - Alice and ALICE are the same first names, but in Python they are two different variable names, and consequently, two different variables);
- the name of the variable must not be any of Python's reserved words (the keywords - we'll explain more about this soon).
Comments
Post a Comment