Home » Programming » Python

Checking If A File Exists in Python, With Examples

Checking if a File Exists in Python

This article will outline several methods which can be used to check that a file exists/is readable in the Python programming language. You may wish to check whether a file exists before reading or writing to it to reduce the number of alerts you need to display to the user or to make sure you aren’t accidentally overwriting an existing file (or a combination of both, letting the user know a file already exists and giving them a choice to overwrite it). Using pathlib to Check if … Read more

Home » Programming » Python

How to Remove Duplicates From a List In Python, With Examples

Python remove duplicates from list

This article explores several ways to remove duplicate items from lists in the Python programming language. Using The dict.fromkeys() Method To Remove Duplicates From a List in Python This is the fastest way to remove duplicates from a list in Python. However, the order of the items in the list is not preserved. The fromkeys() method creates a dictionary (a collection of key:value pairs) with keys from a supplied sequence of elements (in this case, a list). As a dictionary cannot contain duplicate keys, they are removed. … Read more

Home » Programming » Python

Using the Python ‘accumulate()’ Function to Aggregate Data, With Examples

Python accumulate()

This article will detail how to use the ‘accumulate()’ function in Python and provide some easy-to-follow example code. The accumulate() function in Python will process an iterable (iterables are things like lists, dictionaries, sets, or tuples – collections containing items which can be looped over) – returning the accumulated sum of the value of each item or running a given function on each item. The value of the accumulated value is stored for each step and returned with the result. The syntax and code in this article are written for … Read more

Home » Programming » Python

Using the Python ‘filter()’ Function to Filter a List, with Examples

Using Python filter()

This article will cover the syntax and usage of the filter function. It will work for both Python 2 and Python 3. The filter function in Python takes an iterable (iterables are things like lists, dictionaries, sets, or tuples – collections containing items that can be looped over) and checks that each item matches a set of criteria, removing those that don’t match. It’s a fast and easy way to filter the values in a list or array. Syntax for Python filter() The filter() function does not need to be imported to … Read more

Home » Programming » Python

Using the Python ‘map()’ Function to Process a List, With Examples

Using Python Map()

This article will explain the map() function in Python and show you how to use it. The Python map() function applies a given function for all items in an iterable (iterables are things like lists, dictionaries, sets, or tuples – collections containing items that can be looped over). The syntax and code in this article will work for both Python 2 and Python 3. Syntax for Python map() The map() function is built into Python, so nothing needs to be imported. Here’s the syntax: map(FUNCTION, ITERABLE) Note that: FUNCTION is a standard … Read more

Home » Programming » Python

Using the Python ‘reduce()’ Function to Aggregate Data, With Examples

Using Python reduce()

This article will show you how to use the Python reduce() function to aggregate data from values in an iterable (iterables are things like lists, dictionaries, sets, or tuples – collections containing items that can be looped over) and reduce them to a single accumulated value. The code and examples in this tutorial will work in Python 3. Syntax for Python reduce() functools.reduce(FUNCTION, ITERABLE[, INITIALIZER]) Note that: FUNCTION is a standard Python function, previously defined in your code It should accept two parameters The first parameter is the current accumulated value up until this point … Read more

Home » Programming » Python

Using Recursion/Recursive Functions in Python, with Examples

Recursive Functions in Python

This article will explain how to use recursion in Python functions, with some example code to illustrate the concept. A recursive function is a function which calls itself one or more times until a condition is met. If a function that uses recursion calls itself too many times, Python will throw an error. As this is a demonstration of a code structure rather than anything with any specific syntax, I’ll show a simple example of a recursive function and leave comments explaining what is going on. Python … Read more

Home » Programming » Python

Raspberry Pi & Python Powered Tank Part III: More Tank

Python Powered Tank Part III More Tank

This was only meant to be a two-parter, but I couldn’t leave well enough alone. Raspberry Pi & Python Powered Tank Part III is an update with better Python code and an improved design. I’ve gone back to fix the biggest issues that were left after Part II. I’ve omitted a bit of the mucking about in this article to keep it focused on what worked. How Did We Get Here? If you haven’t yet checked out the full build process, here are the previous … Read more

Home » Programming » Python

Converting Bytes To String In Python [Guide]

Converting Bytes To String

This tutorial covers converting Bytes to a string variable (and a string to Bytes) in Python versions 2 and 3. What is the difference between a string and a byte string? A string is a series of characters. When stored on disk, the characters are converted to a series of bytes. What series of bytes represents what character is defined by the character encoding. When a string is stored to disk, it is converted to bytes (encoding). When it is read back into a program, the bytes must be converted back to a … Read more

Home » Programming » Python

Checking Type in Python [Guide]

Checking Type in Python

This article explains how to check the type of a variable in the Python programming language. What are Types? The type of a variable determines what it can or can’t do. It determines what value the variable may take and what can be done with that value. Using the type() Function Pass a variable or value to the type() function, and the variable’s type will be returned: myNumber = 4 type(myNumber) # int myString = ‘foo’ type(myString) # str Using the isinstance() Function For more examples, check out the similar isinstance() function: To compare the … Read more