To start off, we will look at the rules for naming your variables.
- All names must start with a letter
- They may contain: letters, numbers, and the underscore character ( _ )
- The names are case sensitive. count is different from Count
- There are certain keywords that cannot be used (seen later on)
- Using lowercase letters for variable names
- Using UPPERCASE letters for constants (seen later on)
- The names should be short enough to remember and must be self descriptive
As discussed above, there are certain keywords that cannot be used for variable names. They are as follows:
False | None | True | and |
---|---|---|---|
as | assert | break | class |
continue | def | del | elif |
else | except | finally | for |
from | global | if | import |
in | is | lambda | nonlocal |
not | or | pass | raise |
return | try | while | with |
yield |
As long as you stay away from these variable names you will have no problems. The next thing to cover is Variables and Constants.
These are defined as:
Variable:
- adjective. able or liable to change
- noun. a name given to a memory location designed to store a data value (Comp.Sci.)
- Called ‘variable’ as the value stored in it is likely to change during program execution
- noun. a quantity that has a fixed value throughout a set of calculations
- Called ‘constant’ as once set, the value stored does not/cannot change during program execution
Comments are a great way to describe what each part of code is doing in your program. It's beneficial to both you and anyone else who reads your code. To add comments to your code its as simple as adding the hash sign. The comment will span the entire line, when writing longer comments, it is best to split it amongst multiple lines, each starting with the hash sign.
*NOTE* When using the print function, you must use quotations when outputting a string directly. If you are outputting a variable, they will not be needed (eg. print(myVariable))
When running your program, the comments will be ignored, they are only there for your benefit and the benefit of those who read the code.
That's it for this lesson. Play around with the comments and variables and see how you go. If you have any trouble, go back through the lessons, or post a comment, I'm happy to answer any questions you may have!
Continue on to: Exercises Part 1
-Nathan (everythingPython)
No comments:
Post a Comment