Chapter 3: Variables and What They Do in Python
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: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: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: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 beGlobal 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.
The assignment operator
We have discussed this option a bit throughout the book, but haven’t really
given it a name. The assignment operator is the equals sign or the (=). It isgoing to be used in programming to assign the value to the right of the
statement to the variable that is located t the left. Sometimes the variable will
be created first. In cases where the value on the right is from an expression,
such as an arithmetic expression, the evaluation will take place before this
assignment happens.
Keep in mind that the (=) is not going to be a mathematical sign in
programming. You can add things to the number and make all sorts of changes
that wouldn’t make sense if you thought of this sign as a mathematical one.
Rather it is an assignment operator so that the statement will be turned into the
part on the right.
When you assign the first value to this variable, you are going through the
process of initializing. The definition of a value assignment and variable are
carried out in the single step in this programming, although it is sometimes done
in two steps with some of the other programming languages. But since it is
done in one step, it is less likely that the user will make a mistake or receive an
error in the process.
Comments
Post a Comment