What are variables in Python?
Variables in Python can be thought of as containers for storing data values. The value of the data stored in variables, i.e., the value of variables, can change depending upon the program. The variables are created when the values are assigned to them. But note that the keywords can never be used as a variable name. Get the list of keywords here. If you do so, will cause an error in the program that you have made. See the below video to get a better understanding:In the above example, you can see that 'and' and 'or' are the reserved keywords. After using them as a variable, caused an error in the program.
The syntax for assigning a variable to the variable is:
variable_name = variable_value
When you're writing a variable name that has more 1 word, make sure that you put an underscore (_) between the gaps of words. If you don't put an underscore between the gaps, it will cause an error in the program. Check the below video for better understanding:
Text can also be assigned to the variable by enclosing them in double ("") or single ('') quotes. Example,
Do note that the variable name are case sensitive. Example, the variable name Hello differs from HELLO. If you don't name the variable name correctly, you'll end up creating another variable. See the below video to understand better:
Conclusion
Summing up all these exceptions, there are few rules to not while naming a variable. Rules for naming a variable:Rule 1: The name of a variable must start with a letter or an underscore (_).
Example: _hello, Hello, HELLO etc. are accepted. But variable names like 24X, #name, (Name etc. are not accepted.
Rule 2: The variables names can only have alphanumerical characters and underscores.
Example: hello, h_e_l_l_o, hello123, B_2_2 etc. are accepted. But variable names like AG#E, qwerty@ etc. are not accepted.
Rule 3: The variable names are case sensitive.
Example: The variable name age differs from the variable name AGE.
Rule 4: You can't use a reserved keyword for naming a variable.
No comments:
Post a Comment