Learn to execute simple Python commands on the command line, as well as 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. Recall 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 type 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 this shortcut, control d. So now we're outside of the Python prompt. Do you remember our zero one zero three 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 slash exercise files slash chapter zero one. 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. Zero one zero three dot py. Great. We were able to get the output of our file here on the command line. Most of 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