Execute simple Python commands on your command line, and begin to explore Python syntax or rules.
- One way to execute or run your Python code is by using the command line. Let's open up Terminal and try out a few commands. Let's go ahead and launch the Python interpreter. We'll use the Python three command. Now we're inside of our Python prompt. This means the computer's ready to take our instructions. Let's try out a simple math problem. Two plus two. First, we'll try it with what is two plus two? Uh oh, we get a syntax error. We call it that syntax refers to the rules of a programming language. Syntax errors happen when you write code that breaks the expected rules. In this case, we wrote our code in plain English, but the interpreter doesn't understand English, only Python. Let's try this again. This time, we'll take two plus two, and just like that, we get four. That's because two plus two is a valid Python expression. Let's try another one. Two plus, and then enter. Once again, another syntax error. That's because this is not a complete Python expression. For example, in English, I could say you're doing a great job with the course so far. That's a grammatically correct sentence, but if I were to say, you're doing a great job with, that would be an incomplete sentence. I didn't finish it up. Programming languages work much the same way. They need you to follow the syntax or rules in order to understand your commands. Let me show you one more thing. First, we're going to exit our Python prompt. We'll do that by typing exit or we could also use the shortcut, Control D. So now we're outside of the Python prompt. Do you remember our 01_03 file? Let me show you how we can run it from the command line. The first thing we need to do is make sure we're in the same directory as our file. To do that, we'll use the CD or change directory command, and we'll go to our file which is located inside of Desktop/ExerciseFiles/Chap01. Now if we hit enter, let's go ahead and make sure we're in the proper directory. We'll do that using the LS, or short for list command, which is available on Mac and Linux, and there you have it, we see our Python file. To run it, we're going to use the Python three command with a space, and then the name of our file 01_03.py. Great, we were able to get the output of our file here on the command line. Most of the time, you'll run your code directly in the IDE. Though, you may have occasions where you want to run some Python code from the command line, and now, you know how.
- Writing source code
- Basic statements and expressions
- Working with numbers and strings
- Writing conditional code
- Increasing your efficiency with functions
- Working with comments
- Making decisions in code
- Troubleshooting errors
- Learning about other languages