Variables should not be new to you.
It is like a container for storing data.
An example is:
amount = 3
Here amount is a variable which was declared and 3 is an integer (data type) which is currently held in amount.
Since amount is not a constant, it can easily be changed.
What is a constant?
Isn't it obvious, but we would look into it later.
Declaring a variable
This is actually very simple, all you have to do is give the variable a name and assign values to it.
But don't be in a rush, there are rules to naming a variable.
1) variables must start with a letter or an underscore, this means they cannot start with a number.
2) they can only contain alpha-numeric characters and underscores.
3) pop, Pop, POP etc, are not the same (they are case sensitive)
4) certain keywords cannot be used to name a variable.
Assigning a variable
You assign a variable using the equal to sign (=).
Example is pop = 3
Constants
If you remember, I mentioned constants earlier on. They are simply variables who have values that cannot be changed.
When you want to declare a constant you do so in a module ( constant.py) and import it into the main file.
Nb: all constant variables are declared in capital letters.
Example: PI = 3.14
When you want to import the constant into main you can use the syntax below:
Import constant
No comments:
Post a Comment