Python can be fun


Some time ago I had to browse a list of many folders on my computer and delete their subfolders with specific names, along with their content. Shortly afterwards I had to do this again, and it was possible I would have to repeat this time-consuming operation many more times. This made me think about automating this process. I didn’t know any ready-made tools for this kind of recursive browsing and deleting folders. I could use PowerShell to do this: I have some experience with this tool, but I don’t like it very much. I could also use C#, a language I use every day for much more complex tasks. Ultimately, I decided to treat this problem as an opportunity to learn Python from scratch, if only to see if it’s really as simple and easy to learn as people say, and to find out why it’s becoming increasingly popular.

I don’t regret my choice and I’m impressed with how simple and logical Python is.

When coding in C#, Java, JavaScript, or PowerShell, I’ve often wondered why we need so much code for simple tasks. Naturally, there are often reasons for this extra code, and it can be especially beneficial when building complex systems. However, sometimes these overcomplicated language constructs result from underdeveloped technology, or the language designers not being very concerned with simplicity.

Python is the exact opposite of that. Simple tasks are usually performed using one simple command. Redundancy is kept to a minimum, which is immediately apparent when you look at any piece of code. In languages derived from C, code blocks are separated using braces (which is required by the language structure), and we also use indentation to make the code more orderly and readable. So, if indentation is a better way of transparently structuring the code, why not stop there and get rid of the braces? That’s the Python approach: we use only indentation, without any braces, because it’s simple and logical.

So, how do you start the adventure with this nice language?

Installing the environment

Python is over 30 years old and is available for all leading operating systems. On Windows 10, the easiest way to install Python is via the Microsoft Store:

You can also download the installer from https://www.python.org/downloads/, where you can also choose your version. This could be the latest version, not yet available in Microsoft Store.

After a successful installation, running the python command from the command line or Powershell will launch the interactive Python interpreter (which you can exit using the exit () command or the Ctrl + Z keyboard shortcut).

How to start learning

Python is simple and has a lot of tutorials available, which makes it easy to efficiently learn.

Visit https://www.python.org/ to explore the capabilities of the language. It’s best to start learning with syntax and basic commands. A clear description of the commands is available at https://www.programiz.com/python-programming; there are also tutorials available at https://www.learnpython.org.

You can learn the basics of Python in one afternoon. It takes a lot more time to explore the multitude of possibilities offered by additional, specialized libraries which extend Python’s capabilities. For example the powerful Django (https://www.djangoproject.com/ ) or the simpler Flask (https://flask.palletsprojects.com/en/1.1.x/), help you create your own websites or microsites. Meanwhile PyTorch ( https://pytorch.org/) gives you a leading toolkit for machine learning.

Your first app

You can write Python scripts in any text editor. However, it’s more convenient to use the small IDE included with the Python installation, called IDLE. It offers syntax coloring, suggestions and the ability to launch your script by pressing F5.

Python has similar support in Visual Studio. An interesting alternative is the extensive PyCharm IDE by JetBrains ( https://www.jetbrains.com/pycharm/download ) and several other solutions which you can compare e.g. at: https://www.softwaretestinghelp.com/python-ide-code-editors/.

Your script file should have the .py extension. Python is an interpreted language, so you don’t have to worry about compiling your script.

I started my first app by defining two variables (which could as well have been constants, but constants don’t exist in Python). By convention, constants should be named in capitals, but technically they are still variables, so their values can be freely modified.

As you can see, I didn’t define types. Variable type is automatically determined based on their values. We use single or double quotes to denote text values.

Then I import the libraries I will use.

‘Def’ starts the function definition. The body of the function follows the colon (remember about the mandatory indentation). ‘#’ indicates the beginning of a comment, which ends at end of line.

All that’s left is calling the above function for the defined path:

The entire script is these 16 short lines of code:

Summary

Python is certainly not ideal for every application. For example, it lacks strong typing and the scripts aren’t compiled prior to launch. This means it’s better to find another solution for large, monolithic systems or applications where speed is critical. Still, whenever simple and readable code is a priority, it’s worth considering this nice language. Another good reason is its established position, exponentially increasing popularity and plenty of easily available, and understandable, documentation.


Piotr Zyśk

About Piotr Zyśk

Fullstack .Net/Angular developer in Atena since 2019. Previous 16 years spent on IT support for large contact center company, including development and maintenance of local infrastructure. After work I like to spend time on my hobbies, like photography, composing music on my PC or playing virtual racing games. You can also meet me biking in the nearby forests.

Leave a comment

Your email address will not be published. Required fields are marked *