Articles from Pratik Choudhari

"A pythonista who loves blogging and automation."

29 Articles from Pratik.

Socket Programming in Python

Learn how to create a web socket client and server in Python using the socket module.


Web Sockets allow a bi-directional channels between client and server whereas REST APIs do not offer a full-duplex connection. Python has built-in support for creating server sockets and connecting to them as a client.

Continue reading

How to limit float values to N decimal places in Python

A handy trick using format specifiers to limit float values.


A float data type is a numeric data type with decimal places. In some cases, float values can go to hundreds of decimal places. Therefore, we need to truncate the value to view only the first N decimal places.

Continue reading

Exploring the statistics module in Python

Learn all functions in the statistics module with examples.


The statistics module is a useful yet overlooked module in the Python standard libraries. It provides functions through which one can calculate almost all statistical values such as mean, covariance, etc.

Continue reading

How to use pprint in Python?

Exploring the pprint library using explainations and examples.


pprint is used to print a beautified representation of an object in Python. It is available as a standard library that comes preinstalled with Python.

Continue reading

What is functools in Python?

Introduction to the higher-order functions library of Python.


Functools is one of the most useful Python standard libraries which contains a collection of higher-order functions.

Continue reading

How to use ThreadPoolExecutor in Python

Optimize your code using thread pools.


If a Python program is heavy on the I/O side, running it in a sequential/synchronous pattern can take a lot of time, and the execution time here can be reduced many folds using threading.

Continue reading

How to work with ZIP files in Python

Learn ZIP file manipulation with the zipfile module.


ZIP format is commonly used as a file archival as well as compression format which is supported across all platforms. Files can be compressed without losing any data. Python has built-in support for ZIP files.

Continue reading

How to work with tarball/tar files in Python

Learn to manage tar files using the tarfile standard module.


TAR stands for Tape Archive Files and this format is used to bundle a set of files into a single file, this is specifically helpful when archiving older files or sending a bunch of files over the network.

Continue reading

How to use dotenv package to load environment variables in Python

Know how environment variables can be set and used in different setup.


Working with environment variables in Python is easy, getting and setting of variables is done using the os standard library, however, what if a user wants to set the environment variables when a program is executed and also avoid version controlling the variable values? The dotenv package does exactly that.

Continue reading

How to count the occurrence of an element in a List in Python

Learn four different ways in which the occurrence of an element in a List can be counted.


A common task when working with lists is to count the occurrence of an element.

Continue reading

How to use Poetry to manage dependencies in Python

Learn how Poetry can be initialized in a project and used to resolve dependencies.


The Python programming language has tools for managing environment and interpreter versions such as venv and pyenv respectively. We have talked about virtual environments in Python and how to use them in this blog post.

Continue reading

What does the yield keyword do in Python

Understand the use of yield keyword with examples.


The yield keyword in Python is used exclusively with generators to return values on iteration. In this article, we will explore yield in terms of its use and purpose with examples.

Continue reading

How to access and set environment variables in Python

Learn how environment variables can be retrieved and created using Python.


Environment variables are stored at the operating system level as key-value pairs, they play a crucial role in abstracting sensitive information.

Continue reading

What are virtual environments in Python and how to work with them

Understand why virtual environments are required and how to manage them.


Python has robust support for third-party libraries. Instead of writing code themselves, users can install already built solutions using pip (a package management tool for Python). Although this ability to easily integrate foreign packages gives Python a superpower, managing the packages and their versions can quickly turn into a mess.

Continue reading

What is super() in Python

Learn the meaning of super and its usage with examples.


In Python, the super keyword is used to refer to the parent class. In this article we will understand the usage of super() and why it is required with examples.

Continue reading

What is the meaning of single and double leading underscore in Python

Learn how the number of underscores affect the accessibility of variables in Python.


Python does not have access identifiers like public, private and protected. Single and Double underscores are used as alternatives for pseudo access restriction.

Continue reading

How to convert a string to float/integer and vice versa in Python

Learn type conversion between string, float, and integer in Python.


A data type defines the type of operations that can be performed on the data stored in a variable, these data types support type conversion which means to convert one data type into another based on some assumptions and rules.

Continue reading

What are metaclasses in Python

A beginners guide to metaclasses in Python.


Metaclass is an advanced Python concept and is not used in day-to-day programming tasks. They allow a user to precisely define a class state, change its attributes and control method behavior.

Continue reading

What are global, local, and nonlocal scopes in Python

Understand scopes and their usages with examples.


Scope is defined as an area where eligible variables can be accessed. To enforce security, programming languages provide means by which a user can explicitly define these scopes.

Continue reading

What is self in Python classes

Learn how class states are controlled and tracked using a single word.


In object-oriented programming, a class is a template that defines methods and variables common to all objects of a certain kind. The self word in Python refers to an instance of a class, it is not a keyword and can be replaced by any other name.

Continue reading

How to check if an object is iterable in Python

Three methods to check iteration status of a variable in python.


An iterable is an object which can be traversed. Data structures like array, string, and tuples are iterables.

Continue reading

How to slice sequences in Python

Learn slicing with its variations and examples.


Slicing of sequences in Python is a crucial and easy to learn concept. In this article we will see different types of slicing and understand them with examples.

Continue reading

How to read and write files in Python

An easy guide to master file handling in Python.


Python has in-built functions which can be used to perform file operations such as opening a file, reading its content, writing content, and closing a file.

Continue reading

How to remove duplicate elements from a List in Python

Three ways in which Lists in Python can be deduplicated.


A list data structure is capable of storing elements of different data types and multiple occurrences. In some cases, lists need to be deduplicated, which means we have to remove copies of elements from the data structure.

Continue reading

What is assert in Python

Learn what the assert statement is and its usage in Python.


Assert statement in python is a way to check for unrecoverable conditions before proceeding further in a program. It prevents runtime errors by evaluating causes that could certainly raise an error after performing a few operations. It is similar to a self-checking mechanism for a program, a bug-free program should not be affected by asserts. An assert is equivalent to:

Continue reading

How to flatten a list of lists in Python

Know how vanilla Python and a few libraries can be used to flatten a list of lists.


A flat list is a type of list which is not nested, for example:

Continue reading

How to delete files and folders in Python

Delete files/folders in Python using os and shutil, and pathlib.


In a previous blog, we discussed how to copy files using python. In this article, we will see how os, pathlib, and shutil libraries can be used to delete files and directories on a computer.

Continue reading

What is __init__.py file in Python

Understand why __init__ file exists in Python packages.


This article explains why the __init__.py file exists in Python packages.

Continue reading

How to copy files in Python

Learn how to leverage the shutil package to copy files in python.


Python is widely used as an automation tool and one of the major automation tasks is copying files from a source to a destination.

Continue reading