(bright upbeat music)
- Writing code typically involves
the handling of information.
If we're making a game, we need to store the user score.
If we're creating a banking application,
we'll need to have their current balance and so forth.
A variable is simply a container for value.
When we run our programs,
the computer gives us space in its memory
where we can put data that we want to use
as a reference for later,
this data is called a variable.
Much like how we label jars in our kitchen
so we know where to go for our favorite cookies.
We declare variables in Python
by giving them a name and then setting their value.
This is called assigning the value.
And this assignment is done with the assignment operator,
which is the equal sign.
As we see here, age is the name of our variable.
And because of the equal sign
we are signing it the value of 36.
That means that somewhere in the computer's memory
it's made space for our variable and stored our value in it.
Later when we asked the computer to print
what's inside the age variable, it prints out 36,
the value that we assigned previously.
Although we're using an equal sign,
try not to think of it the same way you did
in high school algebra.
In programming, the equal sign means
I want to take the value on the right
and store it in the variable on the left.
It doesn't mean the two sides are equal.
That's important to keep in mind.
Okay, let's look at another example.
This time we've created a variable named email address,
and we've assigned it to the following value.
John.doe@example.com.
If we run this program, we can see that the values
of our variables are printed out.
Now, even though we've created both of these variables
by giving them a name, there's something different
about the value that we assign them.
Can you spot the difference?
Yes, it's that with the first value
we are only using numbers.
And for the second value
we have double quotes with letters and symbols inside.
This is because we want the variables
to represent different data types.
A data type allows us to put our variable
in a particular category so that the computer knows
how much space to give us in its memory.
And it generally knows how we plan to use our variable
later on in the program.
In this example, the 36 is an integer,
like what we're already familiar with for math class.
In case you've forgotten an integer is a whole number,
no decimal places.
And the letters and symbols inside of the double quotes
is called a string
because it's made up of a string of characters.
In fact, you can check the type of any value
or variable in Python by using the type function.
Let's take a look.
We'll print out the type of the age
and then the email address variables.
And look, the age is an int or short for integer.
And the email address is str short for string.
Depending on the programming language
that you're working with,
variables can behave in different ways.
Some languages require that you define your variables
and their types before you can use them,
like Java, C# and C++.
For example, if we want to store the name
of a cookie in Java,
we would have to first declare
that it's going to be of type string
and then give our variable a name, cookie.
When we run this code, we get sugar as our output.
Since we've defined our variable as type string,
we can't then later decide to set this variable
to a different, let's say an integer.
When we compile our program, we'll receive an error
because Java is a strict programming language.
It doesn't have that allow you
to reassign data types in this way.
But if we were to compare this
to a more relaxed language like Python,
we don't need to declare a variables type
before using it.
Here, we declare the variable and use it at the same time.
Notice how we only provide the name, not the type.
But when we run this code,
the Python and interpreter was able to figure out the type
based on the value that we provided.
What's more, if we wanted to provide a value
of a different type, Python has no issues with that either,
it just figures it out and keeps on moving.
There are pros and cons to both styles.
The more relaxed languages tend to be more flexible
and result in less code.
Whereas with more strict languages,
you have fewer surprises when the code is actually run.
If you created an account balance variable as an integer,
you don't have to worry about it later
being set to feel, for example.
Like all things in programming,
there are a few rules that we have to follow
when working with variables in Python.
First, variable names should contain only letters,
numbers, and underscores.
Even though numbers are allowed,
the name shouldn't start with a number.
3blindmice would not be a valid name for instance.
Second, spaces are not allowed in the name.
Account, space, balance would be invalid.
But account_balance would be just fine.
Third, it's important to know that names are case sensitive.
Capital C Cookies is not the same as lowercase C cookies.
Python considers them
to be two different locations in memory.
And finally, variables can not be keywords.
These are reserved words that means something special
to the Python interpreter, like, and, break or try.
If you're ever curious about the complete listing
of keywords in the Python language,
you can run the following code snippet
to get a listing of all of the keywords.
In general, you want to use short descriptive names
that make your code easy to understand.
Naming things takes practice.
In fact, it's often cited
as one of the hardest problems in computer science.
Keep that in mind and be patient with yourself
as you get used to working with variables.
(bright upbeat music)