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 readingHow 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 readingExploring 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 readingHow 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 readingWhat 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 readingHow 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 readingHow 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 readingHow 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 readingHow 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.
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 readingHow 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 readingWhat 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.
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 readingWhat 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.
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.
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 readingHow 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 readingWhat 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 readingWhat 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 readingWhat 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 readingHow 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 readingHow 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 readingHow 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 readingHow 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 readingWhat 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 readingHow 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 readingHow 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 readingWhat 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.
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