Each language has grammar and vocabulary. We follow these grammar rules to make meaningful sentences. In English, a period (.) is used to denote the end of the sentence. Similarly, you have to learn the syntax of programming languages.
In the previous examples, we used space in the variable names. While it was a good start for basic understanding, spaces are not allowed in variable names in python programs. When we declare a variable, we need to follow certain rules (consider it a programming rule of grammar). If you don't follow these rules, your program won't run – the Python compiler won't understand what you're saying and will produce an error.
1. It can contain only Latin characters (English uppercase or lowercase characters), numbers, and the underscore character ( _) Examples: myName, my_name, MYNAME, Week2, etc.
2. Names are case-sensitive, which means there is a distinct difference between uppercase and lowercase characters. For example, X and x are two different variables. Similarly, myName, myname, MyName, Myname are 4 different variables.
3. No space characters are allowed. To give a readable and meaningful name, we can separate words by _ (underscore)
4. A valid variable or constant name can start with a letter or an underscore. Numbers are allowed, but they cannot be used at the beginning of the name. _name, name, name2 are valid but 2name is not.
No comments:
Post a Comment