Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Let's explore how to store the objects that you create for later use.
Variable Naming Rules
- Can contain both letters and digits, but must begin with either a letter or an underscore.
- Remember that case matters, you can use upper case, but by convention we do not.
- They cannot be a Python keyword
Learn More
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
So you know that Hello, World string
that we used in our hello.py file?
0:00
To be a little bit more specific
we created the new string.
0:04
We did that creation by surrounding
our text with quotation marks.
0:09
This is what is known as a string literal.
0:13
Now, we currently don't keep that
string around, we create it,
0:16
we use it in our print statement,
and then we let it go.
0:19
But we can actually store it and
use it later.
0:22
In the real world, we create things and
store them for later use all the time.
0:26
Here's an example I can
think of right now.
0:30
Actually I can't stop thinking though
probably because it is about lunch time.
0:32
Last night I made some delicious tacos, so
0:36
good in fact that I wanted to
bring them into work today.
0:38
So I grabbed a food storage container,
some Tupperware, and
0:41
I put the remainder of
the delicious meat in it.
0:44
When I put it in the refrigerator
here at the office,
0:47
I decided that I had better label it,
mostly so people knew it was mine.
0:50
Putting a label on it ensures that no
one is just going to throw my food away.
0:54
Also I know it's mine.
0:59
We have a lot of other labeled
Tupperware in our fridge.
1:00
Labeling it helps me find my food.
1:03
Now, here's a little confession.
1:06
I end up making a lot of tacos, and
I end up keeping a bunch of leftovers.
1:07
And the problem that leads to is this,
if I just label this with my name,
1:12
I don't actually remember
what is in the Tupperware.
1:17
Now one way I get around that is
by adding another label to it.
1:20
This one is beef.
1:23
This one is chicken.
1:24
Some carnitas and this one is something.
1:25
So there are two of these labels
referring to the same object.
1:30
In Python everything you create is
an object and you can label an object so
1:35
that you can refer to it
later in your program.
1:40
These labels that you create
are called variables.
1:42
Variables allow you to refer to
objects that had been created.
1:46
They are object references.
1:49
Now let's launch our workspace and take
our example from Tupperware to software.
1:51
Okay, it's time to make this
code a little more personal.
1:56
Let's change this so it says hello to you.
1:59
So I'm gonna come in here and
I'm gonna change this to be my name.
2:02
I'm gonna type Craig.
2:05
You should type your name there.
2:06
And now, let's go ahead and
add another print statement and
2:08
this time write it about yourself.
2:12
So we'll do print, call the function,
and we'll say, Craig is learning Python.
2:13
Now let's go ahead and
run the script, again, that's python and
2:23
then you can type the start of your
file name and then you can press tab and
2:25
then it will complete it for
you, nice right?
2:29
Awesome, now, what if I asked you to
change who this program was addressing?
2:32
Like instead of you,
some other clever person.
2:38
Ooh, I know.
2:42
Let's change this to greet the very
first computer programmer, Ada Lovelace.
2:43
She published the first algorithm for
2:48
a device called the analytical
engine in 1840s.
2:50
Pretty clever as putting it lightly.
2:53
Check the teacher's notes for more.
2:55
So now, I know you could very easily just
go and replace your name with Ada's.
2:57
But let's do something a little
more clever than that.
3:02
How about we create a new string
with her first name in it?
3:06
And then we store it.
3:11
So we could use it later
in these lines right?
3:13
These lines here.
3:16
We can use it here and here.
3:17
So, you know how to
create a string already.
3:20
You just surround some text with quotes.
3:22
So we'll put Ada's name in there, Ada.
3:26
So, that creates a brand
new string object.
3:30
That's a string literal.
3:34
And we wanna keep this around so
we need to give it a label or a name.
3:35
So let's see,
how would we like to refer to the string?
3:40
What's a good name for it?
3:44
Well, it is a first name,
so let's call it that.
3:46
Now, there are some naming rules that
I've added to the teacher's notes.
3:51
But the most important one is that there
can't be spaces in a variable name.
3:56
So if we say first and
I wanted to call this.
4:01
We'll move her over here.
4:04
So we wanna say first,
4:06
and then this is where I would say first
space name if I was just naming something.
4:07
But we can't have spaces
in a variable name.
4:12
So what you do,
is where you would normally put a space,
4:14
you use an underscore.
4:18
So we'll put underscore, that's next
to the zero key on your keyboard.
4:19
I know that that's one that you
probably don't use very often.
4:22
And then you put the next word so, name.
4:25
And you assign object to
the label using the equal sign.
4:27
This is called assignment.
4:31
So the string Ada is assigned
to the first_name variable.
4:32
And now the first_name variable refers to
this string object that we created, Ada.
4:39
So let's use it.
4:47
But one thing to notice is that we've
kept the variable name all lower case.
4:49
Remember, case matters.
4:52
And it makes a difference in
your variable names as well.
4:54
We follow the typical
Python naming convention.
4:56
You keep you variable names lower case.
4:59
And you use underscores to separate words.
5:01
A fun fact, this naming style
is often called snake case.
5:04
More in the teacher's notes.
5:07
So, first_name is our string, right?
5:08
So we should just be able to use it.
5:11
So lets see, if I come in here and
I just say print(first_name),
5:12
let's go see, so I'm gonna use the up
arrow, let's see what that did.
5:17
We should see Ada and then Hello, Craig.
5:21
Awesome, it's right there.
5:25
So, in order to fix these other
statements we're going to
5:26
first explore a little something
about the print function.
5:30
So the print function as we've used it,
is just taking a single argument, right?
5:34
We're just giving it one argument here.
5:40
Well, it actually takes
multiple arguments.
5:42
Remember when I talked about the ellipses
just briefly when we looked at that
5:44
help documentation?
5:47
So, what happens is that each value that
you add to the print function is printed,
5:48
one after the other,
separated by spaces by default.
5:52
Like this, here.
5:56
Here, check this out.
5:57
So, if we come in here and we say print.
5:58
Now, I'm just gonna create
a string called These.
6:01
And then I'm going to add a new argument
and I do that by typing a comma.
6:04
And now I'll type a new string,
let's say, will be.
6:09
And I'm gonna close that string and
then I'll do one more so
6:13
we can see that there's just
another argument with a comma.
6:17
And joined together by spaces.
6:20
So if I'm gonna save that and
if I just run that one more time,
6:26
we'll see each of these strings one
after the other separated by spaces.
6:30
So let's go ahead and
rewrite this line first, so
6:36
that we can see them next to each other.
6:39
Right, so I'm gonna go ahead,
give ourselves some space here.
6:41
I'll say print, and
I want the first part to be the same.
6:44
I want that Hello, that's kind of
gonna be the same no matter what.
6:47
We'll say, Hello comma.
6:50
But now I want to end my string.
6:52
And then we want to show our first_name.
6:55
So we're going to do a comma,
See there's a comma in the string but
6:58
there's also a comma on the outside here,
cuz that's separating our arguments.
7:03
So we're gonna add our next argument.
7:08
We'll do first_name, and
then we'll close the function call.
7:09
So we should see, Hello,
Craig, Hello, Ada.
7:17
And we do, awesome.
7:22
So let's get rid of some
of these other lines.
7:24
So I'm gonna come here and
in the Edit menu if you come here.
7:26
You can see that there is a Delete Line.
7:29
Which is Shift+Cmd D on a Mac.
7:32
I believe it's probably
Shift+Ctrl D on Windows.
7:35
So I'm gonna go ahead and press that.
7:38
You'll see the line went away,
but if I come here and
7:39
I do Shift+Cmd+D, it goes away.
7:41
Now if you mess that up, there is always
Edit, Undo, and it will come back, right.
7:44
And that was Edit, Undo is Cmd+Z.
7:50
And there's a redo, Shift+Cmd+Z.
7:53
So if I press Cmd+Z, it will go away and
Shift+Cmd+Z they'll go away again.
7:55
Cool, so let's rewrite this
7:59
learning Python using our
variable just like this is.
8:04
So we'll say, print,
8:07
and actually, why don't you go ahead,
and give that a go?
8:12
Why don't you make that line,
use our variable so
8:16
that it says Ada is learning Python.
8:19
Pause me, and try to finish the line.
8:21
When you're done, unpause me,
and I'll show you how I did it.
8:24
Now don't worry,
you won't break anything, remember?
8:27
Ready, Pause me.
8:30
So how'd you do?
8:32
Here's what i did.
8:34
I used our variable first, right?
8:35
So, I said, first_name, and
then I wanted to add another argument so
8:37
I typed a comma and
then the remainder of the string.
8:43
Now, I'm lazy so I just copied this, came
like this, and I did copy and I pasted it.
8:46
Close that string here at the end and
close the call.
8:52
There we go.
8:58
And if we come down here,
we can clear this.
8:59
We press up a couple times, there we go.
9:02
Ada is learning Python,
I bet she'd love Python.
9:05
One final thing that I'd like
to point out about our variable,
9:09
is that you can actually label any object.
9:12
So let's do this.
9:14
Let's assign the variable
first_name a number.
9:15
So let's say, 11.
9:19
Right, so now we go and we run it.
9:22
And it says 11 is learning Python.
9:26
Not a common name, but
I've seen stranger things.
9:29
11 would love Python too I'm pretty sure.
9:31
Aren't you glad that we stored
that object for later use?
9:34
It's just like those leftovers, mm.
9:37
Ooh, that reminds me.
9:40
I'm gonna go retrieve my tacos from
the fridge by using the label.
9:41
After this delicious break,
9:45
we'll talk about how to add a little
interactivity to our script.
9:46
Taco to you soon.
9:49
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up