Why can't you declare variables that start with a number in Python?
Example:
prueba1 = "hola mundo" #bien
1prueba = "hola mundo" #mal
Why can't you declare variables that start with a number in Python?
Example:
prueba1 = "hola mundo" #bien
1prueba = "hola mundo" #mal
Almost no programming language [*] supports identifiers (variable names) that start with numeric digits.
As they explain here , accepting such identifiers would introduce ambiguities in the syntax.
For example, it would not be clear if
x = 1600L
we refer to a named variable
1600L
, or a numeric literal with theL
(long) suffix. Worse still, if we accept identifiers consisting only of digits, it would be impossible to distinguish them from numeric literals.Of course one could introduce restrictions, like "we accept identifiers starting with digits if they contain at least one letter and if they cannot be confused with a numeric literal..." but this would be very ugly and fragile. The simplest and most elegant thing is to agree to prohibit them from entering.
[*] The only exception I know of is Perl - because in this language variables are distinguished by a prefix ("sigil") like
$
. Thus, there is no danger of ambiguity.[**] The restriction is present even in element identifiers in HTML4 ( attribute
id
), although in this case it is more difficult to justify it. In fact, in HTML5 the restriction was lifted.What you comment is not exclusive to Python, in almost all languages it is not allowed to declare a variable that begins with a digit. And it has a logical explanation: it is to avoid creating a variable name that is the same as a number.
If a variable is allowed to start with a digit, it could be the case that there is a variable whose name consists of only digits. How would you differentiate it then from the integer that is written the same? For example (C notation):
Does that last one
1
(the one inint b = a + 1;
), refer to the number 1 or the variable1
? What would then be the value ofb
: 13 or 14?And even if the variable name weren't just digits, there are cases where it could match a number depending on the different formats the language allows. For example, hexadecimals: 0x60 is equal to 96 in decimal.
As our colleague Alvaro Montoro comments in his answer, this is not something exclusive to Python, but practically no programming language supports the use of numbers at the beginning of an identifier.
Why is this? Well, let's look at it from an analytical point of view:
Let's imagine that we declare the following variable, in Python :
This would cause a syntax error in the whole language and ask to return to the beginning, imagine the same example in C:
It is another one that will explode the moment it is compiled.
Looking at it then from the logical point of view, it is where the answers are obtained.
Let's ask the question again, why can't you? Now yes, the answer is simple:
Programming languages normally meet 3 stages (the most summarized):
It is in the Lexical Analysis where all the words and symbols written in your source code file are converted into understandable units for the compiler, let's take the input in python again:
When parsed by the tokenizer, we would get the following results:
And it is right at the input of the semantic analysis process where it says that your input does not meet any rule defined in the language (Read: Backus-Naur Form) .
Having said all this, the best way to understand an identifier is the following (at least for me) :
An identifier can start with an underscore or a letter, after the first character, it can contain letters, numbers or underscores.
Translated:
Although the rule defined above is not necessarily applied to all programming languages.
You can call it a convention : any literal that starts with a number is treated as a number . By following this agreement you avoid falling into ambiguity in the following cases:
1e10
number with exponent1j
imaginary number0x10
number in hexadecimal0o10
octal number0b10
binary number20L
integer long (python2)