Python: Check if Array/List Contains Element/Value

Introduction In this tutorial, we’ll take a look at how to check if a list contains an element or value in Python. We’ll use a list of strings, containing a few animals: animals = [‘Dog’, ‘Cat’, ‘Bird’, ‘Fish’] Check if List Contains Element With for Loop A simple and rudimentary method to check if a list contains an element is looping through it, and checking if the item we’re on matches the one we’re looking for. Let’s use a for […]

Read more

Python: Check if Variable is a List

Introduction Python is a dynamically typed language, and the variable data types are inferred without explicit intervention by the developer. If we had code that needed a list but lacked type hints, which are optional, how can we avoid errors if the variable used is not a list? In this tutorial, we’ll take a look at how to check if a variable is a list in Python, using the type() and isinstance() functions, as well as the is operator: Developers […]

Read more

Python: Check if Variable is a String

Introduction Python is a dynamically typed language, and the variable data types are inferred without explicit intervention by the developer. If we had code that requires a string to work correctly but lacked type hints, which are optional, how can we avoid errors if the variable used is not a string? In this tutorial, we’ll take a look at how to check if a variable is a string in Python, using the type() and isinstance() functions, as well as the […]

Read more

Seaborn Line Plot – Tutorial and Examples

Introduction Seaborn is one of the most widely used data visualization libraries in Python, as an extension to Matplotlib. It offers a simple, intuitive, yet highly customizable API for data visualization. In this tutorial, we’ll take a look at how to plot a Line Plot in Seaborn – one of the most basic types of plots. Line Plots display numerical values on one axis, and categorical values on the other. They can typically be used in much the same way […]

Read more

Python Inner Functions: What Are They Good For?

Inner functions, also known as nested functions, are functions that you define inside other functions. In Python, this kind of function has direct access to variables and names defined in the enclosing function. Inner functions have many uses, most notably as closure factories and decorator functions. In this tutorial, you’ll learn how to: Provide encapsulation and hide your functions from external access Write helper functions to facilitate code reuse Create closure factory functions that retain state between calls Code decorator […]

Read more

Functional Programming in Python: When and How to Use It

Functional programming is a programming paradigm in which the primary method of computation is evaluation of functions. In this tutorial, you’ll explore functional programming in Python. Functional programming typically plays a fairly small role in Python code. But it’s good to be familiar with it. At a minimum, you’ll probably encounter it from time to time when reading code written by others. You may even find situations where it’s advantageous to use Python’s functional programming capabilities in your own code. […]

Read more

Guide to Flask-MongoEngine in Python

Introduction Building a web app almost always means dealing with data from a database. There are various databases to choose from, depending on your preference. In this guide, we shall be taking a look at how to integrate one of the most popular NoSQL databases – MongoDB – with the Flask micro-framework. In this guide, we’ll be exploring how to integrate MongoDB with Flask using a popular library – MongoEngine, and more specifically, its wrapper – Flask-MongoEngine. Alternatively, you can […]

Read more

Python: Get Number of Days Between Dates

Introduction In this tutorial, we’ll take a look at how to get the number of days between two dates in Python. We’ll be using the built-in datetime package, that allows you to really easily work with datetime objects in Python. Creating a Datetime Object As datetime is a built-in module, you can access it right away by importing it at the top of your Python file. You can construct datetime objects in a few different ways: from datetime import datetime […]

Read more

Python: Check if Variable is a Dictionary

Introduction Variables act as a container to store data. A developer can use type hints when creating variables or passing arguments, however, that’s an optional feature in Python, and many codebases, old and new, are yet to have them. It’s more common for a variable in Python to have no information of the type being stored. If we had code that needed a dictionary but lacked type hints, how can we avoid errors if the variable used is not a […]

Read more

Python: How to Add Key to a Dictionary

Introduction A dictionary in Python is a collection of items that store data as key-value pairs. We can access and manipulate dictionary items based on their key. Dictionaries are mutable and allow us to add new items to them. The quickest way to add a single item to a dictionary is by using referencing a dictionary’s index with a new key and assigning a value. For example, we add a new key-value pair like this: snacks[‘chocolate’] = 5 Python allows […]

Read more
1 4 5 6 7 8 54