Running Scripts4:59 with Craig Dennis
Let's learn how to use workspaces to run an existing script
script - The file we are writing that the Python interpreter will use.
interpreter - The program that will read and execute our script.
console - This is a tool that sends low level commands to your Operating System (macOS / Windows / Linux).
We used the
ls command to list files in the current directory. The
~/workspace folder is the same as appears in the file browser.
All right, so let's get your Workspace open, 0:00 remember Workspaces is your online coding environment, it's web based. 0:02 So you can code from anywhere, on any computer with an Internet connection. 0:06 Now I recommend that you also have a physical keyboard, 0:10 coding on a tablet is a bit of a bummer. 0:12 I've attached a Workspace for this video. 0:15 You should see a button that says launch workspace. 0:16 Go ahead and give that a click. 0:19 All right, welcome to Workspaces. 0:21 This is all yours. 0:23 So over here we'll see a list of files underneath this Python Basics here. 0:25 It says hello.py. 0:29 Let's go ahead and click that. 0:30 So this little area here, this is what's known as a text editor. 0:33 And you'll see here we have a tab open, this is a tab. 0:38 And this is where we're gonna be writing our code. 0:42 Now, as you can see, there is a single line of code here, and 0:45 you'll notice that the text is in different colors. 0:48 As you can see, there's a reddish color and then there's a teal color here. 0:51 This is called syntax highlighting. 0:54 Syntax is the rules of the language. 0:56 Now, this syntax highlighting is just one way that our editor is gonna help us out. 0:58 The traditional first step when learning any programming language is to write this 1:02 phrase here, this "Hello, World". 1:07 You gotta write that out to the screen. 1:10 It's like a rite of passage. 1:12 And its history dates way back to the 1960s. 1:14 I wanted you to be a part of this tradition. 1:16 So there's more information in the teacher's notes. 1:18 So here it is. 1:20 Here is Hello, World in Python. 1:21 What this script is instructing the computer to do is to output, or 1:24 print, the string, this string here. 1:28 Which is a series of letters and numbers, Hello, World to the screen. 1:32 Now, you might have noticed that I'm referring to this file 1:38 named hello.py as a script, that's 1:41 because we're going to pass this file to what is known as the Python Interpreter. 1:43 And the Interpreter is going to read through this file 1:48 just like a movie script, line by line. 1:51 The Python Interpreter will then interpret meaning from your script and 1:54 perform it, or evaluate each line of code. 1:58 This bottom area here is known as the console. 2:03 Now, this is a terminal session running on your very own version of Linux. 2:07 Linux is an operating system much like Windows or Mac OS. 2:11 Now, depending on which operating system you're running on your local machine, 2:15 you also have an application like this available to you, 2:19 it's usually tucked away. 2:22 This is what is actually running underneath all of those Windows. 2:24 So, on a Windows machine, this console is called the command prompt, or 2:28 the PowerShell. 2:31 On Mac OS, it's called a terminal. 2:32 But this here is running in the Cloud for you, so you don't need to worry 2:34 about getting things set up on your local machine just yet. 2:38 Check out the teacher's notes if you would like to learn more. 2:42 And this here is called the Command Prompt, 2:45 this little tilde here means that I'm in my home directory, and 2:48 this slash means that I'm in a subfolder or a subdirectory named workspace. 2:51 Directories are synonymous with folders in most operating systems. 2:59 Now this workspace directory, again, or 3:02 folder, is the same one that this file browser here is showing. 3:04 You can send commands to your operating system from here. 3:09 For instance, if I wanted to list all the files, 3:12 I could just run the command ls, which is short for list. 3:14 And see how it shows my hello.py file? 3:20 It's just like this file browser up here. 3:23 It's the same folder, it's the Workspace folder. 3:25 So now that we're here at the Command Prompt, 3:28 we can actually tell the Python Interpreter to run our script. 3:30 Now, you do this by typing Python followed by a space and 3:33 then your script's name, which is our script name is hello.pi. 3:38 Here we go, and now you'll see the output Hello, World indeed. 3:43 Wow, that was a whole bunch of new information, right? 3:48 Nice job immersing yourself. 3:51 We'll talk about all these terms again throughout the course but 3:53 let's do one more run through of what just happened. 3:56 So we have a file browser over here and it's showing the files from 3:59 the Workspace directory, which is the same directory in our terminal session. 4:02 And from the command line here we can call the Python Interpreter by typing python 4:07 and then we pass our file named hello.py to it. 4:11 In our code editor here, we see a single line of code and its syntax highlighted. 4:17 That line of code is calling the print function 4:21 which is being passed a string Hello, World. 4:25 This line, when evaluated, will write out that string to the terminal. 4:28 Awesome, glad you got a chance in Workspaces. 4:33 It's a super handy tool. 4:35 Remember, you can get the Python Interpreter installed on your computer and 4:37 run things there, but let's not focus on that just yet. 4:40 Let's get you coding first. 4:43 We've only read existing code. 4:45 You haven't even written your code yet. 4:46 Let's change that. 4:48 I know, let me show you a cool feature of the Python Interpreter, 4:49 it has an interactive mode where we can type Python and it evaluates immediately. 4:52 Let's go do that right after this quick break. 4:56
You need to sign up for Treehouse in order to download course files.Sign up