Panama Hitek Logo
Panama Hitek Logo

How do you create your first Python Script?

Python is an extremely simple programming language that has gained enormous popularity in recent years. It has a huge number of features that make it very attractive to programmers, such as simplicity, the enormous amount of resources that are available, mathematical calculations and image processing tools, among many other things.

I personally find Python to be a very useful programming language. It’s not my favorite (Java will always be), but it helps me solve things quickly and efficiently.

In this post I am going to explain the absolute minimum you need to know to create a simple Python script. Let’s say this will be a tutorial on how to create your first Python program and I’ll show you how to run it.

The first thing we have to do to program in Python is to install the Python compiler. This procedure depends on the platform we are working on. For example, in Windows it is enough to enter this page and download and install the latest version of Python for our operating system. On Linux you can install Python from the terminal.

Once Python is installed, any file with the .py extension can be executed on our operating system. Let’s look at an example.

I have already installed Python and I have created a folder on my C disk called python_test, as shown in the following image:

Now in that folder I am going to create a text file with Windows Notepad. I’ll name it test.txt

Inside of this file, we have to write the following code:

Creating a python script with NotepadWe save the text file and proceed to rename it. Now instead of being named test.txt it will be named test.py. When making this change we must see how the symbol of the file changes. If we installed the Python compiler right, it should look like this:

And that’s it. We already have our first Python script. It is a little program that prints the message “Hello World” on the console. If we execute it (by double clicking) we will see the result. Or not?

When we double click on the script we will see that a black window opens and that it closes immediately. This is because the program runs and ends instantly, since it only has one line of code. I personally recommend testing the scripts we do on Windows using Powershell. For that we go to disk C and holding down the “shift” key, we double click on our python_test folder. This should allow us to see the following context menu:

By clicking there, what we will do is open a command window that will be located in the folder we created. After that we just run the code we create using the following command:

With that, when pressing enter, we should get the following result:

python script results

And there we see, the “Hello world” appears on the console. That is the result we expected to get when we created the test.py file. That single line of code written in a file created with Notepad is enough to create a Python script.

And you will say, this is nonsense! Well, believe it or not, any regular Python scripts start out like this. It sounds simple, but it is precisely that simplicity that has made Python such a popular language.

If we want to achieve something a little more interesting, such as obtaining the system time with a Python script, we simply search the Internet and we will find a code similar to this:

We write that code in the file we made before (test.py) which we can edit directly from Notepad. The result of the execution is as follows:

python script results

That is a script. A piece of code that does something, that gives you an output. That is one of the main uses of Python, creating scripts that solve things that can be as simple as giving you the time of your computer, or as complicated as solving an integral or doing a Fast Fourier Transform. If you want to learn more about Python, I recomend this post about Python Books: 11 Best Python Books for Beginners and All Skill Levels (2021).

Anyway, this is what I wanted to share with you in this post. I just wanted to show you how to create simple scripts and run them. Later I will share with you other more interesting things that I have learned to do over time and that have allowed me to work on very interesting projects using Python, such as this one or this one.

Enjoy.

Antony García González
Antony García González
Ingeniero Electromecánico, egresado de la Universidad Tecnológica de Panamá. Miembro fundador de Panama Hitek. Entusiasta de la electrónica y la programación.

Posts relacionados

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Post relacionados