Running Your Python Code

in STEMGeeks7 months ago

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:

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.

From The Command Line

The first thing I like to show people is you can run basic Python commands and scripts directly into the command line. This is a great way to test a small piece of code or perform minor tasks directly from the command line without needing to create an actual running program or script.

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.

From The Interactive Shell

Just like running Python from the command line, the interactive shell is another easy way to test our Python code and run small programs. When I first started learning Python I used the interactive shell to really grasp the syntax as it lets you simply type the code into the shell, you press enter and it either works or you get an error. I still use it when I am trying to work out a piece of code I want to put into a script.

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.

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.

From An Application File

This is the main way you'll be running Python programs and scripts, especially when your creating larger more functional applications. A lot of the time you'll need a text editor when creating these scripts as you will only be able to use valid ASCII characters in your script. A word processor on the other hand will add in all different types of characters which your interpreter will not understand and fail. There are many basic editors that will come with your system, like notepad for Windows, and vim for Mac and Linux. In the example below, we will by pass using an editor for now as, once again we will create and run our 'Hello World!' code again.

The code below will output our application file into a script called 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 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!')" >>

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.

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.

Posted with STEMGeeks


I have some understanding of Python programming. It's cool to see writing the program on the command line and then running it directly.. cheers

Good one. I will hop on and see when you have come to IDE .
I personally at the time use Jupyter notebook .

What are you planning to post next ?

Thanks @arm008 hoping to start from the basics and then move onto data analysis and maybe development on Hive.