Now you've installed Python onto your system and if not, don't worry because there's plenty of time for you to quickly head back to our previous post and install Python onto your system:
@run.vince.run/installing-python-on-your-system
It's not difficult to run your Python code, but depending on what you want to do, there are a few different options to choose from. Through this post, we'll also cover off the customary "Hello World!" that seems to be a compulsory first program in these types of tutorials.
All you need to do is open a terminal in Linux or Mac, or open Powershell in Windows and run a command similar to the one below. All we are doing in this command is starting with python3 as we want to use this specific version of Python, we then use the -c option to then specify we want to run a command. We can then put any valid Python code in between the inverted commas. In our example below, we are using the print() function which will print out data for us to the screen. As we haven't really learnt about any data types as yet, we have simply added the string 'Hello World!' in the print function brackets.
python3 -c "print('Hello World!')"
Hello World!
As you can see from the output, when you press enter, the string 'Hello World" is printed to the screen. You can type almost any valid Python code in between the inverted commas, but as you'll see as you continue to work with Python, white space is what determines structure in Python, so you either need to get very creative to get more complicated code to be run...We'll touch on this in a later post, but for now just keep that in mind.
Instead of typing our complete command into the command line, we instead type python3 into the command prompt. Without any other options, this will open the interactive shell for you in the terminal or Powershell. From this interactive shell, you will be provided with three greater than symbols(>>>) as your prompt, where you can enter your Python code, press enter and then be presented with the result. As you can see below, we have once again entered the Python code of print("Hello World!") into the shell and get the same response we got previously.
python3
Python 3.8.7 (tags/v3.8.7:6503f05, Dec 21 2020, 17:59:51) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello World!")
Hello World!
The shell will continue to wait for you to enter more Python code until you either press quit(), exit() or Ctrl-D for Linux and Mac, and Ctrl-Z for Powershell.
The code below will output our application file into a script called test.py in our working directory. As you can see we are using the echo command to create our output. The first line is known as a "Shebang" and is used to indicate the path of Python we are going to use to interpret the following lines of code in our script. We leave a space and then enter the print function again as we have in the previous parts of this article.
The code is output to the file named test.py. It has the .py file extension to allow your system to know it is a Python script.
echo "#!/usr/env/bin python
>>
>> print('Hello World!')" >> test.py
To then run the script, we can simply run the code from the command line using the python3 command and the name of the file as we have below, and when you press enter, you will be presented with the output our print() function includes.
python3 test.py
Hello World!
These examples have been very basic but are the basis of a lot of scripting and applications you'll make when using Python as a programming language. From here, you're well on your way to learning the basics of the Python programming language and moving on to more advanced things.