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
Learn how to use the Python interpreter interactively
This video doesn't have any notes.
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
We just saw how to pass a script
to the Python interpreter.
0:00
Another way to use the interpreter is
in a more exploratory interactive way.
0:03
You can actually open up a prompt
that allows you to type Python code
0:07
line by line.
0:10
This is super handy when you just
wanna see what some code does, and
0:11
not actually go and create a whole script.
0:14
This type of interactive programming
prompt is pretty popular in a lot of other
0:16
languages.
0:20
Generically, this type of exploratory
prompt is referred to as a REPL or
0:21
R-E-P-L.
0:26
which stands for Read, Evaluate, Print,
Loop, which is basically what its role is.
0:28
It reads the line, it evaluates it,
it prints the result and
0:32
then it loops back so
you can add another line of code.
0:36
Python's REPL is often referred
to as the Python shell.
0:39
It is a wonderful place to
hang out as you are learning,
0:42
so I definitely want you
to be familiar with it.
0:45
Come on, let's go explore.
0:47
So, to open up the REPL
we simply type Python.
0:49
And we'll get some information about
the version of Python that we're running.
0:55
So we're running 3.6.4 on Linux.
0:59
Remember, your workspace is running
a Linux OS, or operating system.
1:03
These three greater than signs, or
chevrons, as they're sometimes called,
1:07
are communicating to us that this is
the place where we can write some code.
1:11
So, let's do it.
1:15
Since we know we have a working program up
here, let's just write that out ourselves.
1:17
So, we want to make sure that we
type it exactly like it is above.
1:21
Make sure that you keep everything
lowercase, case matters in Python.
1:25
So, this prinT is different
than the lowercase print.
1:30
So, that's the name of the function
that we want to call, and
1:34
you call a function using parenthesis.
1:38
And now we wanna pass in a string.
1:41
And we can create a string
using quotation marks.
1:43
We'll do that, open that up.
1:46
And then our characters,
which here happens to be, Hello, World.
1:47
And then we close our string
with another quotation mark.
1:53
And then, finally, we close our
function call with a closing paren.
1:58
So let's go ahead and run that.
2:02
Awesome, Hello, World.
2:05
Now, a great thing about the shell
is that it keeps it's history.
2:06
If you press the up arrow, you can get
the last line that you just typed back.
2:10
This is handy if you had made a typo or
2:13
you wanna write a line
that is very similar.
2:15
Like for instant,
let's just say hello to you.
2:17
So I'm gonna put my name in here.
2:20
Hello, Craig.
2:21
Awesome, and Python shell is
also a pretty good calculator.
2:23
You can use it to do math-like things,
things like 1 + 2.
2:26
And so you'll see here that
the result printed out 3.
2:30
Note that we didn't actually
call the print function.
2:34
Now this is a good example
of the REPL in action.
2:38
What happened was it read the line,
1 + 2, it evaluated it, what's 1 plus 2?
2:40
And then it printed the result,
3,and it looped back to the prompt.
2:46
It showed the result so
that we could see it.
2:50
We'll get to some more math
in the course in just a bit.
2:52
Now another thing that is
wonderful about the Python prompt
2:55
is that you can help when you need it.
2:58
So, for instance, if we wanted to
know more about the print function,
3:01
we could just call the help function.
3:04
So we say help and we pass in
the function that we're interested in,
3:06
we're interested in print.
3:10
Let's see what happens.
3:11
So this kicks open the documentation for
the print function.
3:13
Now, this is gonna have some
terminology in here that we haven't yet
3:16
covered, so don't let that overwhelm you.
3:19
Now believe it or not,
if you stick with it and
3:21
immerse yourself,
this will all make sense.
3:23
We'll get to all of this,
just not right now.
3:26
So here's the description,
it prints the values to a stream, or
3:29
sys.stdout by default.
3:32
Standard out is another way of saying
the place that you ran the program from,
3:35
the default output.
3:40
So basically, what this is saying is
that we can print to other places, too.
3:43
This value here is the hello
world string that we passed in.
3:47
Then you'll notice that there's a comma
and then there's these ellipses,
3:51
there's these three ellipses here, right?
3:54
So, this means that we can actually
pass multiple values to print, and
3:56
we'll do that here in a bit.
4:00
We'll pass multiple values.
4:01
Now, if you look down at the bottom here
you'll see that that says END and that's
4:03
because we're inside of what is known as
a pager, when help opens it does that.
4:06
Now, it just so happens that all the help
fits on one page but let's go ahead and
4:11
let's make that not happen.
4:14
So this is something you can do too, you
can make the console bigger or smaller, so
4:15
I'm going to make it smaller.
4:18
And you'll see what happens is,
eventually,
4:19
there's this like blinking colon.
4:22
And I wanted to show you this just in case
you opened up help to something else.
4:24
You can press the up and
down keys and move around.
4:27
And space bar actually
moves a page at a time.
4:29
So, to get out of a pager,
what you can do is press Q.
4:32
And now normally what would happen when
you popped out of here is you would pop
4:39
back into your shell.
4:42
But it looks like I had a little
bit of a workspace problem.
4:43
Which I'm glad happened, so I can show
you what to do when this happens.
4:45
So the console,
4:49
you can restart your console always
by clicking either this X here.
4:50
This will close the console.
4:54
And I can up here and
say View > Show Console.
4:55
It should pop back open, there we go.
4:59
And so if we are inside of a shell,
5:02
You might wanna know how to
get out of here too, right?
5:04
So, there is a handy function called exit,
and that will pop you out.
5:07
But even better, and
I know cuz we programmers are lazy,
5:12
you can also press Ctrl +
D to drop out of there.
5:16
There we go.
5:20
Awesome, so now you have a place to go and
explore when you need to.
5:21
Also, you can't break anything in there.
5:25
So, feel free to do whatever.
5:27
And like you just saw,
you can always exit out.
5:28
A common thing that happens to people
just beginning to learn to code
5:31
is that they're afraid to make mistakes,
so they freeze up.
5:35
Please don't be afraid of making mistakes,
that will bite you in the future.
5:38
Try to change your point of view to
this one, making mistakes is awesome.
5:41
Messing up simply means
that you're trying, and
5:45
you can't learn without trying, can you?
5:48
So, don't let those mistake get you down.
5:50
It's part of the learning process,
and REPL is awesome for
5:51
that kind of exploration.
5:54
I do get it, though.
5:56
Those error messages can be intimidating.
5:57
So let's do this,
let's take a quick break, and
6:00
then take a look at some of the more
common errors, and how to handle them.
6:02
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