There are three functions in python that provide vast practicality and usefulness when programming. These three functions, which provide a functional programming style within the object-oriented python language, are the map(), filter(), and reduce() functions. Not only can these functions be used individually, but they can also be combined to provide even more utility.
In this tutorial, we will cover these three functions and some examples of when they can be useful. But before we start, we need to review what lambda (or anonymous) functions are and how to write them.
Lambda expressions are used to create anonymous functions, or…
Python is an object-oriented programming (OOP) language. However, it provides some tools that provide a functional programming style. Some of these tools include the map(), filter(), and reduce() functions. In this tutorial, we will explore the reduce() function and unravel the versatility and usefulness that it provides.
The map() and filter() functions are covered in detail here:
The best way to introduce the reduce() function is to start with a problem and attempt to solve it the old-fashioned way, using a for loop. Then we can attempt the same task using the reduce function.
Let’s say that we have a…
Being able to quickly organize our data into a more readable format, such as when data wrangling, can be extremely helpful in order to analyze the data and plan the next steps. Python offers the ability to easily turn certain tabular data types into nicely formatted plain-text tables, and that’s with the tabulate function.
We first install the tabulate library using pip install in the command line:
pip install tabulate
We then import the tabulate function from the tabulate library in our code:
from tabulate import tabulate
And now we are ready to use the tabulate function!
All Python programmers should know what the and and or operators are in Python and how to use them. Sometimes, however, there are better ways to accomplish the same task performed by those operators.
In this article, we will review two such ways: the all() and any() functions, which provide cleaner and simpler code that is easier to read and interpret.
Let’s say that there’s a job posting for a data science position. This position requires all of the following: at least 10 years of SQL experience, 12 years of machine learning experience, and 8 years of statistical analysis experience…
Writing short, clean, efficient, and readable Python code should always be a priority for any Python developer. Ternary operators, which provide a shorter way to write conditional statements in Python, can be used to do just that.
In this short tutorial, we will learn what ternary operators are, and go over some examples of how they can be used in your code.
Let’s start with the following if/else scenario to find which number, num1 or num2, is the minimum:
num1, num2 = 5, 10
min = Noneif num1 > num2:
min = num2
else:
min = num1print(min)
#…
Indexing a dataframe in pandas is an extremely important skill to have and master. Indexing just means selecting specific rows and/or columns in a dataframe or series.
In this tutorial, we will learn about the loc method, which is the easiest and most versatile way to index a dataframe in pandas.
We will be working with the ufo sightings dataset found here in jupyter notebook. Let’s read in our data into a dataframe and take a look at the top 5 rows of our ufo dataframe:
With the vast amount of Python resources available, it can be extremely difficult to know which available tools are actually useful to know.
In this article, we will focus on four tools that, in my opinion, are incredibly valuable to know in Python. We will first discuss how to slice sequences in Python, specifically lists and strings, including how to easily reverse them. Then we will move on to one of the best ways to loop through a sequence, keeping track of both the index and element, by using the enumerate() function. After that, we will learn about the zip()…
Ever wonder how much time is remaining on some Python code that you’re running? Well, it turns out that there’s an easy way to find out. How? By adding a progress bar to your Python code using the tqdm Python library.
In this tutorial, we will learn how to use the tqdm library to display a smart progress bar as our code is running (even in pandas). In addition, we will see how a more visually appealing option is also available for Jupyter Notebook.
If you’re familiar with Arabic, then you may have noticed that the pronunciation of tqdm sounds…
There is certainly some controversy regarding the benefits of Python coding websites such as codewars or leetcode, and whether or not using them actually makes us better programmers. Despite that, many people still use them to prepare for Python interview questions, keeping their Python programming skills sharp, and/or just for fun. Nevertheless, there’s definitely a place for these resources for any Python programmer or data scientist.
In this tutorial, we’ll look at the best way to extract the most utility out of these python coding problems. We will look at a fairly simple Python coding question and work through the…
Python is an extremely versatile and fun language to learn. However, due to the vast amount of tools it provides, it can be difficult to navigate through what’s actually useful to know and what’s just nice to know.
In this tutorial, we will focus on three things that, in my opinion, are incredibly useful to know in Python. We will first discuss what unpacking operators are (* and **) and how to use them to both unpack and pack iterable objects. Then we will move on to *args and **kwargs. …
A Data Scientist with a passion for teaching.