[No audio]
Hi, welcome back.
Please take a look at this program in here.
Do you see any issue with this program?
[No audio]
The program works well.
If we run it, we get 7
as output, which is the expected output.
So 3 plus 4 gives you 7.
But there is an issue with the program,
and the issue is that we have no
idea what 3 means and what 4 means.
So as programmers, we don't know what's going on
in here and that makes this program not readable.
Now let me show you how I will
fix that issue right away by using variables.
So I'll delete this entirely and say spent = 3,
[No audio]
donated = 4,
[No audio]
total_amount = spent + donated, and then print total_amount.
If I execute this program now,
I will get the same output.
So it's 7 again.
But this time we as
programmers can understand this program.
So we can give this program to some other
persons, some other programmers, or we can go back
to this program later and we can still understand
this program because now we have names for the
values and these are referred to as variables.
So spent, donated, and total_amount are all variables.
The syntax is pretty simple.
So all you have to do is just pick
a name from your mind for a variable.
So I chose to have spend as the first variable.
So this is the amount of money I spent for
purchasing things, for example, and then this is the amount
I donated and then I calculated the total_amount.
So now this program makes more sense.
And of course there are other examples such as
items = 10 and price = 2.
total_price = items * price.
So now there is this asterisk here, which
means multiplication operator.
[No audio]
print(total_price) and we
get 20 as output in here.
So again, we have one variable here, two and three.
[No audio]
Now, there are some rules regarding
to the syntax of variables.
The first rule is that you shouldn't have numbers
in front of your variables, but you can have
numbers in the middle somewhere or at the end,
but not in front of them.
You can use underscores, such as I did
in here, but you cannot use spaces.
So this will not work in Python, which
means that if you have variables with more
than one word, you should use an underscore.
It's the best practice to use an underscore.
And lastly, you can have as many spaces
as you want around the assignment operator.
So that will work, but it is recommended, according
to the official Python style guide, it is recommended
to have one space on both sides of the
assignment operator to make the code more readable.
So spaces serve only the purpose of readability here.
The same goes for these operators.
It's good to have spaces around
the operator for readability again.
So now our program makes more sense.
We know what's going on in here.
We understand the code.
And that is one benefit of variables.
There is a second benefit as well.
Variables serve as buckets to carry data or objects.
So 10 is a data value, or you
can also refer to it as an object.
So 10 is an object, 2 is an object
and items is the name of this object,
or you can say the variable of this object.
And so these names, these variables are like buckets.
They are used to carry objects around.
For example, let's say we placed
10 inside the items bucket,
and then we get that bucket and multiply that
bucket with a price bucket, which has 2 inside.
So instead of writing 10 in here and 2, we
actually work on a bucket basis, on a variable basis.
There is a benefit for this because, for example, let's say
we decide to change the value of this to 3, and
that means items now here will also be 3.
So 3 times 2 we're going to get 6.
That's why we don't want to have objects hard coded.
For example, if this was 10, we run
this, we would get the old output 20.
So I hope that is clear.
Always use variables.
And that concludes this lecture about variables. I did go
into a lot of details because variables are used
in every single program that you will write.
So make sure you understand them and I'll
talk to you in the next video. Thank you.