Variables and What They Do in Python | CMD TO

The next thing that we are going to discuss are variables. Variables are
basically the labels that will denote where in your computers memory
something is going to be stored and they can also hold values. When it comes
to programming that is typed with statistics, the variables will each have a
value that is predetermined and each variable is only going to hold the value of
that type. Python has made it a bit easier because you can use one of your
variables in order to store different types.
Think about your calculator for this one. The variable is going to be like the
memory function in this calculator. It will hold onto a value so that you can
retrieve it any time that you want, but when you store in a newer value, the
older one will be erased. The only difference is that you will be able to have a
large number of variables and each of them will have different values, each of
them being referred by their own name.
With Python you will be able to define a variable by giving the label a value.
For example, you can name a variable count and have it an integer value of
one. You would show this by simply writing
count = 1
Note that with this syntax, you can assign a value to the variable with the same
name. If you try to access values in a variable that hasn’t been defined, the
Python interpreter won’t read through this. It will just exit out of the program
and give you an error.
You can choose to define a few different variables in one line, but this is not
the best practice for you to use. For example, you could do this:
# Let’s define three variables at the same time:
count, result, total = 0, 0, 0
And while that is the correct way to do things, it is much better to show it like
this:
# This is the same as:
Count = 0
Result = 0
Total = 0
It is much easier to read the second way and will ensure that the Python
program is going to understand what you want it to say.
Understanding the scope of a variable
You won’t be able to access every variable from all parts of the program and
not every variable will be the same length. The way that you defined the
variable is going to determine where and how long you will be able to access
this variable. The section of your program where you can access the variable is
going to be known as the “scope” and the time that the variable will be
available is known as the “lifetime”.
Global variables are those that are defined within the main file body and you
will be able to see these variables throughout the entire file as well as inside a
file that will be able to import the specific file. These variables have far
reaching effects and because of this, you may notice some consequences that
you didn’t notice. This is why most people won’t use global variables, or they
will use them sparingly. You should only add stuff into the global namespace if
you plan to use them globally, like with functions or classes.
On the other hand, if you define a variable inside of another variable, it will be
called a local variable. This one has the ability to be accessed from where it is
defined and will only exist when that function executes. These are only going to
be available in certain areas of the program and can’t be found or used
elsewhere.

No comments:
Post a Comment