0% found this document useful (0 votes)
7 views6 pages

PYTHON100 MOD4 NumericVariables V2 en

Py4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views6 pages

PYTHON100 MOD4 NumericVariables V2 en

Py4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

- [Instructor] Hello, welcome to Python Fundamentals.

This is Module 4: Numeric Variables.


So in this module,
we're gonna learn how to create a variable,
specifically numeric variables in Python and work with them.
So let's get started.
So what is a variable?
A variable is a named location in the computer memory.
When we create a variable,
we have the ability to give it three things,
and these three things are critically important.
We give every variable a name.
We can put a value inside a variable,
and every variable has a type.
So every variable has these three features
and they're really important to understand for programming.
Python in particular supports two numeric types,
integers and floating point numbers.
And we'll work with them today in the code.
So I think it's important to understand
what's going on inside computer memory
when we create these variables and use them.
So I wanna show you some illustrations
of what's happening in computer memory
before we get to Python to start the coding.
So let's take a look
at what happens in computer memory when we have variables.
So we can think of this blank screen,
think of that blank screen that you just saw
as computer memory, okay?
If you want to work with data,
in this particular module,
we're gonna work with numbers,
but if you wanna work with any kind of data
you have to save it in a variable.
You have to store it somewhere
so the computer can access it and work with it.
The way we store data is by creating variables, okay?
So let's create two variables.
So if we create a variable,
essentially what we're doing is we're taking a piece
of computer memory and giving it a name.
So here's one variable and I'm gonna give it a name.
I'm gonna call it X.
The second feature of a variable is its value.
I'm going to put a value in here,
I'm gonna put in the number 5.
So now I have a piece of computer memory
with a name, X and a value, 5.
Because I put 5 in there,
I know that in Python the type is going to be int.
I'll just write that below to remind you.
Python knows because I put a 5 in there
that the type is int,
it's an integer,
it has nothing after the decimal point.
That's what we have in that variable.
Now I can create another variable
if I have a need to do that.
Let's create a second variable called Y.
And I'm gonna put a floating point type value in there.
Let's say 6.2.
That's a floating point 'cause it has a decimal part.
And if I create that in Python,
Python will know that that's a float
because it has a decimal part.
So you can see there on the screen
that I've created two variables in computer memory.
I've given them names, X and Y, I've given them values.
X is 5, Y is 6.2.
And because of the values I put in there
Python knows their types.
Python knows that X is an integer
and Y is a floating point number.
Okay, now that we have this illustration
let's write the Python code
that would actually create that in computer memory.
So at this point what I want you to do
is maybe pause the video,
bring up Anaconda Jupyter notebook.
Your Jupyter Labs will open up
and we will open up a Jupyter notebook for the code
for this particular module.
I might wanna pause the video here.
I'm gonna go ahead.
I already have Jupyter notebook up,
Jupyter Labs, I'm sorry, up, here it is.
Here's my launcher.
So if you recall from the previous module
where you learned how to install Anaconda,
I already have it pointing to the place
where I'm gonna store all the Python code for this class.
I can see my other notebooks that I created
in a previous module here.
And you should see the launcher up.
If you don't see the files,
make sure that you've clicked over here on this file icon.
And you can see a list of all the files here
that we're using for the course.
And the launcher should come up for you
on the right hand side.
So what I'm going to click on now
is I'm gonna create a new Jupyter notebook
for this module.
And typically I think
that you should create a Jupyter notebook for every module
'cause it'll organize your code a little bit better.
So I'm gonna go ahead and say new notebook.
A new notebook comes up,
I see it over here in my list of files
and it's called a default name of untitled.
I don't really like that name.
So I'm gonna make it a new name.
I'm gonna go over the file menu, rename notebook,
and I'm gonna call it Module 4.
That seems to be a better name, right?
I'm gonna click on rename.
And I see over here in my list of files
the module was renamed
and up here it was renamed to Module 4.
You learned from the previous module
that when you start a new notebook
we get one code cell at the top
where we can start typing code in our Jupyter notebook.
So I see up here that I have my one cell
that I can start typing some Python code
and then we did a little Python code in the last module.
Okay, so I wanna create a variable.
So what I wanna do is I want to create
what's on this illustration with Python code.
I wanna make two variables.
I wanna put 5 in X and I wanna put 6.2 in Y.
So let's go ahead and do that in our Jupyter notebook.
So the way that you create a variable
in Python is you put the name of the variable.
So we want X.
I use the equal sign to put a value into the variable.
So I wanna be very clear about this equal sign in Python.
It doesn't mean what you typically think
of as equals in math,
it means assignment.
So equal signs very often
in computer programming mean assignment.
So this means to essentially take 5 and put it into,
assign it to the variable X.
That's exactly what that code means.
So I'm going to run the code by clicking the triangle here
or you can do shift enter.
And as we learned previously
a couple things happen when we run Python code.
Jupyter notebooks gives the code a number.
So that's going to give that code snippet.
We talk in Python often about code snippets.
So that's code snippet 1, it numbered it.
If the code snippet has any output,
we would see that right underneath here,
we don't have any output
'cause we haven't told Python to print anything out.
And we automatically get a new code snippet up ready to go
for our next line of code.
So I'm gonna type in the next line of code,
I'm gonna create the variable Y,
Y equals 6.2.
Now I'm gonna run that.
Now those two lines of code,
I've created two code snippets,
snippet one and two,
they have created in computer memory two variables
just like my illustration.
So if I go back to my illustration,
I have now written the Python code
to create these two variables.
So how do we know what Python thinks the type is?
We can look at it and say obviously 5 is an integer,
6.2 is a floating point number,
but we can be sure in Python
we can actually check to see what those types are
by using the type function.
So the way the type function works is we use the word type
and then in parentheses we give Python the variable
that we want to ask.
What type is this variable?
So the code I've written here is saying to Python
tell me what type X is.
And Python comes back with output saying
that it knows X is an integer
and I can do the same thing with Y and run that.
And I know that Y is a floating point number.
Now what you see on the screen here
is all of the features of a variable.
So if you remember from the para point,
there are three features.
Variables have names, they have values,
and they have types, right?
So every variable has a name, a value, and a type.
So we've written the code to do all of those things.
Now, the reason that they're called variables
is because we can actually change the values in a variable.
We can put different values in.
So let me go back to the illustration of computer memory
and show you what that looks like.
Suppose that I don't want 5 in X anymore,
I wanna put a different value in it.
So essentially I wanna write some code in Python
that gets rid of the 5,
completely removes it from the variable,
and instead, I wanna put in 47.
Instead I wanna put in 47.
So let's write the Python code to do that.
So I'm gonna go back to my Jupyter notebook
and I'm gonna say X equals 47 and run that.
So couple things to notice is I don't need the spaces.
Do you see the spaces that were
on either side of the equal sign?
I don't really need those here.
They're just for human consumption, better viewing,
looks a little bit cleaner with the spaces
not necessary for the code.
So code snippet 5 has actually put 47 and X.
And actually if I just type in X in the code snippet
and run it,
it will come back
and tell me what the value is for the variable.
So code snippet 6, just typing in X and running it,
Python comes back and says,
the value inside X is 47.
So this line, this code snippet here,
x equals 47 has done this
what we see here in computer memory.
Now, something I wanna emphasize
that's super important here
is when I did X equals 47, 5 went away.
There is no more 5, we've lost 5, it is gone.
Now you and me as humans,
we know 5 used to be there,
but the computer does not know that.
If we replace a value in a variable,
it is gone never to be seen again, okay?
So if you wanna save a value,
you need to create another variable
or not wipe it out with something.
Putting a new value in completely
gets rid of what was there before.
So it's a very important concept.
Okay, so let's go back to our Python
or actually let's go back to our PowerPoint.
We've covered everything here about what is a variable
and the three features and the two numeric types.
We've created variables.
Python is case sensitive.
So let me show you something about that.
Let's go back to our code.
We have two variables, X and Y.
We could create a third variable with capital X.
We could say capital X equals 9.
Now what we've done in computer memory,
if we went back to computer memory
is we'd have a third variable, capital X.
We put 9 in it so it's an integer type.
Now this is not a good idea, right?
It's not a good idea
because capital X and small X easily confused, right?
It's legal in Python,
completely legal to have two variables,
one capital, one small.
Python knows them as completely different things.
But as human beings,
it could be kind of confusing.
So it's not a good idea to have those variables
with those very similar names
even though it's completely legal.
So Python is case sensitive for everything.
We've assigned values,
we learned how to display variables.
We can type in the variable name,
or we could do something like this, print, capital X.
So a couple different ways to see what's inside a variable,
we could just put the simply the variable name
or we could use the print statement like we did
in a previous module to print out what's inside a variable.
Finally, there's some basic math operations
that we can do with variables.
So for example,
all of the basic math operations work in Python.
So we can do something like X plus Y
and we get the answer 53.2 is the answer X plus Y.
If you recall, we are now adding 6.2
and 47 together and the answer's displayed.
Now, something that's super important to understand
is 53.2 is not saved anywhere in computer memory.
Python calculates it, prints it out,
and forgets about 53.2.
If you wanna save 53.2
because maybe you wanna use it later in code or print it out
or do something else with it,
you have to put it in a variable.
So let's create a variable called sum
and I'm going to put X plus Y into sum
and I'm gonna run that.
Now I have a fourth variable sum that contains 53.2.
So let me add that to my illustration here.
I have sum, and it's got 53.2 in it
and we know that's a floating point type.
So really important, if you wanna keep something
put it in a variable.
All of the other basic math operations work
in Python as well.
So for example, we could do X minus Y
for to do subtraction.
We could do X times Y to do multiplication,
and we can do X divided by Y to do division.
So let me put up all of those here for you to look at.
We've done X plus Y, we've done X minus Y,
we've done X multiplication,
and we've done division.
So we have now covered all of the basics
about numeric variables.
See you in the next module.
(upbeat music)

You might also like