Coding in Python: a comprehensive Beginners Guide to Learn the Realms of Coding in Python
Download 1.25 Mb. Pdf ko'rish
|
Coding in Python A Comprehensive Beginners Guide to Learn the Realms
Naming Variables
When you are naming a variable, you must adhere to a couple of guidelines and rules. If you break some of the rules, you will see errors in the code. Even if missing out on the rules does not trigger errors, breaking the rules will make the code look vague and abstract. The success of a programmer is that his code should be easy to understand and read. You must keep the following rules in mind when you are creating variables. The names of variables may contain numbers, letters, and underscores. They may start with an underscore and a letter, but they cannot start with a number. For example, a variable can be written as greetings_77 but it cannot be written as 77_greetings. You must avoid the use of Python keywords while naming a variable. For example, you cannot name a variable as print because print is a Python keyword. If you can keep the names of variables as much descriptive as possible, it will help you better read and understand the code when you come back to at a later stage. For example, greetings are better than simply writing g. Writing variable names is a continuous practice in Python. You will learn how to create better variable names as you move further into the world of coding. When you are creating variables, it is common to get errors. You should make sure that you are not misspelling different words. Take a look at the possible errors and try to learn how to avoid them and fix them. I will use the same variable greetings to show how you can end up getting an error message. >>> greetings = "This is the world of Python." >>> print(greeting) Traceback (most recent call last): File " ", line 1, in print(greeting) NameError: name 'greeting' is not defined >>> All I did was writing the wrong spelling in the code. What makes Python different from other programming languages is that it displays the error message which tells you the exact line on which you have made the error. This helps rectify the errors and clean the code. By writing the wrong spelling, I don’t suggest that Python recognizes the wrong or right spelling. I am trying to suggest that it reads the name of the variable and matches it with the command you have entered. In case of a mismatch, the error message pops up. So, even if you misspell the word while you allot a name to a variable, Python does not declare it an error. >>> greetings = "This is the world of Python." >>> print(gretings) This is the world of Python. >>> I have deliberately misspelled the word greetings but still, Python read it and ran the code. A lot of programming errors happen just because of wrong spellings. You may call them typos. They can be easily managed if you read your code with close attention. Download 1.25 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling