How to Return Multiple Values from a Function in Python

Understanding Functions in Python πŸ’‘ A Python function is a block of reusable code that performs a specific task. Functions help break your code into smaller, more modular and manageable pieces, which improves readability and maintainability. Python functions are defined using the def keyword, followed by the function’s name and a pair of parentheses containing … Read more

Python Tuple Concatenation: A Simple Illustrated Guide

Python tuples are similar to lists, but with a key difference: they are immutable, meaning their elements cannot be changed after creation. Tuple concatenation means joining multiple tuples into a single tuple. This process maintains the immutability of the tuples, providing a secure and efficient way to combine data. There are several methods for concatenating … Read more

Sort a List, String, Tuple in Python (sort, sorted)

Basics of Sorting in Python In Python, sorting data structures like lists, strings, and tuples can be achieved using built-in functions like sort() and sorted(). These functions enable you to arrange the data in ascending or descending order. This section will provide an overview of how to use these functions. The sorted() function is primarily … Read more

Python Return NamedTuple From Function

In Python, you can use the collections.namedtuple() function to create a tuple with named fields. Here is an example of a function that returns a named tuple: In this example, the get_person_info() function creates a Person named tuple with name, age, and height as named fields. The function then creates an instance of this named … Read more

Python Find in Sorted List

Python’s built-in sorted() function enables programmers to sort a list efficiently and easily. On the other hand, the list.sort() method provides an in-place sorting mechanism. Additionally, Python allows for custom sorting using the key parameter in these functions, enabling more advanced sorting scenarios. In this article, I’ll show you how to find an element in … Read more

Python Return Arguments From Function

πŸ’¬ Question: How to return one or multiple arguments from a Python function? Basically, creating a function that reflects one or more arguments like a mirror: Let’s find out! πŸ‘‡ Method 1: Return Single Argument A function can return a single argument by passing that argument variable after the return keyword. If the argument name … Read more

How to Change Tuple Values in Python?

Problem Formulation and Solution Overview This article will show you how to change tuple values in Python. To make it more interesting, we have the following running scenario: Eddie, the Meteorologist at NTCV, has calculated an incorrect five (5) Day weather forecast. These values are saved as a Tuple and require updating. πŸ’¬ Question: How … Read more

Python TypeError ‘set’ object is not subscriptable

Minimal Error Example Given the following minimal example where you create a set and attempt to access an element of this set using indexing or slicing: If you run this code snippet, Python raises the TypeError: ‘set’ object is not subscriptable: Why Does the Error Occur? The Python TypeError: ‘set’ object is not subscriptable occurs … Read more