Welcome back.
So we've just learned about the first two well kind of three because we also mentioned that there is
such a thing as a complex number in python.
But we talked about the main ones that end and float and there are still a few data types remaining
but before we get to these we are missing an important concept and this is going to be our first important
term in Python.
And as a matter of fact in all programming languages it's called variables.
Yep that's a term.
Now if this is your first time learning a programming language you might not know what this means.
If this isn't your first time well this is very simple because all languages have variables but what
are they.
Exactly.
Well variables store information that can be used in our programs.
So we can hold perhaps user inputs like values maybe when you log into facebook you need to hold some
information such as your profile picture or maybe your date of birth in a variable.
Variables are ways for us to store information on our computer so let's have a look at this.
If we remove this and let's say I'm creating a quiz program and this quiz program maybe measures your
IQ let's say you just took the quiz and you found out that your IQ is 190 quite high.
Good job.
But we need to store that information somewhere where we can do that with variables so that in Python
all we need to do is name it whatever you want in our case there'll be IQ and we're going to say IQ
equals one 90 and this IQ here is a variable.
It is something that I just completely made up.
I can name it whatever I want.
That's a variable too.
The idea is that once we assign to a variable that is we're saying One ninety is going to be assigned
to IQ.
I can now use it in my program whenever I want.
For example I can later on print or make sure it's not cap.
Let's do print IQ.
And if I do that and click Run you see that I can use IQ so we can pretend here that a user takes a
quiz find out their IQ is one ninety we can store that information in this variable and later on when
they come perhaps online or try to use the program again they don't have to take the quiz all over because
well we store that information in IQ and remember what I said at the beginning programs are simply data
that's being stored that's being changed that's being removed and that's all programs are and variables
are important concepts in Python and all languages now variables can also sometimes be called names.
So this could be a name for example in assigning a value is also known as binding.
That is where binding the value one to this variable so that when we request this variable later on
in our program our computer knows how to look for this information it's going to say hey I know what
IQ is I stored it somewhere a memory and it's going to go look for that and because it's being bound
to a value it points to this value one ninety.
And remember this number in memory gets stored as a binary representation in zeros and ones.
Right.
But it doesn't matter to us because however our machine stores it we don't care we just want to be able
to retrieve it.
And then when we printed to do well get one item.
Now we're gonna be using variables all over the course but on top of just naming variables however we
want.
There is some best practices around variables of how you should write good variables and as a matter
of fact these are specific rules that the Python community as a whole has that you'll just have to remember.
So let's have a look at this variables.
And remember this is the symbol for best practices are what we call snake case snake case means it's
all lowercase and then spaces while they don't exist we use underscores variables must start with a
lowercase or an underscore variables can be anything with letters numbers and underscores.
But remember they have to start with lowercase and underscore that means we can't start a variable with
a number.
There are also case sensitive.
That means if I create a variable but let's say this snake case this variable has a capital E instead
of a lower Casey there'll be a different variable.
And then finally you can't overwrite keywords.
Let's go through these with some examples first.
A variable has to be in the form of a snake case.
That is if I want to call this user IQ I should technically have an underscore here instead of a space
just to make sure that a programmer.
Maybe I'm working on a team can read this variable so that's snakes.
You also have to start your variables with either a letter or an underscore.
So I can technically do this and I click Run.
Well that's going to give me an error because I've changed a variable so now in order to access that
variable you have to go like this.
Now underscore in Python signifies a private variable something that will go over later on in the course.
But usually you're starting your variables with a letter and afterwards yeah you can add numbers if
you want in here.
That's no problem.
This is still going to work.
This is a valid variable.
Finally a variable is case sensitive.
So if I do user IQ here and I do.
Capital letters I can't access this
like this because while it doesn't exist again it's case sensitive we have to make sure that we match
and finally we don't want to overwrite key words.
What does that mean.
Key words in python.
Well they already mean something in Python.
For example this print is a keyword.
You can see it highlighted in blue so that if I create a variable saying print equals one 90 and then
I do print print.
Let's see what happens we get an error because I can't really assigned to this variable because print
already means something.
Now I know what you're wondering.
What are these keywords in Python.
That's a simple Google search away and we'll learn these throughout our course.
If we go to Python keywords by W3 schools you'll see that we have these keywords that each mean something
in Python again.
We'll go through these and we'll learn them throughout our course.
And if you look it's not that intimidating.
There's not that many.
So as we practice you'll start to get familiar with them.
But the easiest way to tell whether it's keyboard or an important word in in python.
Well you see that it's highlighted in blue as soon as you create a variable that is unique.
It's highlighted in white and this will be the case with whatever environment that you're typing code
into as long as it's set up for python.
Now beyond the python keywords there are different things like for example the I.A. for integer that
we've learned so these.
We're gonna get familiar with.
So a good rule of thumb for variables is to make them really descriptive really say what your intention
is and a good programmer is somebody that's able to name things really well with their variables.
So if a new developer comes and looks at your code it's easily understood.
Finally variables can also be reassigned.
For example let's say we have IQ here of one 90 and then I decide to perhaps have another variable call
it user page and for some reason I want to assign user H to perhaps have IQ divided by 4.
Is that going to work.
It should.
Right.
I'm saying user age is going to equal forty seven point five I'm using IQ which is 190.
Dividing it by four and then assigning it to user age.
I can maybe assign this to another variable called user age or called a.
And once again I run this and it's printing the same thing so you can use variables to store that information
and use it whenever you want.
You can use it in operations you can use it to reassign it whatever your program needs.
Now later on in the course we're going to learn about classes and classes actually have a different
convention than this but we'll get to that later on.
For now though I want to mention two small gotchas with variables that you should be careful with.
For example there's an idea of constants and constants are those things that never change in a program.
For example if we wanted to create a constant such as the value of pi let's say for now it's three point
1 4.
We can have it all in capitals and that's going to tell other programmers that hey this is a constant.
This number is not meant to change.
I mean we could change it if we want.
There you go.
I just made pi equal to zero so it was stored as three point one for a memory but then we overrode it
and reassigned it the value of zero.
You can still do that but a good convention is that if you see this that means this number or this value
should never be changed.
Another type of variable that you're gonna see and this is something we'll see later on in the course
are the.
It doesn't look like I just did double on discourse here but it's two underscores and we call these
Dunder.
And as you can see here we have some Dunder variables that Python has now we'll learn more about these
later on.
But the idea here is that these are meant to be left alone.
You should not touch them.
You shouldn't create a variable with two underscores like this and call it high high and assign it a
value.
I mean you still can.
However this is generally not good practice.
So you want to be careful with that but the one thing that I really want you to take away from this
is that variables are really important concepts and programming naming variables is one of the most
important skills we have as a programmer.
I know it sounds silly but there's so many times that I read code that is so hard to understand simply
because a programmer is not descriptive enough.
So throughout the course we're gonna learn how to name things well so that our code reads like English
and that's the whole point of writing a code.
The point of writing good code is that it's readable and understandable by other programmers by the
way to finish off.
I just want to show you one quick trick.
There's also a way that you might see in some code bases that uses something like this.
He calls 1 2 3 and this simply is a way for us to rapidly assign values to variables multiple times.
So for example if I do print a then print B then print C and I run this you see that I get 1 2 3.
We assign value of 1 to a value of 2 to B and value 3 to c.
Just a quick shorthand way that you might encounter.
All right let's take a break and I'll see you in the next one.