Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to run Python in terminal

Getting Started: The Terminal

Before we dive into Python, let's first get a little familiar with the Terminal. The Terminal is like your command center for your computer. It's where you can give direct instructions to your computer and get immediate feedback. If you're a Windows user, you might know this as Command Prompt.

Opening the Terminal

On a Mac, you can find the Terminal in your Applications folder under Utilities. On Windows, you can use the Command Prompt or PowerShell, both of which you can find by searching in the Start menu. For Linux users, it's typically under Applications > Accessories > Terminal, though this can vary slightly depending on your distribution.

Once you've opened your terminal, you might notice a line of text waiting for your input. This is called the prompt. It's waiting for you to type a command.

One of the most basic commands is cd, which stands for "change directory". Directories are like folders on your computer. To navigate to a different directory, you type cd followed by the path to the directory you want to go to.

For example, if you have a folder in your home directory called "python_projects", you could navigate to it by typing:

cd python_projects

Running Python in the Terminal

Now that we've got some basics down, let's move on to running Python in the Terminal.

Check Python Installation

The first thing we need to do is to make sure Python is installed on your computer. In your Terminal, type:

python --version

If Python is installed correctly, you should see something like "Python 3.8.5". The numbers might be different, that's okay. They represent the version of Python you have installed. If you see a message saying that Python is not found, you'll need to install it.

Running a Python File

You can run a Python file from the Terminal by using the python command followed by the name of the file. For example, if you have a Python file named "my_script.py", you could run it by typing:

python my_script.py

This will execute the code in the "my_script.py" file and display any output in the Terminal.

Running Python Interactively

You can also use the Terminal to run Python code interactively. This means you can type in Python code, hit enter, and see the result immediately. This is a great way to experiment with Python and test out ideas.

To start an interactive Python session, just type python in your Terminal and hit Enter. You should see something like this:

Python 3.8.5 (default, Jul 28 2020, 12:59:40)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

The >>> is the Python prompt, waiting for your Python code. Try typing print('Hello, World!') and hitting Enter. You should see "Hello, World!" printed to the Terminal.

Conclusion

Running Python in the Terminal might seem a bit overwhelming if you're new to programming, but don't worry, with a little practice, it'll become second nature. And remember, the Terminal is just a tool. It's a way for you to communicate with your computer, to tell it exactly what you want it to do. As with any tool, the more you use it, the more comfortable you'll become with it. And as you become more comfortable with it, you'll find that it's a powerful ally in your Python programming journey. So don't shy away from it, embrace it!

Remember, every great programmer was once a beginner who didn't give up. Keep experimenting, keep exploring, and most importantly, keep coding!