Python: Catch Multiple Exceptions in One Line

Introduction In this article we’re going to be taking a look at the try/except clause, and specifically how you can catch multiple exceptions in a single line, as well as how to use the suppress() method. Both of these techniques will help you in writing more accessible and versatile code that adheres to DRY (don’t repeat yourself) principles. Let’s start by looking at the problem: try: do_the_thing() except TypeError as e: do_the_other_thing() except KeyError as e: do_the_other_thing() except IndexError as […]

Read more

How to Randomly Select Elements From a List in Python

Introduction Selecting a random element or value from a list is a common task – be it for randomized result from a list of recommendations or just a random prompt. In this article, we’ll take a look at how to randomly select elements from a list in Python. We’ll cover the retrieval of both singular random elements, as well as retrieving multiple elements – with and without repetition. Selecting a Random Element From Python List The most intuitive and natural […]

Read more

Introduction to Data Visualization in Python with Pandas

Introduction People can rarely look at a raw data and immediately deduce a data-oriented observation like: People in stores tend to buy diapers and beer in conjunction! Or even if you as a data scientist can indeed sight read raw data, your investor or boss most likely can’t. In order for us to properly analyze our data, we need to represent it in a tangible, comprehensive way. Which is exactly why we use data visualization! The pandas library offers a […]

Read more

Python: Update All Packages With pip-review

Introduction Updating Python packages can be a hassle. There are many of them – it’s hard to keep track of all the newest versions, and even when you decide what to update, you still have to update each of them manually. To address this issue, pip-review was created. It lets you smoothly manage all available PyPi updates with simple commands. Originally a part of the pip-tools package, it now lives on as a standalone convenience wrapper around pip. In this […]

Read more

How to Access Index in Python’s for Loop

Introduction Python is a very high-level programming language, and it tends to stray away from anything remotely resembling internal data structure. Because of this, we usually don’t really need indices of a list to access its elements, however, sometimes we desperately need them. In this article, we will go over different approaches on how to access an index in Python’s for loop. How to Access Index in Python’s for Loop? The easiest, and most popular method to access the index […]

Read more

Comparing Datetimes in Python – With and Without Timezones

Introduction When working with dates, oftentimes, you’d like to know if a given date comes before or after another date. We can get these answers by comparing dates. In this article, we will learn how to use the Python datetime module to create and compare both naive (without timezone info) and aware (with timezone info) dates. To compare the dates, we will use the comparison operators in Python: , ==, =, !=. Note: The datetime module has two methods for […]

Read more

Python: Safely Create Nested Directory

Introduction File manipulation is one of the most important skills to master in any programming language, and doing it correctly is of utmost importance. Making a mistake could cause an issue in your program, other programs running on the same system, and even the system itself. Possible errors can occur due to the parent directory not existing, or by other programs changing files in the file system at the same time, creating something that is called a race condition. A […]

Read more

How to Merge DataFrames in Pandas – merge(), join(), append(), concat() and update()

Introduction Pandas provides a huge range of methods and functions to manipulate data, including merging DataFrames. Merging DataFrames allows you to both create a new DataFrame without modifying the original data source or alter the original data source. If you are familiar with the SQL or a similar type of tabular data, you probably are familiar with the term join, which means combining DataFrames to form a new DataFrame. If you are a beginner it can be hard to fully […]

Read more

How to Use Global and Nonlocal Variables in Python

Introduction In this article we’ll be taking a look at Global and Non-Local Variables in Python and how you to use them to avoid issues when writing code. We’ll be starting off with a brief primer on variable scopes before we launch into the how and why of using global and non-local variables in your own functions. Scopes in Python Before we can get started, we first have to touch on scopes. For those of you who are less familiar, […]

Read more

Ultimate Guide to Heatmaps in Seaborn with Python

Introduction A heatmap is a data visualization technique that uses color to show how a value of interest changes depending on the values of two other variables. For example, you could use a heatmap to understand how air pollution varies according to the time of day across a set of cities. Another, perhaps more rare case of using heatmaps is to observe human behavior – you can create visualizations of how people use social media, how their answers on surveys […]

Read more
1 6 7 8 9 10 54