Lists and Tuples

In this posts we intend to introduce the use of lists and tuples for those who are new to programming or are migrating from some other language, as well as those who have been in Python for a while and want to expand their knowledge and improve their code. As a first approach, it could be said that lists and tuples are what we in other languages call vectors or arrays. However, they have several differences that we'll learn through this tutorial.

A list is not the same as a tuple. Both are an ordered set of values. Values can be any Python object: numbers, strings, functions, classes, instances, etc., and even other lists and tuples. The difference is that lists have a series of additional functions that allow an extensive handling of the values they contain. On the contrary, the content of a tuple cannot be changed once created. Thus we can state that lists are dynamic or mutable, while tuples are static or immutable. Let's start with lists.

Read more…

Create PDF Documents in Python With ReportLab

ReportLab is an open source toolkit for creating PDF documents from Python. It is a very extensive library with many features, from small texts and geometric figures to large graphics and illustrations, all of which can be included in a PDF. In this post we will be approaching to its general features and main functions to create this type of documents.

Read more…

Run Python Application at Startup on Windows

On Windows, it is something common for a program to run when the system starts. If we have an application written in Python (either as .py source code or packed as an executable .exe file) and we want it to be executed when Windows starts, we can include in it a code to let the operating system know that it must be invoked whenever the computer is turned on. The procedure is simple. We create a new entry inside the registry, in a specific location where Microsoft's system will be able to read our program's path and run it on startup.

Read more…

Background Tasks With PyQt

Every library for desktop applications development works with a main loop that manages events such as displaying, moving, or resizing the window, responding to a button or keyboard press, or any other interaction with the user interface. Some of those events might be connected to our own functions; for example, a button1_pressed() method might be invoked by a library when the user presses the button1 widget. When working with Qt, the proper way to respond to those events is by connecting a signal to a slot.

But the problem arises when, while responding to a certain event or during the interface setup, we execute a task whose duration is not negligible (we could say that any task that lasts for more than one second is not negligible). When this happens, the processor is busy executing our heavy task and is not able to execute our application's main loop, thus our interface stops responding: we cannot move it, close it, resize it, nor interact with it in any other way.

Read more…

The None Datatype

Python is a dynamic-typed language, which means that it is not necessary to indicate the type of a variable when creating it, nor is mandatory for a variable to keep its datatype during the program execution. A variable might be created as an integer, then changed to a string, then to a floating point, etc. However, every variable in a program has some datatype (or, more precisely, every variable is an instance of some class, since everything in Python is an object) which can be known via the type() built-in function:

>>> pi = 3.14
>>> type(pi)
<class 'float'>

Read more…

Placing Widgets in Tk (tkinter)

Tk provides three methods to position widgets within a window, which are represented in code by the pack(), place() and grid() functions. They differ in versatility and restrictiveness, so which one you should use it will depend on the result you want to achieve. Let's make an approach to each one of them and see how they work. It is worth noting from the begining that these methods should not be mixed in a single window.

Read more…

'python' is not recognized as an internal or external command

'python' is not recognized as an internal or external command, operable program or batch file. When this error happens while typing python in the Windows terminal (or command prompt), then you first need to ensure that Python is installed. For this you can press or click the Start button (the one with the Windows logo) and then type "python" (without quotes). On Windows 8, look for the search button at the top-right corner of the screen.

/images/python-is-not-recognized/windows-start-python.png

If you don't see something similar to the previous image, then you don't have Python installed. Download the latest version from https://python.org and when running the installer make sure to check the "Add Python 3.x to PATH" option. But if you do have Python installed, and nevertheless the python command is not working in the terminal, then keep reading.

Read more…

Background Tasks With Tk (tkinter)

During the development of a desktop application with the tkinter standard module, it is usual to reach the situation where a heavy operation (i. e. a task which lasts at least two or three seconds) freezes our window and every widget within it. When this happens, the user is not allowed to interact with our application anymore, nor can our code make changes to the interface (like increasing the value of a progress bar). This might be the case, for example, when we try to download a file via HTTP, to open a big file, to send an email via SMTP, to execute a command via subprocess, etc.

Read more…

Tetris With PyGame

This time we present the source code of a simple implementation of the classic Tetris game using the PyGame library for 2D game development. The program has less than 500 lines, but we admit it was not an easy task as it seemed a priori. Although the game is very simple, its development is a meticolous task that requires patience.

/images/tetris-with-pygame/tetris.gif

Read more…

Send File via Socket

The following code establishes a client-server connection between two Python programs via the socket standard module and sends a file from the client to the server. The file transfer logic is contained in two functions: the client defines a send_file() function to send a file through a socket and inversely the server defines receive_file() to receive it. This way the functions can be easily moved from this post to other programs that already implement a client-server connection. The code is ready to send any file format and size. Python 3.8 or greater is required.

Read more…