Remember where you have placed this as you will need to access this folder to run these files at a later date.
One of the first steps in learning any programming language is to get familiar with the tools you’ll be using. For Python, this means learning how to use the terminal and a text editor. The terminal is a command-line interface that allows you to interact with your computer using text commands, and a text editor is a program that allows you to write and edit text files.
While the terminal can be confusing to traverse, it becomes a lot easier when you think as a tree like structure. For example, the user can often be considered the trunk (we usually go further back, however for basics, we will refer to it as the trunk). Here, there are a few branches you can take, Documents, Desktop, Downloads etc. Inside each of these branches, you will be able to go further in, creating folders within folders, until you reach the files which can often be considered as the leaves.
To get started with the terminal, you’ll need to open it on your computer. On Windows, you can do this by opening the Start menu and searching for “Command Prompt.” On macOS, you can open the terminal by searching for “Terminal” in the Applications folder or using the Spotlight search.
Once you’ve opened the terminal, you can start typing commands. Some basic commands include:
pwd
: Print the current working directory (i.e., the folder you are currently in).ls
: List the files and folders in the current directory.cd
: Change the current directory. For example, cd Documents would change the current directory to the Documents folder.mkdir
: Create a new directory. For example, mkdir my_project would create a new folder called “my_project”.Now you have a basic understanding of how the terminal works. Let’s put it to use. Create a folder in your Documents folder, called ‘Beginners_python’ – this folder will be used shortly to store all your python files!
Remember where you have placed this as you will need to access this folder to run these files at a later date.
To get started with a text editor, you’ll need to choose and install one on your computer. As mentioned previously, there are many text editors however they all depend on your needs, for a free text editor that has a lot of capabilities, I’d suggest using Visual Studio Code.
Once you’ve installed a text editor, open your newly created folder ‘Beginners_python’ in your text editor and create a new file called ‘hello.py’. Giving the file the extension of ‘.py’ allows your python interpreter to understand that it is Python code and to treat it as that.
Now open your file and add the below – this will output a simple message saying “Hello, World!”.
print("Hello, World!")
To run your script, you’ll need to open the terminal and navigate to the folder where you saved the script using the cd command. For example, if you saved the script in the Documents folder, you would type cd Documents to change to that directory.
We use the command python3 to ensure that we are running the script with the most up to date version of python, when you hit enter, the script will run, and you should see the message “Hello, World!” printed to the terminal. If you encounter any errors, please double-check your print statement with the print statement above.
cd Documents/Beginners_python
python3 hello.py
Alternatively, if you don’t want to create a python file, you can open the terminal, type “python3” and press enter. This will open up the python interpreter in your terminal, allowing you to write executable python code. Once the python interpreter is open, all you need to do is type the same print statement as above, when you press enter it should return the output.
To close the Terminal Python interpreter, all you need to do is type exit() and press enter.
Congratulations! You’ve just written and run your first Python project. With a little practice and some more learning, you’ll be on your way to becoming a proficient Python programmer.