Shubham Sayon

What Is Python Output Buffering and How to Disable It?

Summary: Python output buffering is the process of storing the output of your code in buffer memory. Once the buffer is full, the output gets displayed on the standard output screen. Buffering is enabled by default and you can use one of the following methods to disable it: Execute the Python code with the -u …

What Is Python Output Buffering and How to Disable It? Read More »

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 …

How To Resolve UnboundLocalError On Local Variable When Reassigned After The First Use? 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 …

How To Import A Module Given The Full Path 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: subprocess.call() function subprocess.run() function subprocess.Popen Class os.system() function os.popen() function 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 …

How To Call An External Command In Python? 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 …

How To Remove Items From A List While Iterating? Read More »

How To Create a Variable Number Of Variables?

To create a variable number of variables in Python, either use a dictionary or a namedtuple. Problem:  How can you store variable number of variables in your Python code?   Example: Consider that there is a requirement wherein you have to store the specifications of a product in a single variable which includes a variable …

How To Create a Variable Number Of Variables? 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 …

How To Ask Users For Input Until They Provide a Valid Input? 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: Run the code. Which of the duplicated entry ends up in …

How To Merge Two Python Dictionaries In A Single Expression In Python? Read More »

How to Search and Replace a Line in a File in Python?

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. Text in the file before replacing a line: There was an idea to bring together a group of remarkable people to see if we could become something more. …

How to Search and Replace a Line in a File in Python? 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 …

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