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