How to Round Numbers Up/Down/Nearest in Python

Python Round Numbers

This article will show you how to round numbers up, down, and to the nearest 1, 10 100 (or any number) in the Python programming language. Truncation vs. Rounding Truncation isn’t technically rounding, but it serves a similar purpose, so I’ll include it here. It’s the simplest way to shorten a number – just cutting off the characters at the end until it’s in the format you require. Rounding gets the nearest number, whereas truncation cuts off digits from the number. For example: To round 2.9 to … Read more

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

How to use the PHP parse_url() Function, With Examples

PHP parse_url

The PHP parse_url() function processes a given URL and divides it into its individual components.  Here’s how to use it. parse_url() is often used to get the host/domain name from a given URL or the path to a remote host file. Syntax for parse_url() parse_url ( $url , $component ) Note that: $url is the URL (e.g., https://www.linuxscrew.com) which you want to parse Relative URLs may not be parsed correctly $component is optional and can be one of: PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PORT, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URL_QUERY or PHP_URL_FRAGMENT … Read more

Categories PHP

What is the ‘this’ Keyword in JavaScript (Object-Oriented Programming)

JavaScript 'this'

The ‘this‘ keyword in JavaScript is a confusing concept for some – the meaning of ‘this‘ changes based on context.  This article explains what ‘this‘ is. What is Object-Oriented Programming Object-Oriented Programming is a way of structuring your code so that data is organized into objects. JavaScript supports Object-Oriented programming and provides various tools to help implement it. Put simply, rather than having a bunch of separate abstract variables describing an item, you combine them into a single object which represents that item. You can also add functions … Read more

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

PHP isset() – Check if A Variable is Set, With Examples

Using PHP isset()

This article shows you how to use the PHP isset() function to check whether a variable exists and is populated. So, it’s checking if a variable is set. Why would you want to do this? You can’t perform actions on a variable that doesn’t exist or that has no value – you’ll either get unexpected output or an error. So, being able to check that a variable is set and is populated is pretty useful. Syntax for isset() isset ( $var1 , $var2…) Note that: $var1 should be … Read more

Categories PHP

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

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

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

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