How To Resolve UnboundLocalError On Local Variable When Reassigned After The First Use?

Summary: To resolve an UnboundLocalError when the local variable is reassigned after the first use, you can either use the global keyword or the nonlocal keyword. The global keyword allows you to modify the values of a global variable from within a function’s local scope while the nonlocal keyword provides similar functionality in case of … Read more

How To Import A Module Given The Full Path

Summary: To import a module given the full path, the following methods can be used : Using the importlib module Using SourceFileLoader class Using sys.path.append() function Adding __init__ file to the folder containing the module and then importing it Problem: How to import a module if its full path has been given? Example: Consider we … Read more

How To Call An External Command In Python?

Summary: To call an external command in a Python script, use any of the following methods: Whether you are a developer or a system administrator, automation scripts are going to be a common part of your daily routine. These may include automating routine tasks like file backups or health checks. However, maintaining such shell scripts … Read more

How To Remove Items From A List While Iterating?

Summary: To remove items from a list while iterating, use any of the following methods. List comprehension, Reverse iteration with the remove() method, Lambda Function with the filter() method, or While loop with the copy(), pop() and append() functions. Let’s start with defining the exact problem you want to solve. Problem: Given a list. How … Read more

How To Ask Users For Input Until They Provide a Valid Input?

Summary: To accept valid inputs from the user either use a While Loop With Custom Validations or use the PyInputPlus module to avoid tedious validation definitions. You can also use recursion to prompt the user for entering the valid input. Overview Problem: Given a user input; accept the input only if it is valid otherwise … Read more

How To Merge Two Python Dictionaries In A Single Expression In Python?

Summary: To merge two dictionaries dict1 and dict2 in a single expression, use the dictionary unpacking feature z = {**dict1, **dict2}. This creates a new dictionary and unpacks all (key-value) pairs into the new dictionary. Duplicate keys are automatically resolved by this method. Exercise: Which of the duplicated entry ends up in the dictionary? Other … Read more

How to Search and Replace a Line in a File in Python? 5 Simple Ways

Problem: Given the contents of a text file. How to search and replace a specific string or line in the file? Example: Let’s consider the following example where you want to replace the highlighted (bolded) text parts. Text in the file before replacing a line: There was an idea to bring together a group of … Read more

How to Make an Integer Larger Than Any Other Integer in Python?

This article discusses a situation where you are required to implement an integer object which, when compared using the greater than operator >, to any other existing integer value in the program, will always return True.  Before diving into the possible solutions, we must take note of the following: Python 2 consists of plain integers … Read more

Python Infinity

Summary: Python Infinity is an undefined value (negative or positive) such that positive infinity is greater than while negative infinity is lesser than any other value in a given code. The numerous ways of using Python infinity are: Using float(‘Inf’) and float(‘-Inf’) Using the math module. Using the decimal module. Using the β€œNumpy” library. Using … Read more