Hello everybody and welcome back to this tutorial. And what we're going to do
right now is we're going to take a look at how we can add values to our
variables. Now we already saw some of the examples in the previous video as we
actually tried to concat different strings, we used the string1, string2
and string3 variables. But right now we are going to take a look at
more examples of this stuff. So, first of all, open up your IDLE, I will open it up
right here. And to actually give a value to a certain variable we use an equal
sign. So, basically, just simply equal will give a value to a certain variable. Now
the variable you can call anything you want. In just a few seconds, or moments, we
are going to discuss how you can actually name variables. There are
different types of names that you can use, and there are different types of
name that you can't use. Of course, I will discuss all of them. But for now I'll
just name a variable named a. Now a can have a value of 5, and that is how you
simply just put a value into a certain variable. In order to check out the value
that is stored in the a, you can just type your a, it will print out what value
it has. Let me just enlarge this. We can see a has a value of 5. You can also do
something like this. So, print, we use a print function, and in between the
brackets you specify a. Now, notice that right now once I am using a print
function, I'm not really using the double or single quotes in order to define a.
That is because a is a variable and not a string, and I can simply just put it in
between the brackets without the single quotes and it will print the value
of a. Now let's actually add a value of b = 7, for example.
It doesn't really matter, it can be anything we want. We can now manipulate these
variables, such as a +b. It will give us a result of 12. a - b, it will
give us a result of -2. We can also do something like this, for example,
print, and in between the brackets a*b.
And we can see we get the result of 35, which is 5*7. So,
this is just simply using instead of the actual values, you want to use the actual
variable itself. Now variables can also be strings, it doesn't have to
be a number, as we saw in the previous video. If
c = and then a certain string between these single quotes, for example
'string'. And if you just type here c it will print out the string,
which is just simply specified between the brackets as a string. Now you can
also concat two different variables, which have to be the same type. So you
can't really add a string variable with a string variable, you can only add a
number with a number. Or, pardon me, the string variable with the number, you
can't really do that. You can only add number with number and string with
string. So if you just type here d =
this...or let's do something like this: so '<-- this is a string'.
You can use any characters you want between the single quotes, so this is a
string. And if you just type here c + d
we get the output of 'string <-- this is a string'. So, simple as that. Now let us now
discuss what or how you can actually name your variables. Sorry, everybody.
My PC crashed so I had to actually restart it
So right now let us continue from where we stopped. Let me open up my IDLE right
here. And what we were going to do right now is talk briefly about the actual
naming of the variables. Since you can't really name a variable anything you
particularly want, there are certain rules that you need to follow while
naming the variables. Now, for example, the most important thing is that you don't
name the variables with a name of the actually already used words in Python. Now
what I mean by that is don't name a variable the same as some function that
is in Python. For example, don't name a variable print. If you name it print,
basically, it will mess up with the actual print command. So, I tested it out.
If you just check out right here, print = 2, it won't print any error right
here. But if I just I try to print the print variable, it will not know what the
actual variable, what the function is, and it will say integer object is not
callable. Because right now it is referring to the print as the actual
integer number and not the function that is pre-built in the actual Python 3
itself. So even if you, for example, right now
set some variable a to be equal to 5, and wanted to print that a variable, it
will still not work. Because right now the print function doesn't even exist
anymore, it is set to be equal to 2. Therefore, the print function cannot be
used anymore to print the statement to the screen. So let me just restart this
and open the next one. And you can see right now if you don't really mess
with the print function, you can simply just print a and it will work as usual.
So, that is the most important thing, but right now let's go through the rules of
naming the variables. First of all, let us talk about
what are the correct variable names. So, for example, let's say we want to create
a variable called variable. This would be a normal, or basically a correct
variable name. So you can use a small character word as long as it is not
used in Python 3, as I said, and you can set it to be equal to 3. And you
could just print(variable), and you can see it will work. You can also use
multiple words, for example, variableOne = 5. And if you want to
print(variableOne), it's still a legit name, and it will still work as it does belong
to the actual rules that we are going to cover right now. If you want to separate
these two words you can do that, but you can only do that by specifying the
underscore between those two words. For example, if you want to name it
variable_One = 6 you can do that, and you can also print it.
It will work. One more important thing is that the variable
name can start with a dash. Now it cannot start with a number, or with a
character, or special characters such as apostrophe, and so on and so on, but,
however, it can start with a dash. So you can just type _one = 3, and you can
print(_one), it will print it out to the screen as it is a valid variable name.
The usual capital letters can also be used, for example, STRING = 'hello
world', print(STRING). Not many people will use this, but you can use a capital
letter if you want to. If you want to interact your variable name with numbers
you can do that by adding the actual number to the end of the string. So, for
example, string2 =, for example, 'hi'. And you can print(string2). So these are
all valid names for the actual variables. As we can see, we can add numbers to
the end of the string. We can also print the string in capital, pardon me,
the variable can be a capital name. The variable can also start
with a dash at the beginning of the actual
variable name. It can have a dash to separate those two words. Now, of course,
you do not need to use it to separate words, you can separate any letter if you
want to with dash. But the important thing is that the dash is something that can be
used. Also, it can come in the lower letters, and also multiple words, doesn't
even matter. Now let's talk about some of the things that you can't really do with
the variables, such as, for example, you can't name a variable
like this: so variable, pardon me, not dash, this is underscore, and this is this is dash. You
cannot name it variable-one. As you can see name 'one' is not defined. Even if
you try to set a value to it, variable-one = 3, it will say can't
assign to operator. Because this dash sign is basically referring to as a minus, which
we use in subtraction. Therefore, we get this error right here. So this is an
invalid variable name. Also, of course, you cannot name it like this: so you can't
really name a variable with two separate words. You need to have
something in between, and that something is usually the underscore, as we saw right
here. Because then the Python 3 interpreter doesn't really know this 3
to put it either in the variable or either in the one, because it is
referring to this as two different variables. Therefore, it only prints out
the one to be the invalid syntax. If you, for example, delete this one, this would
be a valid variable name. One more thing, the variable name can't start with a number,
so 5string = 3 is an invalid variable name. You cannot start a
variable name with an actual number. So, that is another thing to take note of.
And also the variables can't really be numbers, which is normal, because you
are actually adding a value to a variable. If you, for example, type here
23 = 5, you cannot store a value of 5 into 23. The Python interpreter doesn't
really know how to do that. It says right here, can't assign to literal. Because 23
is already a value, so therefore we cannot really use it as a variable name.
So, let me just say it once again. Also, you can use the characters such as the
%, the #, wherever the hash is on the keyboard, the
$. Basically, all those signs shouldn't be used in the variable names,
or they can be used in the variable names, as well as the
special characters such as apostrophe. So that would be about it for the
variable names. I hope you enjoyed this tutorial and I will see you in the next
one. Bye!