Important Rules while Creating Variables
Programming languages understand instructions only if they are given in a specified format (or syntax). When creating variables, we need to be careful of these rules, or the variables will not be created properly, and the program may give errors.
- Variable names can be created using
alphabetical letters
(a-z and A-Z),digits
(0-9), orunderscore
symbol (_). Special symbols are not allowed. For example,abc&def
is an invalid variable name. - Variable names can begin with the underscore symbol or alphabetical letters only, not digits. For example,
123abc
is an invalid variable name and will give you an error. - Variables in Python are
case-sensitive
. This means that if we declare two variablesscore
andScore
with different values assigned to them, then they are two different variables (not the same variable!). - Every programming language has certain
keywords
, that define in-built functionality in the language. These should not be used as variable names. Since it has a different meaning in the language, declaring variables with already existing keyword names will cause an error. These keywords will be highlighted with a different color whenever you type them, so it is easy for you to distinguish. For example,and
is a keyword in Python, hence declaring a variable with that name would raise an error. - In Python, it is important to assign a value to a variable. If you only define the variable without assigning it a value, it will produce an error.
Now that we know how to create variables, let's see if we understood them properly.