-: Hi guys and welcome to your first video
in this Python refresher.
In this video, let's talk about variables.
A variable in Python is a name for a value.
You can think of them as similar to those in algebra,
but there are slight minor differences,
that you'll understand as you programme more in Python.
Here's how we define a variable.
You can define a variable name, for example x,
and then you type the equal sign
and finally you type the value, for example, 15.
And what Python is doing here
and this is very important,
is it's creating this value that's after the equal first.
So here Python sees the value 15
and says okay, you're gonna use the value 15,
I will create that or store that somewhere
and then it's gonna say, and what am I going to
refer to that value as, and that's gonna be x.
So the right side of this assignment,
this equal sign happens first,
and the left side happens later.
Outside this point, there are few values in Python
where Python has already created them
before it runs your code.
So Python doesn't create these values when you use them,
but if you have something like 1,500
then Python will do what I just said,
it will create this value and store it in its memory
and then actually become a name for this value just created.
As well as integers,
which are whole numbers like we've got here,
you can define variables such as 9.99.
And here what I've done is I've created the value 9.99
which is a float value, or a value with a floating point
and then told Python to give that value
the name of price.
Let's say we've got a variable called discount
that equal to 0.2.
We can then say that the final result of our price
when the discount is applied
would be the price multiplied by one minus the discount.
And here's what happens when you execute this.
Of course following the rules of mathematics,
which Python does, this gets evaluated first,
since it's between brackets and you get 0.8, one minus 0.2.
Then you're gonna multiply 9.99 by 0.8
and that value will get calculated
and stored in Python's memory.
Whatever that is, result will be the name for that value
that it will refer to from here.
If you wanted to print a value out
and show it to the user,
you can very easily do that with the print function.
So here we will type print
and then inside brackets we will type result.
The brackets in here are not the same as they are out here.
Here they were used for order of preference
in mathematical expressions
and here they're used to signal to the print function
what we want to print.
And this is fairly common in Python.
Whenever you see a name, such as print,
followed by these brackets,
what you've got is a function.
And a function in Python is something that
performs an action such as showing something to the user,
or it calculates an output, based on some inputs.
Or it can do both.
We're gonna look more at functions in a coming video.
But this is how you'd show the user
something in the console.
So at this point, I'm gonna save this file
and I'm going to run it.
And you can see that the output is 7.992.
So this output here is what this line eight,
this print function will calculate.
Notice that we've got a number of other variables up here
but we are not printing them out,
so they will never be shown.
As well as integers and floats,
Python has another data type called the string.
Integers and floats are useful in mathematics
or for calculating things,
whereas strings are used to store characters,
for example a person's name, a person's data of birth,
a person's phone number, so on.
Even though some of them do contain numbers,
they're not gonna be used for mathematics,
so we can store them in each string, each set.
Let's say that we wanted the person's name to be stored.
What we will do is we will same name equal,
and then inside quotation marks we will type Rolf.
What Python does is it creates this string first
and then this name here because a name for that string.
Notice that we can reassign a name,
so that we can do something like this if you'd like.
And now name, instead of being a name for Rolf,
will be a name for Bob.
So no two variables are created here,
it's just the same variable,
but we have essentially moved it.
You can of course print this out if you want.
And I will save this file and run it.
And you'll see down here that we get Rolf printed out
because that is the contents of our string.
Note that Python uses the quotation marks
to signal a string,
but these quotations marks themselves
are not part of the contents,
they're just used as delimiters.
Instead of double quotation marks,
you can use single quotation marks if you like
and that is totally fine as long as
you don't mix and match them.
If you mix and match them,
then that won't work and Python will complain.
I recommend you always stick to
one same type of quotation marks,
and I generally use double quotation marks.
What happens if we print the name multiplied by two,
which is how you would multiply any number?
When you run this you'll See that the output is now Rolf,
because that's line three,
and then you get RolfRolf for line four.
What's happened here is this has essentially done
name plus name, it has added two names together.
That's the same meaning as the multiplication.
And when you add two strings together
what Python does is it will join them
and it will add one after another
so you can end up with a longer string.
This can be very useful at times,
but normally is not what you want
when you multiply a string by a number.
Let's say you have two variables,
a equals 25 and b equals eight.
I mentioned earlier that 25 is special value,
because it is under 255,
so Python creates it as soon as Python starts up.
If it was a larger value, then at this point,
Python would create it.
So we get the value 25 and we say that
a is a name for that value.
From here on we can refer to a,
and really we'll be referring to the value 25.
So what happens when b equals a?
Well Python is gonna evaluate a
and is gonna say okay, a is a name for 25,
so we're gonna use 25 here instead.
So this means the same thing as this.
And then b is now a name for a,
which really means that b is a name for 25.
So both a and b are names for the number 25.
So you can verify that that's correct
by printing them both out.
So I'm gonna print a and then print b,
and you'll see that we get 25 and then 25,
which is perfect.
But what happens if we do b equal 17?
Should a b 17 as well?
Well let's run that.
And now down here you get 25 and 25 for lines four and five
and then you get 25 and 17 for lines nine and 10.
What we've done here in line seven
when we said b equals 17,
is we've told Python to evaluate the value 17,
and then make sure that b is a name for 17.
Python completely disregards that b
was a name for something else before
and it just says okay, this b thing is a name for 17.
A is completely unchanged.
The assignment expression when used like this,
does not change other variables,
it just changes the variable that you refer to.
There are ways for us to change a by using b,
but not when we're talking about integers.
We will talk about that later on in the videos
when we learn about mutability in the manage.
All right, that's it for this Python refresher on variables.
Do remember that this is not a Python introduction course,
because we've talked about some quite advanced things here,
but it's just to get you back up to speed with Python
in case you have experience from before,
or with other programming languages.
Thank you for joining me in this video
and I'll see you in the next one.