Month: August 2020

Matplotlib Histogram — A Simple Illustrated Guide

The histogram is one of the most important plots for you to know. You’ll use it every time you explore a dataset. It is the go-to plot for plotting one variable.  In this article, you’ll learn the basics and some intermediate ideas. You’ll plot histograms like a pro in no time using Python and matplotlib. …

Matplotlib Histogram — A Simple Illustrated Guide Read More »

Python String Formatting: How to Become a String Wizard with the Format Specification Mini-Language

Python provides fantastic string formatting options, but what if you need greater control over how values are presented? That’s where format specifiers come in.  This article starts with a brief overview of the different string formatting approaches. We’ll then dive straight into some examples to whet your appetite for using Python’s Format Specification Mini-Language in …

Python String Formatting: How to Become a String Wizard with the Format Specification Mini-Language Read More »

Python One Line Conditional Assignment

Problem: How to perform one-line if conditional assignments in Python? Example: Say, you start with the following code. You want to set the value of x to 42 if boo is True, and do nothing otherwise. Let’s dive into the different ways to accomplish this in Python. We start with an overview: Exercise: Run the …

Python One Line Conditional Assignment Read More »

How To Create a Variable Number Of Variables?

To create a variable number of variables in Python, either use a dictionary or a namedtuple. Problem:  How can you store variable number of variables in your Python code?   Example: Consider that there is a requirement wherein you have to store the specifications of a product in a single variable which includes a variable …

How To Create a Variable Number Of Variables? Read More »

How To Ask Users For Input Until They Provide a Valid Input?

To accept valid inputs from the user either use a While Loop With Custom Validations or use the PyInputPlus module to avoid tedious validation definitions. Some other methods may also fascinate you which have been discussed below. Problem: Given a user input; accept the input only if it is valid otherwise ask the user to …

How To Ask Users For Input Until They Provide a Valid Input? Read More »

How to merge two Python dictionaries in a single expression in Python?

Summary: To merge two dictionaries dict1 and dict2 in a single expression, use the dictionary unpacking feature z = {**dict1, **dict2}. This creates a new dictionary and unpacks all (key-value) pairs into the new dictionary. Duplicate keys are automatically resolved by this method. Mastering dictionaries is one of the things that differentiates the expert coders …

How to merge two Python dictionaries in a single expression in Python? Read More »